před 2 dny

Upgrading PHP with Homebrew

Are you using josegonzalez brew repository according my previous blog post? You will need switch your brew to an official PHP repository.

brew untap josegonzalez/homebrew-php
brew tap homebrew/dupes
brew tap homebrew/versions
brew tap homebrew/homebrew-php

Then you can exec upgrade everything:

brew update && brew upgrade

Do not forgot change your shortcut in your bash profile:

alias php-start="sudo launchctl load ~/Library/LaunchAgents/homebrew.mxcl.php55.plist"
alias php-stop="sudo launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.php55.plist"
alias php-restart="php-stop && php-start"

You can also getting an error Dubious ownership on file… then change *.plists rights:

sudo chown root ~/Library/LaunchAgents/*
sudo chmod 644 ~/Library/LaunchAgents/*

#brew #php #osx #mac

19. 1. 2014

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);

 

17. 1. 2014

Find all unnecessary images without link from code

How to find all unnecessary images in your website project?

find public/images -type f|while read line ; do printf "$(basename $line) > " && ack --nofilter -l -c "$(basename $line)" | wc -l; done

Output will be:

sprite.gif > 0
ajax-loader-bg-f9.gif > 2
ajax-loader-bg-trans.gif > 3
ajax-loader-static-bg-trans.gif > 2
bg_about.png > 2
...

If you want output filenames not just marches count remove | wc -l

1 2 86 87