nuttycoder Posted April 17, 2009 Share Posted April 17, 2009 Hi, I'm after creating a CMS system that has support for plugins to make my code more modular I have looked all over Google and can't find any tutorials on the subject I assume I need to look at some port of hook mechanism but not to sure how to proceed. Can anyone point me to any tutorials or resources on creating a custom plugin system? Or have any tips on how to get started.. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/154542-how-to-create-a-plugin-system/ Share on other sites More sharing options...
soak Posted April 17, 2009 Share Posted April 17, 2009 Not exactly what you've asked for I know but it might be worth looking to see how apps like Joomla and Drupal do it. Quote Link to comment https://forums.phpfreaks.com/topic/154542-how-to-create-a-plugin-system/#findComment-812582 Share on other sites More sharing options...
laffin Posted April 17, 2009 Share Posted April 17, 2009 This really depends on how You want to implement plugins/modules. I have built two different type of module systems. They work pretty similar. It's just one takes info from a db the other takes info from the script itself (allowing the owner to include the modules). These are filebased, and are included into the script than ya have systems which keep the entire module in the db, I believe this will be eval'd later. The module systems I have made, are just base engines (meaning they dont do nothing themselves except load in the various modules). The modules themselves dictate wut they display/do. than you have a plugin system, which does involve setting up hooks and the like. where u have a standard api. which gives the base system more functionality. Quote Link to comment https://forums.phpfreaks.com/topic/154542-how-to-create-a-plugin-system/#findComment-812615 Share on other sites More sharing options...
nuttycoder Posted April 17, 2009 Author Share Posted April 17, 2009 thanks for that, given me a few ideas. Quote Link to comment https://forums.phpfreaks.com/topic/154542-how-to-create-a-plugin-system/#findComment-812625 Share on other sites More sharing options...
laffin Posted April 17, 2009 Share Posted April 17, 2009 I can show u one of the module systems I wrote up. Its for a basic web server development. QuickPHP Module Loader Quote Link to comment https://forums.phpfreaks.com/topic/154542-how-to-create-a-plugin-system/#findComment-812635 Share on other sites More sharing options...
nuttycoder Posted April 17, 2009 Author Share Posted April 17, 2009 thanks for that but am afraid to say I don't know what to make of it. Quote Link to comment https://forums.phpfreaks.com/topic/154542-how-to-create-a-plugin-system/#findComment-812639 Share on other sites More sharing options...
keeB Posted April 17, 2009 Share Posted April 17, 2009 Well here's the thing.. In order to provide plugin support you need some sort of glue. That is, something that maps your plugins to your architecture. This is actually the hardest part of a plugin system. I suggest you spend some time thinking about what you want your plugins to be able to do, and what they should not be able to do. Once you come up with this, we can start providing a bit more information. Quote Link to comment https://forums.phpfreaks.com/topic/154542-how-to-create-a-plugin-system/#findComment-812648 Share on other sites More sharing options...
laffin Posted April 17, 2009 Share Posted April 17, 2009 I think thats why I chose a modular system over a plugin system. I didnt need an api, as the modules developed for this system, have a 1 time use and dont need interaction with each other. Quote Link to comment https://forums.phpfreaks.com/topic/154542-how-to-create-a-plugin-system/#findComment-812659 Share on other sites More sharing options...
keeB Posted April 17, 2009 Share Posted April 17, 2009 Until you want to extend your system to do something you haven't thought of yet. There's pros and cons to each approach. You can have a modular design that incorporates plugins! Quote Link to comment https://forums.phpfreaks.com/topic/154542-how-to-create-a-plugin-system/#findComment-812673 Share on other sites More sharing options...
nuttycoder Posted April 17, 2009 Author Share Posted April 17, 2009 yeah some valid points. I do need to research which way is better for me as I currently have a system with modules that are integrated into the system then to use a module you would put a tag in a page where you wanted the module to be similar to workdpress like if I wanted a blog I would put [blog] on a page. You can see a demo of my system at http://www.surrectmedia.com/demo to get a better idea of what am using at the moment. The problem with this system is it's slow and to add a new module there's lots of files to edit rather then being standalone like a module one would be. Quote Link to comment https://forums.phpfreaks.com/topic/154542-how-to-create-a-plugin-system/#findComment-812705 Share on other sites More sharing options...
keeB Posted April 17, 2009 Share Posted April 17, 2009 So it sounds like you just need a way to glue it together -- meaning, have 1 file which maps all of the actions/commands to the other files. You can implement this as a proxy[1] (which I like) .. or as a Flyweight[2], which could also be nice depending on your needs 1: http://en.wikipedia.org/wiki/Proxy_pattern 2: http://en.wikipedia.org/wiki/Flyweight_pattern Quote Link to comment https://forums.phpfreaks.com/topic/154542-how-to-create-a-plugin-system/#findComment-812710 Share on other sites More sharing options...
nuttycoder Posted April 17, 2009 Author Share Posted April 17, 2009 Yes I do I would like to change the way the system works so I could create a standalone module have it in its own folder upload it and activate it like you do in wordpress making adding new modules easier. at present for all modules I add all the needed files for the admin side to add/edit items and have the front end side in another folder update delete functions and javascript functions and then modily the htaccess file and index file so it's quite indepth. Quote Link to comment https://forums.phpfreaks.com/topic/154542-how-to-create-a-plugin-system/#findComment-812713 Share on other sites More sharing options...
nuttycoder Posted April 18, 2009 Author Share Posted April 18, 2009 I've found an article on building a plugin architecture, from a pdf it's a little dated was released in 2006. I have a really simple plugin that should replace the title on the main page ony I am getting this error: <b>Warning</b>: Missing argument 1 for date_append() in <b>E:\tutorials\xampp\htdocs\mod\plugins\datetitle.php</b> on line <b>4</b> Here's the plugin file: datetitle.php <?php add_filter('title', date_append); function date_append($title) { return date('l, M j') . ': ' . $title; } ?> Here's the index file where the plugin should be run using apply_filters <?php include_once('config.php'); ?> <html> <head> <title><?php apply_filters('title', 'Main Page'); ?></title> </head> <body> </body> </html> config file: <?php define(ABSPATH, dirname(__FILE__) . '/'); $plugins = array('datetitle'); include_once('plugins.php'); foreach ($plugins as $plugin) { $plugin_file = ABSPATH . 'plugins/' . $plugin . '.php'; if ($plugin != '' && file_exists($plugin_file)) { include_once($plugin_file); } } ?> and the pulgins.php file: <?php //------------------- Filters ------------------------------------- function add_filter($tag, $function_to_add, $priority = 10, $accept_args=1) { global $filter_table; if ( isset($filter_table[$tag][$priority]) ) { foreach($filter_table[$tag][$priority] as $filter) { if ( $filter['function'] == $function_to_add ) { return false; } } } $filter_table[$tag][$priority][] = array('function'=>$function_to_add, 'accept_args'=>$accept_args); return true; } function remove_filter($tag, $function_to_remove, $priority = 10) { global $filter_table; $toret = false; if ( isset($filter_table[$tag][$priority]) ) { foreach($filter_table[$tag][$priority] as $filter) { if ( $filter['function'] != $function_to_remove ) { $new_function_list[] = $filter; } else { $toret = true; } } $filter_table[$tag][$priority] = $new_function_list; } return $toret; } //------------------- Actions ------------------------------------- function do_action($tag, $arg = '') { global $filter_table; $extra_args = array_slice(func_get_args(), 2); $args = array_merge(array($arg), $extra_args); if ( !isset($filter_table[$tag]) ) { return; } else { ksort($filter_table[$tag]); } foreach ($filter_table[$tag] as $priority => $functions) { if ( !is_null($functions) ) { foreach($functions as $function) { $func_name = $function['function']; $accept_args = $function['accepted_args']; if ( $accept_args == 1 ) { $the_args = array($arg); } elseif ( $accept_args > 1 ) { $the_args = array_slice($args, 0, $accept_args); } elseif ( $accept_args == 0 ) { $the_args = NULL; } else { $the_args = $args; } $str = call_user_func_array($func_name, $the_args); } } } } //------------------- Apply Filters ------------------------------------- function apply_filters($tag, $string) { global $filter_table; $args = array_slice(func_get_args(), 2); if ( !isset($filter_table[$tag]) ) { return $string; } else { ksort($filter_table[$tag]); } foreach ($filter_table[$tag] as $priority => $functions) { if ( !is_null($functions) ) { foreach($functions as $function) { $all_args = array_merge(array($string), $args); $func_name = $function['function']; $accept_args = $function['accepted_args']; if ( $accept_args == 1 ) $the_args = array($string); elseif ( $accept_args > 1 ) $the_args = array_slice($all_args,0, $accept_args); elseif ( $accept_args == 0 ) $the_args = NULL; else $the_args = $all_args; $str = call_user_func_array($func_name, $the_args); } } } return $string; } ?> If anyone can spot what's going wrong that would be a huge help. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/154542-how-to-create-a-plugin-system/#findComment-813079 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.