Today I wrote a CakePHP plugin for localizing JavaScript files. Basically it allows you to use the translate convenience function, __(), in your JavaScript files and will cache the results so that the static file can be used on all subsequent requests. Way back I wrote an asset plugin that combines and compacts JS and CSS files automatically. I thought it would be cool if I could get the two plugins to work together, so that if they were both installed a localized, combined, and compacted JS file would be returned to the user.
In the asset plugin I needed to know if the localization plugin was also installed and if it was use it. Cake’s App::import() function returns “boolean true if Class is already in memory or if file is found and loaded, false if not.” Perfect. I moved the majority of the localization plugin code to a model, JsLang. Then I can simply do:
$this->Lang = false;
if (App::import('Model', 'Js.JsLang')) {
//plugin is installed.
$this->Lang = ClassRegistry::init('Js.JsLang');
}
From there I can simply check if $this->Lang is not false and then use it for added functionality.
It’s pretty late and I’m programming on fumes, so assume that something is messed up somewhere. Both plugins have pretty good test coverage, but that doesn’t mean I didn’t fuck something up somewhere.
If you plan on releasing ANY CakePHP code, do it as a plugin. There isn’t any reason not to. Even if it’s the simplest of helpers, it’s worth it just to be able to provide unit test coverage.
