TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 03-21-2008, 12:22 PM   #1 (permalink)
The Acquainted
 
freenity's Avatar
 
Join Date: Feb 2008
Posts: 119
Thanks: 17
freenity is on a distinguished road
Default 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
__________________
http://feudal-times.net - My PBB Game
http://gwphp.feudal-times.net - My Blog "Gaming With PHP"
freenity is offline  
Reply With Quote
Old 03-21-2008, 01:48 PM   #2 (permalink)
The Acquainted
 
Gareth's Avatar
 
Join Date: Jan 2008
Posts: 136
Thanks: 4
Gareth is on a distinguished road
Default

I would also be very interested in this!
Gareth is offline  
Reply With Quote
Old 03-21-2008, 02:20 PM   #3 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,258
Thanks: 90
Wildhoney is on a distinguished road
Default

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!
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 03-21-2008, 09:30 PM   #4 (permalink)
The Acquainted
 
freenity's Avatar
 
Join Date: Feb 2008
Posts: 119
Thanks: 17
freenity is on a distinguished road
Default

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.
__________________
http://feudal-times.net - My PBB Game
http://gwphp.feudal-times.net - My Blog "Gaming With PHP"
freenity is offline  
Reply With Quote
Old 03-27-2008, 08:10 PM   #5 (permalink)
The Contributor
 
flyingbuddha's Avatar
 
Join Date: Jan 2008
Location: Birmingham, UK
Posts: 60
Thanks: 10
flyingbuddha is on a distinguished road
Default

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 :)
__________________
Pro. Geek
http://www.mikeholloway.co.uk
flyingbuddha is offline  
Reply With Quote
The Following User Says Thank You to flyingbuddha For This Useful Post:
freenity (03-28-2008)
Old 03-28-2008, 06:08 PM   #6 (permalink)
The Acquainted
 
freenity's Avatar
 
Join Date: Feb 2008
Posts: 119
Thanks: 17
freenity is on a distinguished road
Default

thanks.
:)
__________________
http://feudal-times.net - My PBB Game
http://gwphp.feudal-times.net - My Blog "Gaming With PHP"
freenity is offline  
Reply With Quote
Old 03-30-2008, 09:48 PM   #7 (permalink)
The Contributor
 
abiko's Avatar
 
Join Date: Feb 2008
Location: Croatia
Posts: 90
Thanks: 4
abiko is on a distinguished road
Default

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 ...
__________________
Back from sysadmins to the programmers.
Send a message via ICQ to abiko Send a message via MSN to abiko
abiko is offline  
Reply With Quote
The Following 2 Users Say Thank You to abiko For This Useful Post:
flyingbuddha (03-31-2008), freenity (03-30-2008)
Old 04-01-2008, 12:43 PM   #8 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

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 is offline  
Reply With Quote
Old 04-01-2008, 01:07 PM   #9 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

Quote:
Originally Posted by Tanax View Post
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
__________________
Tanax is offline  
Reply With Quote
Old 04-01-2008, 01:07 PM   #10 (permalink)
The Contributor
 
abiko's Avatar
 
Join Date: Feb 2008
Location: Croatia
Posts: 90
Thanks: 4
abiko is on a distinguished road
Default

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 :)
__________________
Back from sysadmins to the programmers.
Send a message via ICQ to abiko Send a message via MSN to abiko
abiko is offline  
Reply With Quote
Old 04-01-2008, 01:59 PM   #11 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

Nevermind again
I just made a mistake in the include part ..

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

Last edited by Tanax : 04-01-2008 at 02:41 PM.
Tanax is offline  
Reply With Quote
Old 04-01-2008, 05:08 PM   #12 (permalink)
The Contributor
 
abiko's Avatar
 
Join Date: Feb 2008
Location: Croatia
Posts: 90
Thanks: 4
abiko is on a distinguished road
Default

Quote:
Originally Posted by Tanax View Post
Although, I don't really understand your script :-O
What parts are troubling you?
__________________
Back from sysadmins to the programmers.
Send a message via ICQ to abiko Send a message via MSN to abiko
abiko is offline  
Reply With Quote
Old 04-01-2008, 09:15 PM   #13 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

Quote:
Originally Posted by abiko View Post
What parts are troubling you?
ehm, like.. all xD
__________________
Tanax is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 06:52 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design