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.

4 replies on “Symfony2: Loading external libraries”

I don’t like your way of class loading non PSR-0 or PEAR classes but
I thought a better way would be to create a class map? Symfony2 has a command for this or we are trying to create one, but there is support for this one since there is a service. If you do your method for a library with many classes is going to be a nightmare. Also loading to global scope is not safe.

Comments are closed.