TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Advanced PHP Programming (http://www.talkphp.com/advanced-php-programming/)
-   -   Plugin System (http://www.talkphp.com/advanced-php-programming/2503-plugin-system.html)

freenity 03-21-2008 11:22 AM

Plugin System
 
Hi all.
I have never done a plugin system and I would like to here your thoughts/suggestions or read some articles/anything about how a plugin system should work or should be designed.
The plugin system I'm talking about is like the one for a CMS or even Wordpress that of course extends the functionality of the program.
Thanks

Gareth 03-21-2008 12:48 PM

I would also be very interested in this!

Wildhoney 03-21-2008 01:20 PM

I think probably the best way to go about a plugin system is via hooks. But the plugin should work via a class file which is placed in a special pool folder for plugins. You would do something then like the following:


php Code:
class My_Plugin extends Plugin_Interface
{
    public $m_szHook = 'comment-post';
}

Then in your code you would initiate this class if the hook is valid. All the comment data would then be forwarded to the construct in an array. You would then feed back the data as an array -- and all would be well, the comment would be posted!

freenity 03-21-2008 08:30 PM

Thanks for answering Wildhoney.
I don't quite understand this :S
Let's take an example of a CMS, where I make plugins to extend it's functionality, like say, news plugin, photogallery, blog...
What I'm thinking on now, is have a folder (plugins) where all the plugin files go. Then the cms looks into that folder and knows what plugins I have and what I don't, well then, I have a plugin file (main.php or something) this file will contain a plugin class (ex. News), this class will have several methods like Install() - that will install the plugin (create a needed db, configure some stuff, etc), Configure(), render() - that will actually show the news, and it should be executed on the page where the news will be shown....

Am I right about this?? or is there better ways to do it.

flyingbuddha 03-27-2008 07:10 PM

freenity: this interests me too, I don't really have much experience of how others do it, but here's how I do it in my cms -

PHP Code:

// snipped to the main part
function field($params=array()){
    if(
method_exists($this,$params['type'])){
        
$op call_user_func(array($this,$params['type']),$params);
        return 
$op;
    }
    else{
        return 
'field type not defined';
    }


now when I'm looping through my fields to build the form I call that method (field), I then have several core field types defined such as text,textarea,select,checkbox,radio, etc., example -

PHP Code:

// textfield - I've stripped out all the code so you can see basics
function textfield($params=array()){
    return 
'<input type="text" name="" id="" value="" />';


So I could do something like -
PHP Code:

echo $object->field(array(
    
'type' => 'textfield',
)); 

Obviously these would be called internally, you wouldn't just echo the output, but I'm keeping my examples lean.

This method allows me to keep my core methods (textfield,textarea,etc.) in one place, but gives third parties the chance to 'extend' my class and call their own, i.e.

PHP Code:

function freenity($params=array()){
    return 
'Hello world';
}

echo 
$object->field(array(
    
'type' => 'freenity',
)); 

Alternatively, I could write an api call so people wouldn't need to extend my class, ie. $object->plugin(...);

Hope this spurs some ideas :)

freenity 03-28-2008 05:08 PM

thanks.
:)

abiko 03-30-2008 08:48 PM

Well, this is my way of doing plugings - I write them like a standalone app - with their templates, classes etc.

I have a config file in which is described which plugin go to what page. (About - article, gallery - using gallery plugin).

And I just make a plugin.php file that is loaded to the CMS, and it is using all the global vars.
Example.

CMS calling the plugin.php -
PHP Code:

<?php
 
// Loading the plugin "controller"
  
include_once( 'lib/pluginFrontController.php');
  
$action $engine->url->action;                
  
$fc = new x3PluginFrontController$module$action );  
  include_once( 
'tmpl/index.php');
?>

And the pluginFrontController.php -
PHP Code:

<?php
include_once '__pluginPrototype.php';
class 
x3PluginFrontController extends __pluginPrototype {
  protected 
$module;

public function 
__construct $action$module ) {
 
parent::__contruct();
switch ( 
$action ) {
 case 
'view':
  include_once 
'x3PluginView.php';
  
x3PluginView::__construct$module );
 break;

default:
 include_once 
'x3PluginMain.php';
x3PluginMain::__construct$module );
break;
}
}
}

?>

And at last - pluginPrototype :
PHP Code:

<?php

class __pluginPrototype {
  protected 
$article  "table_article";
  protected 
$comment  "table_comments";
  protected 
$user     "table_users";

public function 
__construct() {} 

protected function 
__doSomeAction() {}
}
?>

In plugin prototype I've added database table names as protected and some function that will be use trough all the classes - eg. check data, or check user..

All "actions"- view, comment etc are seperate classes in which you define what is going to happen in that action - view - loads the selected article etc.

I used to write all this in one big class - and it become ove bloated - one class had ~1200 lines. So this seems like a good thing - for now.
If anybody has any suggestions - please suggest :)
Critcism is always welcome.

This could be modified to use the real MVC platform - like ZendFramework/Cake/Kohana ...

Tanax 04-01-2008 11:43 AM

A question on this subject, is this possible?

php Code:
/**
||||||||||||||||||||||||||||||||||||||||||
|||| @author Tanax
|||| @copyright 2008
||||||||||||||||||||||||||||||||||||||||||
**/


    class TANAXIA_PLUGIN_HOOK {
       
        public static $db;
        public static $plugin;
       
        public static function setDatabase($db) {
           
            self::$db = $db;
           
        }
       
        public static function loadPlugin($name) {
           
            self::$plugin = new $name(self::$db);
           
            return self::$plugin;
           
        }
       
    }

Tanax 04-01-2008 12:07 PM

Quote:

Originally Posted by Tanax (Post 13039)
A question on this subject, is this possible?

php Code:
/**
||||||||||||||||||||||||||||||||||||||||||
|||| @author Tanax
|||| @copyright 2008
||||||||||||||||||||||||||||||||||||||||||
**/


    class TANAXIA_PLUGIN_HOOK {
       
        public static $db;
        public static $plugin;
       
        public static function setDatabase($db) {
           
            self::$db = $db;
           
        }
       
        public static function loadPlugin($name) {
           
            self::$plugin = new $name(self::$db);
           
            return self::$plugin;
           
        }
       
    }

Nvm.
It worked :-)

abiko 04-01-2008 12:07 PM

It is possible, if you look into Zends core - simmilar thing can be seen - I've seen simmilar things in Kohanas core - that is why the check for indexAction() or someAction() to be defined as "controllers".

Edit:
LOL, same post time :)

Tanax 04-01-2008 12:59 PM

Nevermind again :-D
I just made a mistake in the include part .. :-)

And yea, hehe we posted the same time :-D Although, I don't really understand your script :-O

abiko 04-01-2008 04:08 PM

Quote:

Originally Posted by Tanax (Post 13046)
Although, I don't really understand your script :-O

What parts are troubling you?

Tanax 04-01-2008 08:15 PM

Quote:

Originally Posted by abiko (Post 13052)
What parts are troubling you?

ehm, like.. all xD :-(


All times are GMT. The time now is 06:30 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0