Categories
PHP

Symfony2: Loading external libraries

I have always wanted to play with Symfony since I heard many cool things about the framework. Finally got some time and started studying the framework. I would save the experience for another blog post. However I am going to share how easy it is to load external libraries.

Libraries supporting PSR-0 standard
If the library was written according to the PSR-0 standard, it is already well structured with the proper implimentation of namespace. For such libraries, just open up “app/autoload.php” and add the namespace to the array which is passed to the $loader->registerNamespaces() method. Your codes should look something similar to:

Libraries supporting PEAR Conventions
Libraries with support for PEAR conventions could be easily loaded as well. Just pass the autoloader prefix (the first part of the class name or the root directory name followed by underscore) to the $loader->registerPrefixes() method.

Something like:


In such scenarios the class MyLib_MyClass should reside inside __DIR__.’/../vendor/mylib/src/MyLib/MyClass.php – as the PEAR standard suggests.

The Old and Incompatible ones
PHP has a very long history of evolution and there are plenty of community maintained codes which haven’t yet been updated to reflect the modern development standards. There are many 3rd party libraries out there which are old (was written before PHP 5.3) but useful. While there is no direct way to load them into Symfony, thanks to the nifty autoloader, we can easily use them with Symfony2.

Say, we have a class like this:


It is a single file library – no namespace, no PEAR naming. What do we do? We make it PEAR compatible 🙂

# First rename the file according to the class name – KillingCurse.php

# Now create a vendor and source directory under the “vendor” directory of the app. Say, we name “voldemort” as the vendor and name the source directory “src”. The file structure would be- “/vendor/voldemort/src”. This is not a requirement, we could just create any directory under the “/vendor” directory but follwing the Symfony naming convention is a good practice.

# We need to create a prefix, so we create a directory named “Spells” (/vendor/voldemort/src/Spells)

# We put the KillingCurse.php under the Spells directory and define a Spells_KillingCurse class in the same file like this:


The full file now looks like:


# Now we register the “Spells_” prefix to the “app/autoload.php”:


# We use the following codes in the controller:


Points to be Noted:

— \Spells_KillingCurse::load(); We are using a static call which does nothing. But this makes the autoloader load the file in which the bare KillingCurse lives. We used a static call instead of having to initiate an object.

— An extra class without any functionality is added just so that entire file is loaded. Any functions, classes or constants will get imported into the global namespace.

— Be sure to use the “\” before the classes, functions etc. You’re inside a namespace and you must refer to the global namespace to use items in the global scope.

— There could be many other ways to do this. Like I mentioned at the top of this post, I am just beginning Symfony2. I would love to hear new ideas or suggestions to improve the codes.

Categories
Linux

Displaying Skype on Notification Area in Ubuntu 11.10

I am using Ubuntu 11.10 64 bit (don’t ask me why! grr!) and I hate Unity. Besides all the inconvenience – the Skype icon doesn’t appear on the notification area. From Google found this solution:

It works! 😀

Categories
Python

Fetching changed files (diff) between two Git commits in Python

First we import the git module, create a Repo object with the physical path to the git repo. We call the iter_commits() method to get a iterator or generator expression which we pass to list() for converting it to a list item.

To compare one commit to another we use the diff() method of a commit object and pass another commit object as the parameter.

The returned list has all the changed files. Each of this item has two blobs – a_blob or b_blob. a_blob is the blob in the first commit, b_blob is the blob in the last commit – in the selected range.

Each of this blob objects have different properties – name, path, data_stream etc. We’re interested only in the path. We should use both a_blob and b_blob because in case of a renamed file, there are two changes – one is deleted, one is added. a_blob will point to the earlier file, b_blob will be the latest one.

Finally, we print the list of changed files. Easy, is it not? 🙂