Page 1 of 1
Web-site + plugins - how?
Posted: Thu Jan 21, 2010 8:44 am
by malcolmboston
Ive often wondered how systems such as Magento, Swift mailer etc offer the use of plugins, how would this be reflected in the actual code, i would like to do this for my ecommerce framework but have no idea how to start.
Any points in the right direction would be great.
Re: Web-site + plugins - how?
Posted: Thu Jan 21, 2010 10:05 am
by pickle
I've often wondered that too. I found out when I looked through the source code for MyBB. I'm sure there are as many solutions as there are plugin-able architectures, but this is how MyBB does it.
Embedded in the application are a bunch of different hooks. Plugins can then register with the main application to be run at these hooks. So say you have an authentication plugin - you want to use LDAP instead of the default database. You could then write a plugin that would be run at the "authenticate" hook.
Application code example:
Code: Select all
//Somewhere in a config file, the plugin is registered
$Plugins->register('authenticate','/path/to/my/plugin','functionName');
//somewhere in the application, the hook is called
function authenticate($username,$password)
{
if($Plugins->registered('authenticate'))
return $Plugins->runHook('authenticate',$username,$password);
else
//default authentication code runs here
}
Plugin code example:
Code: Select all
function functionName($username,$password)
{
//check LDAP for valid credentials
return TRUE;
}
Re: Web-site + plugins - how?
Posted: Thu Jan 21, 2010 3:42 pm
by JNettles
For my own framework I keep track of registered plugins in a database and then do a system of events that way I can drop in/drop out functionality whenever my clients' needs change. Keep in mind you may want to run multiple plugins for a single event such as having multiple authentication layers.
So when processing a user login I do something along the lines of....
Code: Select all
$authorized = Events::raiseEvent('authentication', $arguments);
if($authorized)
{
...............
A separate class then loads all registered 'authentication' plugins (which are given a specific order number in the database), runs them and returns either true/false.
*Edit*
I think Pickle showed the same thing but I just wanted to clarify on running multiple plugins for the same event. Its also probably a better idea to inject an array into the event rather than explicitly your arguments as different plugins (even for the same event) could require different data.
Re: Web-site + plugins - how?
Posted: Thu Jan 21, 2010 4:55 pm
by alex.barylski
Look at Joomla, their extension system is by far the most robust I have ever seen, more than Drupal, CMSMS, Typo, ModX, etc...
They really take the idea of extension to the next level, IMO. With that flexibility comes complexity, so be prepared to learn.
