Proposal: Auto Register Custom Libraries

63 views
Skip to first unread message

Donald Gilbert

unread,
Dec 4, 2012, 3:49:15 PM12/4/12
to joomla-de...@googlegroups.com
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: https://gist.github.com/4208509

Donald Gilbert

unread,
Dec 4, 2012, 4:31:01 PM12/4/12
to joomla-de...@googlegroups.com

Gary Mort

unread,
Dec 4, 2012, 5:43:09 PM12/4/12
to joomla-de...@googlegroups.com
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')) {
  LibraryLoader::setup();
}

I'm actually doing this pseodo-recursively, ie my folder layout is:
libraries/crafty
libraries/crafty/crafty
libraries/crafty/raptor
libraries/crafty/quotes

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.
Reply all
Reply to author
Forward
0 new messages