Re: What's the best way to make a dynamic plugin architecture?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 8/27/12 4:09 PM, Mark wrote:
On Mon, Aug 27, 2012 at 4:26 PM, Stuart Dallas <stuart@xxxxxxxx> wrote:

2. Let the plugin itself (so in this case PluginOne.php) open itself
and register it to the PluginLoader.

With the first option i have to do eval which i try to avoid if possible.
With the second solution the PluginLoader probably has to be a singlethon.

Why does it need to be a singleton?

Well, i would then do something like this from within the included
plugin file after the class:
PluginLoader::getInstance()->registerPlugin(new PluginOne());

Or something alike.

I'm not sure I see what PluginLoader is doing? It makes more sense to me if you register like so:

PluginLoader::getInstance()->registerPlugin('PluginOne');

Then you get an instance of the plugin:

$plugin = PluginLoader::getInstance()->factory('PluginOne');

Tho, even then I don't see what the PluginLoader is adding to the party.

Well, i'm making the classes up as i type. I don't actually have a
PluginLoader yet. Or rather, i'm just beginning to make it right now.
What it's doing is very simple. Read through the directory of the
plugins and load every single plugin it finds in memory. Then every
plugin registers the mime types it can handle. That information is
stored in the PluginLoader upon which some other place can call:
PluginLoader::pluginForMime("text/html"). Though i still have to take
a good look at that.

But you're right, i can use the factory pattern here.


Ahh, I see. Personally I'd go with the following (pseudocode)...

Inside the PluginLoader constructor (or other method)
   foreach (plugindir)
     require plugindir/plugindir.php
     $plugindir::init($this)

The static init() method calls PluginLoader::registerPlugin('mime/type', 'PluginClassName'). Then pluginForMime does a lookup for the mime type and returns an object of the corresponding type. That way a single plugin can support multiple mime types.

-Stuart

--
Stuart Dallas
3ft9 Ltd
http://3ft9.com/

That sounds sane and i probably go for that :)
Thanks for clarifying it a little.

You should never be calling require() yourself. Just follow the PSR-0 naming standard and use an autoloader, then you don't have to even think about it. There are many existing autoloaders you can use, including Composer's, Symfony2's, and probably Zend has one as well.

Also, the key question is how you'll be mapping your situation to the plugin you need. If it's fairly hard-coded (i.e., "mime type of foo => class Bar"), then just use a simple dependency injection container like Pimple. If it's more complex and situational, then yes a factory is the easiest approach.

--Larry Garfield

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux