Maximum level reached. You cannot reply to that comment.

Connecting CakePHP Plugins

Posted by Matt on Fri, May 29 2009

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.

Here's The Trick

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.

Beta Warning

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.

Plugins: USE THEM

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.

Posted in CakePHP

7 Comments

Andrew said on May 29, 2009
I wonder if anyone is working on a repository for plugins. I remember there was one in development that used the bake tool. Any word if there has been any more development on that tool? If not I wouldn't mind building a repository of sorts.
Matt said on May 30, 2009
Hey Andrew,
There are two that I know of:
The plugin manager

The plugin server
Joshua McNeese said on May 29, 2009
fumes eh? i knew it.
alkemann said on May 30, 2009
Nifty idea :)

Is there a typo in line four? Should it not perhaps be ClassRegistry::init('Js.JsLang', 'Model'); ?
Matt said on May 30, 2009
Yea, you're right. I had App::import on the brain when I wrote that. Fixed.
alkemann said on May 31, 2009
Still missing the "::init" isnt? or is there a global function named ClassRegistry() as a shorthand?
Matt said on May 31, 2009
Yup...fixed once again.

Add new comment