Blog w/ Plugin Support
Moderator: General Moderators
Blog w/ Plugin Support
d11 has inspired me!
In the blog project I am working on currently, I have consulted with the team leader and he likes the idea of introducing plugins into the code. Being completly new to the plugin game, I would not know how to design a backend to accept them.
I have been studying Swift, but it's useage of the plugins is too high-up for me to understand as my target system is very simple.
So does anyone has any psuedo code that can help me with designing a script to accept plugins?
Thanks
In the blog project I am working on currently, I have consulted with the team leader and he likes the idea of introducing plugins into the code. Being completly new to the plugin game, I would not know how to design a backend to accept them.
I have been studying Swift, but it's useage of the plugins is too high-up for me to understand as my target system is very simple.
So does anyone has any psuedo code that can help me with designing a script to accept plugins?
Thanks
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
come up with the list of classes you wish to allow plugins with and how those plugins interact with other things (i.e. do they hook into something, or replace a class, etc). Now you will need to build an interface for each of those to implement to conform to your system. After that, it's a matter of knowing how to hook them in, which can easily be done via a configuration class and servicelocator pattern.
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Re: Blog w/ Plugin Support
Either I'm getting cleverer (*cough* as if) or I haven't made clear how Swift plugins work.Tucker wrote:I have been studying Swift, but it's useage of the plugins is too high-up for me to understand as my target system is very simple.
The concept is simple. The "pluggable" object contains an array. The "pluggable" then allows new objects (plugins) to be loaded into this array. Each of these plugins is given a reference back to the "pluggable" in pretty much the same fashion as the plugin was loaded into the pluggable.
(I'm starting to confuse myself here
Ok so far we've passed a "plugin" passed to a "pluggable" and the "pluggable" passed right back into the "plguin". That creates a cyclic link between the two objects so they both have access to *anything* public in each other.
I'll try to demonstrate this on it's most basic level using PHP4 standards which most OOPers follow
Code: Select all
class pluggable
{
var pluginCollection = array();
function loadPlugin(&$pluginObject)
{
$pluginObject->loadPluggable($this);
$this->pluginCollection[] =& $pluginObject;
}
}
class plugin
{
var $pluggable;
function loadPluggable(&$pluggableObject)
{
$this->pluggable =& $pluggableObject;
}
}
$pluggable =& new pluggable;
$pluggable->loadPlugin(new plugin());Code: Select all
function loadPlugin(&$pluginObject, $name)
{
$pluginObject->loadPluggable($this);
$this->pluginCollection[$name] =& $pluginObject;
}That method to run them looks something like:
Code: Select all
function triggerEvent($event)
{
foreach ($this->pluginCollection as $id => $obj)
{
if (method_exists($obj, $event))
{
//Run it if it exists
$this->pluginCollection[$id]->$event();
}
}
}Code: Select all
function connect()
{
//Do something that connects to a server?
$this->triggerEvent('onConnect');
}Code: Select all
class plugin
{
var $pluggable;
function loadPluggable(&$pluggableObject)
{
$this->pluggable =& $pluggableObject;
}
function onConnect()
{
echo 'Connected!';
}
}Ok I have been thinking of ways to implement this all day based on what d11 says and I just have 2 questions:
a) Would the example you provided allow a plugin to create a new working class that the plugin would operate out of
b) What is with the &'s everywhere? =& and the & in front of parameters
Thanks again
a) Would the example you provided allow a plugin to create a new working class that the plugin would operate out of
b) What is with the &'s everywhere? =& and the & in front of parameters
Thanks again
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
a) I'm not sure how you mean? If you mean can it replace the reference to the pluggable then yes it technically could but then it would not be able to write back to the pluggable. The pluggable could still write to/control the plugin however. If you're asking if you can make the pluggable actually morph into a different object then no, that's not possible (read, not ideal). In PHP4 it's possible to replace $this with a completely different object... this rings bells and sounds sirens in my head but yes that's doable. To make the ahppen to the pluggable from within the plugin you'd need to provide a method in the pluggable which acts as a setter on the object itself. i.e.Tucker wrote:Ok I have been thinking of ways to implement this all day based on what d11 says and I just have 2 questions:
a) Would the example you provided allow a plugin to create a new working class that the plugin would operate out of
b) What is with the &'s everywhere? =& and the & in front of parameters
Thanks again
Code: Select all
class pluggable
{
//Usual plugin support stuff here
function morphObject(&$object)
{
$this = $object;
}
}- blacksnday
- Forum Contributor
- Posts: 252
- Joined: Sat Jul 30, 2005 6:11 am
- Location: bfe Ohio :(