Check your code compatibility with PHP version

PHP 5.6 is already available over a year and even version 7.0 will come soon, but lot of code is still running on PHP 5.3 or even older PHP versions. The main reason is uncertainty if the code will continue running after the upgrade or if it would crash. Upgrading and checking what is going on is not the best method here, there must be some better way. And fortunately, there is, PHP CodeSniffer is capable of doing this. What you have to do is to install three composer packages, export your composer global bin path and run the phpcs.

So, to begin with, you should install composer:

I usually move it to /usr/local/bin and drop the phar extension:

Install PHP CodeSniffer:

Now export your composer global bin path:

You can insert this command into your .bashrc or .bash_profile script in order to use the new path permanently.

And for the end run phpcs:

We have defined here phpcs to version to 2.3.2 because wimg/php-compatibility package was not compatible with newer versions at the moment of writing this text. If you try to upgrade these packages latter, it is necessary to edit ~/.composer/composer.json file and update composer after that:

Enjoy upgrading your PHP version, 7.0 is coming soon 🙂

Testing Protected/Private Methods in PHPUnit using Closure class

PHPUnit-logo

It’s very well known that this is possible to do using ReflectionClass and setting private/protected method or property as accessible, but I want to show another approach which is using Closure class. So, starting with PHP 5.4 it is possible to bind a closure to an object and also set scope of the closure to be the object’s scope. But instead of explaining this, here is the code, enjoy!

Of course it is possible to use this technique for other purposes also. It breaks encapsulation principle, but it also opens new possibilities. Finally, PHP team allowed us to do so 🙂

Trait injection in Zend Framework 2

zf2-logoThere are several tutorials on the Internet which explain how to use interface based dependency injection in Zend Framework 2. The idea is to make an initializer, figure out which interfaces a class implements and then inject appropriate dependencies using setters defined in the interfaces. Bad part about this is that in each class you implement such an interface you have to declare a property which would hold the injected object and also to implement the setter for it, which is defined in the interface, by the way. In order to simplify this further it is possible to write trait for each interface, but then why should not use just traits? Why do we need interfaces? Is this possible at all?
Function which allowed using interfaces for such a purpose is class_implements. Of course it is also possible to use Reflection class, but that would be slow. Fortunately, PHP 5.4 provided us also with function class_uses, which returns an array of all used traits, great! And here is such an implementation:

It is possible to move constants to module configuration file, but this would slow up things a little bit. Also some code could be moved to separate methods (e.g., find setter name), but this would slow up a little bit more. But, this is a proof of concept, anyway. Nice idea is also to cache the array of available dependency injection traits and share this for all web servers, but I leave this to your implementations.

Just to mention, you invoke this initializer either in Module.php or module.config.php. Using first way, you put it inside the getServiceConfig method:

I am going to skip one step here, which does not belong to this topic, but in order to get the dependency as services, you need to write an abstract factory (e.g., QueryAbstractFactory in my case) or set of factories to create what is requested. This abstract factory class that I use produces Propel 2 Query objects. Finally, here is the trait and also a small usage example:

Fixing fail2ban on Fedora SELinux

Besides correct configuration of fail2ban, it may still refuse to work correctly on Fedora SELinux systems, in my case Fedora 17. The reason is SELinux, like in many cases, but instead of disabling it you may configure it properly. First you should restore SELinux security contexts of the /sbin directory:
restorecon -R -v /sbin
Second, after starting fail2ban you should check for AVC (Access Vector Cache) logs:
grep AVC /var/log/audit/audit.log | grep fail2ban
Now you can copy all lines that you want to allow in SELinux to a new file. Let’s name it avcs. After this you should execute next commands:
audit2allow -M local <avcs
semodule -i local.pp
You can repeat steps of the second phase until all AVC’s are gone. After that fail2ban should be happily working 🙂

Joining non-related tables in Propel 1.6

propel-logoI had this problem few days ago and I couldn’t find any useful information on the Internet. I had to browse through Propel internals to find this out and it would be pity not to share it with others. So let’s start with an example:

$accounts = AccountQuery::create()
->joinWith('Account.AdditionalData')
->addJoin(AdditionalDataPeer::VAL, CountryPeer::ID)
->withColumn('country.name', 'CountryName')
->find();

Someone may ask why Country and Account objects have no relation. Well, this is just an example, but imagine that table AdditionalData contain many different values, so we can’t really make any foreign reference as it would have to reference many different tables, which is not possible. There may be other reasons to use something like this, but anyway let’s explain line by line how it works.

->joinWith('Account.AdditionalData')

This is just normal Propel join where we hydrate the main object with related object data.

->addJoin(AdditionalDataPeer::VAL, CountryPeer::ID)

This is the way how we can define additional join with non-related table. The first value, AdditionalDataPeer::VAL represents the field name in the left table and CountryPeer::Id it’s corresponding field in the right table. These constants are defined in base peer classes. As third parameter here you can define the type of join, but I have used just default (inner join). This works fine except the main object is not hydrated with the related object data. Unfortunately, this is not possible using with() method as with() works only with previous join(). Fortunately, there is another way using:

->withColumn('country.name', 'CountryName')

First parameter here is the real table name concatenated with real column name and the second one is an alias name for this column. You can include as many columns from the related table as you want. It’s probably possible to concatenate some predefined Propel constants for the first parameter, like CountryPeer::TABLE_NAME . ‘.’ . CountryPeer::ID, but I haven’t tried that.

And like the documentation for withColumn() says, you can use this value in the resulting object by method getVirtualColumn():

foreach ($accounts as $account) {
print $account->getVirtualColumn('CountryName') . PHP_EOL;
}

I hope this may help someone 🙂

Pairing Apple Magic Trackpad on Ubuntu 13.10

Apple MagicTrackpadI could find many unresolved tries to connect Apple’s Magic Trackpad to Ubuntu 13.10 system and as I just made it, I decided to share it with you, it may help someone. First, I want to point everyone to Ubuntu’s wiki page:

https://wiki.ubuntu.com/Multitouch/AppleMagicTrackpad

What I have actually done is:


sudo apt-get install bluez-compat
sudo apt-get install blueman

Set you trackpad discoverable and run blueman-manager from console:


blueman-manager

Pairing using this dialog didn’t work for me, although it may work if you run it using sudo. I have just used it’s console output to get the MAC address of the device. After you get it, just copy & paste it into this command:


sudo hidd --connect 7C:C3:A1:47:XX:XX

A small windows should show up asking for PIN code. Enter 0000 and it should connect the device and show up mouse icon in the menu bar. Your trackpad should be working.

If you wonder what happends after restart, don’t worry, just some dialog will jump up and asking whether to connect to the trackpad. Just select always allow connection to this device and that’s all. Have a nice trackpading on your Ubuntu 🙂

Laravel

laravel-logoLaravel is a new generation PHP framework with some unique features like free bundle repository, ORM, MVC or routing model, reverse routing, REST compatibility, class autoloading, unit testing, automatic pagination and much more. Compared to Slim, it offers much more features and one of the most important is the ORM, of course. If you consider a new PHP framework for your new project, you should bet on Laravel I believe.

PyroCMS

If you plan to start a new website using some open-source CMS, you should not consider just a well known ones like Joomla or WordPress. In the meantime, we got a “fresh blood” and one of the best of them is eventually the PyroCMS.
Like it’s authors say, PyroCMS takes a different approach to many content management systems and tries very hard not to try too hard. Give it a try and you won’t regret 😉

Logstalgia

Logstalgia, also known as ApachePong, is a great website access log visualization tool which requires just a busy Apache server and an OpenGL enabled workstation. Requests appear as colored balls which travel across the screen to arrive at the requested location. Successful requests are hit by the paddle while unsuccessful ones are missed and pass through.

Besides normal webserver activities it can visualize DDOS attacks as well:

Great tool!

Eclipse 4.2.2

Although this new version has been released over a month ago, I’ve upgraded my Mac today. First impressions are that performances are really improved, but stability also. If you are still running older versions, don’t hesitate to do an upgrade 🙂