Convert JSON data to valid PHP code

Do you need convert JSON data to valid PHP code without pain? I've got a short script for that! My simple solution used var_export() function. This function generates almost valid, well formatted PHP code from usual JSON data. Invalidity of code can be fix with one regular expression, which replace stdClass::__set_state/ with (object).

Take a closer look at example data. I have here currency.json - PHP code should look something like this:

<?php
 return array (
  'USD' =>
  (object)(array(
     'symbol' => '$',
     'name' => 'US Dollar',
     'symbol_native' => '$',
     'decimal_digits' => 2,
     'rounding' => 0,
     'code' => 'USD',
     'name' => 'US dollars',
  )),
  'CAD' =>
  //...
);

It's can be done in only four lines of PHP code:

$data = json_decode(file_get_contents(__DIR__ . '/currency.json'));
$code = var_export((array)$data, true);
$code = "<?php\n return " . preg_replace('/stdClass::__set_state/', '(object)', $code) . ';';
file_put_contents(__DIR__ . '/currencies.locale.php', $code);

If you need call some function (like gettext) above the variable, just add more regular expression:

$data = json_decode(file_get_contents(__DIR__ . '/currency.json'));
$code = var_export((array)$data, true);

// add gettext function call
$code = preg_replace("/'name' => '(.+)'/", "'name' => __('$1')", $code);
$code = preg_replace("/'name_plural' => '(.+)'/", "'name' => __('$1')", $code);

// save PHP code
$code = "<?php\n return " . preg_replace('/stdClass::__set_state/', '(object)', $code) . ';';
file_put_contents(__DIR__ . '/currencies.locale.php', $code);

#PHP#json#2014

How to clean flash drive before unmount on Mac

How to clean flash drive and delete all hidden (dot) files on mac before unmount? It's simple, save follow commands as flash:

#!/bin/bash

if [ -n "$1" ]; then
  read -r -p "Clean /Volumes/$1/ and unmount? [y/N] " response
  if [[ $response =~ ^([yY][eE][sS]|[yY])$ ]]; then      
    find /Volumes/$1/ -name '._*' -type f -delete
    rm -rf /Volumes/$1/.Spotlight-V100/
    rm -rf /Volumes/$1/.Trashes/
    diskutil unmount /Volumes/$1/
    echo "Done..."
  fi
else
  echo "Flash drive name missing"
fi

Then change the access mode of a file chmod +x flash. To unmount a clean flash drive just run ./flash Flashka (Flashka is name od drive).

#macOS#bash#2013

Run FTP server on Mac OS X

Setup everyhing: sudo subl /etc/ftpd.conf

# match umask from Mac OS×Server ftpd
umask all 022
chroot GUEST /Users/roman/ftp
modify guest off
umask  guest 0707
upload guest on

Start FTP server

sudo launchctl load /System/Library/LaunchDaemons/ftp.plist

And stop again:

sudo launchctl unload /System/Library/LaunchDaemons/ftp.plist

#ftp#macOS#2013

How to safely commit vendor dir with php Composer

Composer is great tool, but sometimes you will need avoid all external dependencies. Sometimes you will need have all source codes in your git repository. Should I commit the dependencies in my vendor directory, and still have all benefits of using Composer? Here are my requirements: - Everything need to be automatically.

  • I will work on application and my packages together.
  • I need a simple way how to commit and publish new version of packages.
  • Workflow need to be simple (mimimum of commands)

How to commit vendor dir?

Remove vendor dir from your .gitignore and add follow lines to root of composer.json

"scripts": {
 "post-update-cmd": ["echo [WARNING] Delete all .git dirs", "rm -rf vendor/**/**/.git"],
 "post-install-cmd": ["echo [WARNING] Delete all .git dirs", "rm -rf vendor/**/**/.git"]
},

That's automatically remove the .git directory of every dependency after the installation or update. You can safely commit everything to your git repo and avoid mistakes with fake git "submodules". Run: composer update and commit everything to your repo. ### How make changes in packages?

When you are using tagged releases (no dev versions) of package then run:

rm -rf vendor/some/package && composer update some/package --prefer-source --no-scripts

For dev versions (dev-master) it's simpler:

rm -rf vendor/some/package && composer update some/package --no-scripts

Now, when you go to vendor/some/package dir it's common git repository - you can make changes, commit and publish new versions of package. But don't forgot run composer update before commit main repo, becouse there is still .git dir in package! ### Disadvantages

  • Large VCS repository size and diffs when you update code.
  • Duplication of the history of all your dependencies in your own VCS.
  • There can be conflicts in commits especially when you work in larger team.
  • Still not avoid commit .git directory after running composer with --no-scripts param.

#composer#git#PHP#2013

Setup email sending from PHP on Mac

First write in terminal: which sendmail wlll return path to sendmail app (in my case /usr/sbin/sendmail). Then open php.ini file and setup:

sendmail_path = /usr/sbin/sendmail -t -i

Create necessary folder and setup postfix permissions:

sudo mkdir -p /Library/Server/Mail/Data/spool
sudo /usr/sbin/postfix set-permissions
sudo /usr/sbin/postfix start

And check if emails will come:

php -r "mail('youremail@domain.com', 'subject', 'message', 'From: <youremail@domain.com>' . PHP_EOL);"

#email#macOS#PHP#2013