Welcome Guest, Not a member yet? Register   Sign In
Need help with a plugin system
#1

[eluser]Yorick Peterse[/eluser]
Ok guys, I need your help Smile

I am currently working on an improved plugin system for CI, which works a bit like the WordPress plugin system. The idea is that you write a simple plugin and specify when to execute your plugin. For example, your plugin could look like the following:

Code:
<?php

// Hello world function
function hello_world() {
    // Echo the most famouns combination of letters
    echo "<strong>Hello world!</strong>";
}

function add_filter() {
    
    $data = array(
    'filter_event'        => 'index',
    'filter_function'     => 'hello_world'
    );
    
    // Return it
    return $data;
}


?&gt;

The plugin library looks like the following:

Code:
&lt;?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
* @name Ext.Plugin
* @version 0.1
*
* Ext.Plugin is an extended plugin system for CodeIgniter. It works just like the WordPress plugin system
*
* Copyright (c) 2009 Yorick Peterse
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

class Ext_Plugin extends Controller {
    
    // Get all plugins and execute them at a given time
    function get_plugins() {
    
    // Get all plugins with the ext_ prefix
    if ($handle = opendir('./system/application/plugins/')) {
        
        // Loop through each file
        while (false !== ($file = readdir($handle))) {
        
        // Exclude the dots and files that don't start with ext_ and end with .php
        if ($file != "." && $file != ".." && preg_match("/[.](php)$/",$file) == true && preg_match("/(ext)[_]/",$file) == true) {
            
            // Remove the .php extension
            $plugin = str_replace(".php","",$file);
            
            // Load the plugin
            $this->load->plugin($plugin);
            
            // Get the filter settings
            $filter = add_filter();
            
            $filter_event     = $filter['filter_event'];
            $filter_function     = $filter['filter_function'];
            
            // If statement to see if the current function equals the specified filter function
            
            
        }
        }
        closedir($handle);
    }
    }
}
?&gt;

The system will fetch the $data array and will execute the 'filter_function' when the 'filter_event' occurs, atleast, that's the idea. Currently I'm having trouble "injecting" the plugin into the specified function. At first I thought I could use URI segments, but that isn't going to work out when the user hasn't specified those (for example on the index page).

I'd like to know if there's any other way to do it without having to rely on those URI segments or on the CI hook system Smile
#2

[eluser]woopsicle[/eluser]
some other ways of achieving this..

http://blog.leeane.com/2009/01/plugin-ar...deigniter/
http://blog.danielgratzl.de/
#3

[eluser]Yorick Peterse[/eluser]
[quote author="woopsicle" date="1242019527"]some other ways of achieving this..

http://blog.leeane.com/2009/01/plugin-ar...deigniter/
http://blog.danielgratzl.de/[/quote]

Thanks, will look into it Smile
#4

[eluser]Yorick Peterse[/eluser]
Ok so I took a look at the first link, even though it seemed to be the thing I'm looking the example loads the plugins manually in the view files. I'm looking for a way so that you will only have to put the plugin into the plugins directory to make it work, without having to edit any code at all.
#5

[eluser]xwero[/eluser]
The question is what can the plugins do? Is their function adding content or is their function to alter the controllers behaviour?

Wordpress plugins are easy to develop as it it a closely coupled system. CI is a framework so you can't go to specific because then you loose flexibility.

If you want to add plugins you need to provide triggers. This means you have to change the controller method or the view to do this if you want the plugin to do it's job.

As an alternative trigger for plugins that add content to the page you can connect them to view files. But then you still have to change the view to echo the content if the content needs to be embedded in the view. If the plugin content has to come before or after the view file it is a nice solution i think.
#6

[eluser]Yorick Peterse[/eluser]
[quote author="xwero" date="1242054156"]The question is what can the plugins do? Is their function adding content or is their function to alter the controllers behaviour?

Wordpress plugins are easy to develop as it it a closely coupled system. CI is a framework so you can't go to specific because then you loose flexibility.

If you want to add plugins you need to provide triggers. This means you have to change the controller method or the view to do this if you want the plugin to do it's job.

As an alternative trigger for plugins that add content to the page you can connect them to view files. But then you still have to change the view to echo the content if the content needs to be embedded in the view. If the plugin content has to come before or after the view file it is a nice solution i think.[/quote]

The idea was to load the plugins in the constructor or a certain function, before anything else was being executed.
#7

[eluser]xwero[/eluser]
[quote author="Yorick Peterse" date="1242054965"]The idea was to load the plugins in the constructor or a certain function, before anything else was being executed.[/quote]

For that i think you better change the frontcontroller, codeigniter.php, instead of extending the controller because extending the controller can cause problems if the final controller has other code in the constructor.

But then the problem of placing the plugin content isn't solved as it would be added before all other content.

Break the plugin problem in to little pieces before you start coding. Now you already have code and you want to use that to make it work.

I think the most important question is what do you want your plugins to do.
#8

[eluser]Yorick Peterse[/eluser]
[quote author="xwero" date="1242056083"][quote author="Yorick Peterse" date="1242054965"]The idea was to load the plugins in the constructor or a certain function, before anything else was being executed.[/quote]

For that i think you better change the frontcontroller, codeigniter.php, instead of extending the controller because extending the controller can cause problems if the final controller has other code in the constructor.

But then the problem of placing the plugin content isn't solved as it would be added before all other content.

Break the plugin problem in to little pieces before you start coding. Now you already have code and you want to use that to make it work.

I think the most important question is what do you want your plugins to do.[/quote]

For now each plugin should be fairly basic, for example they should be able to change the output results, replace things, etc.
#9

[eluser]xwero[/eluser]
Changing the output results and replacing things are two things that are not basic Smile

If a plugin has to change output result it needs to know which model methods are used by the controller method and needs to know how it can change the model method to get another result. The latter is the most difficult as it's usual to hardcode this in the controller method.
If the plugin has to replace things it need to know in what order the views are called and then there must be a way to change it.

In short the plugin needs to hook in your controller code on a deeper level than executing plugins before everything else runs. What you are describing is possible in a tightly controlled environment, for example a CMS.

So now the question is do you want to create plugin functionality for a thightly controlled environment or do you want to create plugin functionality developers can use in the open environment that CI is?
#10

[eluser]Yorick Peterse[/eluser]
[quote author="xwero" date="1242066180"]Changing the output results and replacing things are two things that are not basic Smile

If a plugin has to change output result it needs to know which model methods are used by the controller method and needs to know how it can change the model method to get another result. The latter is the most difficult as it's usual to hardcode this in the controller method.
If the plugin has to replace things it need to know in what order the views are called and then there must be a way to change it.

In short the plugin needs to hook in your controller code on a deeper level than executing plugins before everything else runs. What you are describing is possible in a tightly controlled environment, for example a CMS.

So now the question is do you want to create plugin functionality for a thightly controlled environment or do you want to create plugin functionality developers can use in the open environment that CI is?[/quote]

I wanted to make a plugin library that would extend CI's plugin system, thus giving more possibilities without having to do a lot of extra work. Considering the difficulty of it I might drop the idea though.




Theme © iAndrew 2016 - Forum software by © MyBB