
Upgrade Your Drupal Skills
We trained 1,000+ Drupal Developers over the last decade.
See Advanced Courses NAH, I know EnoughUsing the Awesome Xautoload Module
Posted Jul 2, 2012 // 0 comments
I recently discovered the awesome xautoload module by Andreas Hennings (donquixote) when looking for a backport of the Drupal 8 symfony based autoloader to use on the LSD Content Staging Project.
From the project page:
This module implements PSR-0, PEAR, "PSR-0-NG" and "PEAR-NG" (7.x-2.x branch) autoloading for Drupal 6 and Drupal 7.
X Autoload does establish a standard for how to name your module-provided classes and where to put those class files ("PEAR-NG" in $module_dir/lib/), but it can be configured to support other patterns as well. Especially, it does support a Drupal 8 style directory structure (PSR-0 in $module_dir/lib/), if you run PHP 5.3+.
Exactly what I was looking for. The power of the module does not stop there. The xautoload module also has a plugable discovery objects. This means you can use your own standard to use on your projects without registering new spl_autoload functions.
The final feature in xautoload is the ability to use wildcards in your .info file. This will allow your info file to stay clean when you use a large number of classes. Here is an example from the xautoload project page.
1 2 3 4 5 6 7 8 | ; Only look at the top level. files[] = handlers/*.inc files[] = includes/*.inc files[] = plugins/*.inc ; Scan the one-below-top level and deeper. files[] = modules/*/**/*.inc ; Scan the top level and deeper. files[] = tests/**/*.test |
The xautoload module's documentation focuses on the "PEAR-NG" convention. This convention has really long class names with lots of underscores. This works if you are using php 5.2 without namespaces but as soon as you add namespaces it becomes a duplicate of what is already stated in the class name. The PSR-0 standard is what Drupal 8 will be using.
Here is a comparison of PSR-0 to PEAR-NG. I will use an example from the LSD/CSI project
PSR-0
1 2 3 | namespace Drupal\sps\Exception; class ClassLoadException extends \Exception {} |
PEAR-NG
1 2 3 | namespace Drupal\sps\Exception; class sps_Exception_ClassLoadException extends \Exception {} |
Just take a look at the difference and you can see why I choose to use PSR-0.
Using the PSR-0 standard with the xautoload module is easy. There are no hooks you need to implement, just use the Drupal namespace in a lib directory in your project root.
The file format to place your classes in to use PSR-0 with xautoload is <module root>/lib/Drupal/<module_name>/your/name/spaces/classname.php Take the example we had above. The Drupal\sps\Exception\ClassLoadException class is in the sps/lib/Drupal/sps/Exception/ClassLoadException.php.
This may seem a little verbose at first, but when you start working with fifty or so modules, it's easy to get namespace conflicts. This standard is clean and easy to use and keeps your code separated and purposeful so it's easy to maintain.
Team Architect at Phase2, Neil thrives on working with the development team to come up with creative implementations for complex situations. Neil enjoys pushing Drupal past its boundaries to implement cutting edge concepts, and has a ...
About Drupal Sun
Drupal Sun is an Evolving Web project. It allows you to:
- Do full-text search on all the articles in Drupal Planet (thanks to Apache Solr)
- Facet based on tags, author, or feed
- Flip through articles quickly (with j/k or arrow keys) to find what you're interested in
- View the entire article text inline, or in the context of the site where it was created
See the blog post at Evolving Web