问题
Is there a consensus on how plugins should be implemented in a PHP application?
I've looked into the observer pattern which comes close, it's really just a notification system and doesn't allow code to extend the application directly. I'm currently using a simple hook systems that I came up with:
public function registerHook($hookName, array $params = array())
{
$this->hooks[] = $hookName;
foreach ( $this->plugins as $pluginName => $hooks ) {
if ( in_array($hookName, $hooks) ) {
$plugin = new $pluginName($this, $this->view, $this->controller);
$plugin->{$hookName}($params);
}
}
}
This works well for my purposes but I'm curious if there's a design pattern out there that has been tested and proven many times over and I'm just re-inventing the wheel.
回答1:
There is no consensus as in the Silver Bullet sense. For established patterns, you have multiple options like
- Subject/Observer,
- Signal Slot,
- Event Dispatcher or
- Pipes and Filters
to name a few.
Which you use is up to you, but you should make sure your system architecture supports the modularity. Have a look at these slides for some ideas
- http://qafoo.com/talks/11_06_ipc_spring_modular_application_architecture.pdf
回答2:
I think an Events Dispatcher is a nice and clean way to implement plugins, or any extension for that matter. An Events Dispatcher is an implementation of the Observer pattern, and is being used in symfony, Symfony2 and Zend Framework 2 (beta).
Looking through any of that source on github will make for some interesting reading. Though, an interesting bit of information can be found here:
http://components.symfony-project.org/event-dispatcher/trunk/book/02-Recipes
I wrote an Events and Hooks class a few years back for a project, I'll post that here if I can find it.
回答3:
Take a look at the Yii framework. Yii heavily relies on events which are much cleaner than hooks, actions, etc. Using events, you can allow different parts of the system to talk to each other in an object oriented manner.
回答4:
Zend Framework is using the dispatchLoopStartup() and dispatchLoopShutdown() hooks as class methods. Each plugin is a class that implements the aforementioned methods.
ZF manual reference
回答5:
The way you've done this, with hooks is also how I implement this.
The biggest problem with your sample though, is that your function instantiates the plugin. Why not pass an instance of the plugin instead?
The way I've done this, is that a plugin gets instantiated first, and registers its hooks itself.
回答6:
Well, there's a link to the project called jin-plugin right in the Wikipedia article about Plugin concept. I am seeing this framework first time, too, but, maybe, you can use it right away.
Besides, you should really google for things like "Plugin Pattern", there's just two links I found on first page: Plug-in Pattern, Extensibility pattern (wikipedia).
If it's really a pattern, it should be language agnostic, so you can safely take any existing solution from any language and convert it to PHP.
P. S. Thanks for question, anyway, you really did raise my own interest in this topic. ;)
来源:https://stackoverflow.com/questions/9244313/design-pattern-for-implementing-plugins-in-php-applications