Move legacy PHP project to the modern Laravel ecosystem

Legacy projects can be a pain – you have to not just support an old code but add new features. And at some point this may become a nightmare. However, there are many ways to upgrade it without full re-development at one time. One possible solution is to move such projects to the modern Laravel ecosystem as it’s described in this article.

The idea behind of it is to wrap the old code to handle it by Laravel. Once it’ll be done, the old functionality will work as expected, while all new features will be added in Laravel. Additionally the old code can be refactored and moved under Laravel without a downtime or an interruption of the project. This solution will give a smooth transition from old system to the new one.

Also do not forget about tests – unit and integration, static analyzers like PHPStand and code style control with PHPCS. This will help you to catch possible bugs even before run your app.

Allow inline line breaks for Monolog

By default Monolog ignores line breaks inside debug message and puts it in one line. That’s fine for GrayLog or something similar but is not handy for me during local development. The fix is easy. Just add new Formatter with allowInlineLineBreaks option in true to your StreamHandler and enjoy formatted dumping of arrays:

$logger = new Monolog\Logger('MyLoggerName');
$formatter = new Monolog\Formatter\LineFormatter(
    null, // Format of message in log, default [%datetime%] %channel%.%level_name%: %message% %context% %extra%\n
    null, // Datetime format
    true, // allowInlineLineBreaks option, default false
    true  // ignoreEmptyContextAndExtra option, default false
$debugHandler = new Monolog\Handler\StreamHandler('/tmp/my_debug.log', Monolog\Logger::DEBUG);

CakeFest 2011

I couldn’t participate CakeFest 2011 which was on September 1st – 4th in Manchester, UK. But at least I’d see slides of speeches. I found a few of them:


Trick with variable name transformation in CakePHP

CakePHP is a good PHP MVC framework. You can study it easily and start developing web sites very fast. But it has some disadvantages, of course. One of them is strange rules for transformation variable name when it set in the controller. Statement

$my_var = 'Hello world!';
$this->set('my_var', $my_var);

will make the variable $my_var accessible in your view:
<?php echo $my_var; ?>
The phrase ‘Hello world!’ should appear on the web page. But if the same operation is done using function compact, the result is not predictable:
$my_var = 'Hello world!';

Instead of ‘Hello world!’ you’ll see the warning about undefined variable on your web page. It happens because cakePHP removes underscore and makes letter, followed by it, capital. So, instead of $my_var you should use $myVar in the view:
<?php echo $myVar; ?>

Note that this bug appears in the cakePHP version 1.2. It’s already fixed in the 1.3.

SugarCRM: check updating of object’s property

Sometimes it’s needed to know about updating of specified object’s property. SugarCRM keeps previous values for all object’s properties in the array fetched_row. For example, to realise it’s new object or just updated we should compare ID from that array and current value of object’s property:
if($obj->fetched_row['id'] != $obj->id) {
echo "This's a new object!\n";
} else {
echo "This's an existing object\n";

That approach might be used in the handler of logic hook, for example, to perform some action when desire property is changed.

Managing Standalone Scripts in PHP: part II

The second article about using standalone PHP script shows amazing ability of PHP such as fork, signals and demonizing. Yes, PHP allows to catch signals like SIGINT, SIGHUP, SIGALRM and so on. It’s really cool! When I develop background utils for SugarCRM I didn’t know about those useful PHP features. So, it’s time to make them more efficient and geek.

Run PHP script from command-line

I came across a brief howto “Implementing with PHP: Standalone Scripts”. I’m Perl guy but I have to use PHP in command line to perform some SugarCRM backgound tasks. I found useful the part about parsing command-line arguments with Console/Getopt.php. It may do administration tasks much easier.

Lost in Translation

Today I tryed to implement a functionality to create some CRM entities from email. I needed to retrieve an email, first name and last name (if possible) and body of the email. The last name is a mandatory property. I made a mistake when I used perlish style to initialize the last name:
preg_match('/\"(.*)\"\s+\< (.*)\>/', $from, $from_arr);
$names = preg_split('/\s+/', $from_arr[1]);
$email = $from_arr[2];
$first_name = $names[0];
$email_user = preg_match('/^(.*?)@/', $email);
$last_name = $names[1] || $first_name || $email_user;

But PHP is not Perl. I got “1” as the last name. So, I changed it on PHP manner:
$last_name = $names[1];
if(!$last_name) $last_name = $name[0];
if(!$last_name) $last_name = $email_user[1];

IMHO Perl variant is much better 🙂