
phpplexus
Plexus **\Plex”us**, n. An intricate or interwoven combination of elements or parts in a coherent structure.
Overview
Plexus is a unique plugin API for PHP 5.1.2+ It performs, what I’ve dubbed as “magic wiring” to re-wire any object method call on the fly as a “pluggable” call and automatically creates and calls the plugin hooks. It provides ultimate flexibility, and can assist greatly from the simplest of plugin APIs to complex plugin-driven applications.
To create a Plexus, in the simplest form, appears something like: ``` $plugins_to_load = array( 'PluginName', 'AnotherPluginName', );
$plexus = new Plexus(new PluginCollection($plugins_to_load)); ```
To have a Plexus re-wire an object of class “ClassName” method call, you do:
/**
* Re-wire $object->methodName($param1, $param2);
*/
$plexus->doMethodName($object, $param1, $param2);
Basically, when you tell the Plexus to re-wire ClassName::methodName(), it creates Plugin hooks and calls them and the main method as follows (this is not always true and it is more complicated than this, but it is better to express more precise wiring in visual form): * Plugin::beforeClassNameMethodName() * Plugin::aroundClassNameMethodName() * ClassName::methodName() * Plugin::onClassNameMethodName() * Plugin::afterClassNameMethodName()
Some of the hooks may seem very similar, but they help to serve different purposes. The flowchart below helps to outline this magic wiring.
](http://www.smarterwebdev.com/images/Plexus_API.png)
Plugins automatically have the current object being re-wired (the “base object”) loaded into them during the process, so that they can use it if needed: ``` class PluginName extends Plugin {
public function onClassNameMethodName($param1, $param2) {
if ($this->baseObject->property === true) {
// ... let's do something now ...
}
}
} ```
Plugins also have the current Plexus loaded into them so that they can make pluggable calls on other objects if needed, such as the base object or another object: ``` class PluginName extends Plugin {
public function beforeClassNameMethodName($param1, $param2) {
// ...
$object = new AnotherClass();
// ...
$this->plexus->doAnotherMethod($object, 'this is input param 1');
}
public function afterClassNameMethodName($param1, $param2) {
// ...
$this->plexus->doAnotherMethod($this->baseObject);
}
} ```
Plugins can even have calls to themselves re-wired. You could use the Plugin’s Plexus to accomplish this just as with other objects; however, if you are not overriding the Plugin __call()
method, there is an easier way:
```
class PluginName extends Plugin {
public function afterClassNameMethodName($param1, $param2) {
// Re-wire $this->performSomeAction()
$this->doPerformSomeAction();
}
public function performSomeAction() {
// ...
}
} ```
When it is a Plugin that is being re-wired, the base object is not changed. This is usually the desired functionality, but may not always be. However, if one Plugin wants to access another Plugin, it can do so through its Plexus: ``` class AnotherPluginName extends Plugin {
public function onPluginNamePerformSomeAction() {
$plugin = $this->plexus->PluginName;
// ... do something with the other plugin now
}
} ```
Diagrams & Flowcharts, etc…
UML:
](http://www.smarterwebdev.com/images/Plexus_UML.png)
Flow Chart:
Project Information
- License: GNU Lesser GPL
- 9 stars
- svn-based source control