Proposal: Auto Register Custom Libraries

Showing 1-3 of 3 messages
Proposal: Auto Register Custom Libraries Donald Gilbert 12/4/12 12:49 PM
I'm creating a library that interfaces with Joomla and adds in extra functionality that I find myself using throughout my custom developed extensions. I've added this library as a subfolder (named 'easel') in the libraries folder. In my 'libraries/easel/setup.php', I register a custom prefix for my classes to use. Then, at the bottom of the 'libraries/import.php' file (I'm using 2.5), I call 'JLoader::import('easel.import');'

So far, it's been working great. But, I'm worried about when I upgrade, I'll lose that 'JLoader::import()' line from 'libraries/import.php'.

I think we should create the ability for custom libraries to be auto-registered. The basic idea is when we run the JLoader::setup() method, it does it's normal job, but then recurses through all the top level directories in the libraries folder, and looks for a setup.php file. If one is found, that file will be included and executed. This provides the ability to include a custom library without having to modify any core files.

See this gist for an example:
Re: Proposal: Auto Register Custom Libraries Donald Gilbert 12/4/12 1:31 PM
Re: Proposal: Auto Register Custom Libraries Gary Mort 12/4/12 2:43 PM
Hmm, I use JLoader instead of adding a new file.

For each subdirectory in libraries, I check to see if the file loader.php exists - if so, I include it.

I follow that up with a call to
if (class_exists('LibraryLoader')) {

I'm actually doing this pseodo-recursively, ie my folder layout is:

Where libraries/crafty actually consists of nothing but the file loader.php
libraries/crafty/crafty are my main libs
libraries/crafty/raptor are some libs I needed to integrate the Raptor editor
libraries/crafty/quotes are some libs for my Quotation component

So CraftyLoader->setup() is the only bootstrap item[which I use the fact that the CMS will include a file called defines.php from the folder index.php is in if it exists - so it bootstraps the whole thing]

CraftyLoader->setup() then goes and checks to see if it has any sub-folders and if so, checks them for loader.php files and will include them and execute their setup method if any.

Even though all these classes are subclassed off of JLoader, I actually have been calling JLoader::registerPrefix in each of them rather than add more spl autoloaders.  I figure it's nice to have the functionality of JLoader for the future, but it's not needed.

The main reason I like this method is that it as closely mirrors the existing Joomla process without creating a new api.