Source for file PluginManager.php

Documentation is available at PluginManager.php

  1. <?php
  2. /**
  3.  * $Source: /cvsroot/svggrapher/svggrapher/mains/PluginManager.php,v $
  4.  *
  5.  * PluginManager
  6.  *
  7.  * @version CVS: $Id: PluginManager.php,v 1.4 2007/10/23 09:22:53 geelweb Exp $
  8.  * @filesource
  9.  * @author Guillaume Luchet. <guillaume@geelweb.org>
  10.  * @copyright Copyright (c) 2007, Guillaume Luchet.
  11.  * @license http://opensource.org/licenses/bsd-license.php BSD License
  12.  * @package SvgGrapher
  13.  */
  14.  
  15. // const {{{
  16.  
  17. /**
  18.  * Path of the dataproviders plugins
  19.  */
  20. if(!defined('SVGGRAPHER_PLUGINS_DATAPROVIDER')) {
  21.     define('SVGGRAPHER_PLUGINS_DATAPROVIDER'SVGGRAPHER_CLASS_PATH '/dataproviders');
  22. }
  23.  
  24. /**
  25.  * Path of the defs plugins
  26.  */
  27. if(!defined('SVGGRAPHER_PLUGINS_DEFS')) {
  28.     define('SVGGRAPHER_PLUGINS_DEFS'SVGGRAPHER_CLASS_PATH '/defs');
  29. }
  30.  
  31. /**
  32.  * Path of the filters plugins
  33.  */
  34. if(!defined('SVGGRAPHER_PLUGINS_FILTER')) {
  35.     define('SVGGRAPHER_PLUGINS_FILTER'SVGGRAPHER_CLASS_PATH '/defs');
  36. }
  37.  
  38. /**
  39.  * Path of the markers plugins
  40.  */
  41. if(!defined('SVGGRAPHER_PLUGINS_MARKER')) {
  42.     define('SVGGRAPHER_PLUGINS_MARKER'SVGGRAPHER_CLASS_PATH '/defs');
  43. }
  44.  
  45. /**
  46.  * Path of the scripts plugins
  47.  */
  48. if(!defined('SVGGRAPHER_PLUGINS_SCRIPT')) {
  49.     define('SVGGRAPHER_PLUGINS_SCRIPT'SVGGRAPHER_CLASS_PATH '/scripts');
  50. }
  51.  
  52. /**
  53.  * Path of the visuals plugins
  54.  */
  55. if(!defined('SVGGRAPHER_PLUGINS_VISUAL')) {
  56.     define('SVGGRAPHER_PLUGINS_VISUAL'SVGGRAPHER_CLASS_PATH '/visuals');
  57. }
  58.  
  59. // }}}
  60. // doc {{{
  61.  
  62. /**
  63.  * PluginManager
  64.  * 
  65.  * This class is composed of static method to manage the SvgGrapher plugins. The
  66.  * methods are:
  67.  * - bool incl(string, string) inlude a plugin file
  68.  * - string exist(string, string) check if a plugin exist
  69.  * - mixed execute(string, string [, $args]) execute a plugin function
  70.  *
  71.  * The two first parameters are always the plugin type (visual, dataprovider, etc) and
  72.  * the plugin name.
  73.  * 
  74.  * @version $Id: PluginManager.php,v 1.4 2007/10/23 09:22:53 geelweb Exp $
  75.  * @copyright Copyright (c) 2007, Guillaume Luchet
  76.  * @author guillaume luchet <guillaume@geelweb.org>
  77.  * @license http://opensource.org/licenses/bsd-license.php BSD License
  78.  * @package SvgGrapher
  79.  */ // }}}
  80. class PluginManager 
  81. {
  82.     // Properties {{{
  83.  
  84.     /**
  85.      * pluginsPaths
  86.      *
  87.      * Array of paths for plugins types.
  88.      * 
  89.      * @static
  90.      * @var array 
  91.      * @access public
  92.      */
  93.     static $pluginsPaths array(
  94.         'dataprovider' => SVGGRAPHER_PLUGINS_DATAPROVIDER,
  95.         'defs'         => SVGGRAPHER_PLUGINS_DEFS,
  96.         'filter'       => SVGGRAPHER_PLUGINS_FILTER,
  97.         'marker'       => SVGGRAPHER_PLUGINS_MARKER,
  98.         'script'       => SVGGRAPHER_PLUGINS_SCRIPT,
  99.         'visual'       => SVGGRAPHER_PLUGINS_VISUAL);
  100.  
  101.     /**
  102.      * incl
  103.      *
  104.      * Array of plugins names already include for each plugins types.
  105.      * 
  106.      * @static
  107.      * @var array 
  108.      * @access public
  109.      */
  110.     static $incl array();
  111.  
  112.     // }}}
  113.     // PluginManager::incl() {{{
  114.  
  115.     /**
  116.      * incl
  117.      *
  118.      * try to include a plugin file, return true if success or if the plugin is
  119.      * already been included. Throw an exception if the plugin is not found.
  120.      * 
  121.      * @param string $type plugin type
  122.      * @param string $name plugin name
  123.      * @static
  124.      * @access public
  125.      * @return boolean 
  126.      */
  127.     public static function incl($type$name
  128.     {
  129.         if(isset(self::$incl[$type]&& in_array($nameself::$incl[$type])) {
  130.             return true;
  131.         }
  132.         $pluginPath self::$pluginsPaths[$type];
  133.         $pluginName 'svgGrapher.' $type '.' $name;
  134.         $pluginFile $pluginName '.php';
  135.  
  136.         if(!file_exists($pluginPath '/' $pluginFile)) {
  137.             throw new Exception(sprintf('%s plugin (%s) not found.',
  138.                 $type$pluginName));
  139.         }
  140.  
  141.         include_once $pluginPath '/' $pluginFile;
  142.         self::$incl[$type][$name;
  143.         return true;
  144.     }
  145.  
  146.     // }}}
  147.     // PluginManager::exist() {{{
  148.  
  149.     /**
  150.      * exist
  151.      *
  152.      * check if a plugin function exist, return the plugin function name if
  153.      * exist or throw an exception.
  154.      * 
  155.      * @param mixed $type plugin type
  156.      * @param mixed $name plugin name
  157.      * @static
  158.      * @access public
  159.      * @return string 
  160.      */
  161.     public static function exist($type$name
  162.     {
  163.         self::incl($type$name);
  164.         $pluginFx 'svgGrapher_' $type '_' $name;
  165.         if(!function_exists($pluginFx)) {
  166.             throw new Exception(sprintf('%s function (%s) not found',
  167.                 $type$pluginFx));
  168.         }
  169.  
  170.         return $pluginFx;
  171.     }
  172.  
  173.     // }}}
  174.     // PluginManager::execute() {{{
  175.  
  176.     /**
  177.      * execute
  178.      *
  179.      * Try to execute a plugin function, and return his result. Throw an
  180.      * exception if the plugin file or function can not be found.
  181.      * 
  182.      * @param string $type plugin type
  183.      * @param string $name plugin name
  184.      * @param mixed $args params to pass at the function
  185.      * @static
  186.      * @access public
  187.      * @return mixed 
  188.      */
  189.     public static function execute($type$name$args=false
  190.     {
  191.         $result true;
  192.         try {
  193.             $pluginFx self::exist($type$name);
  194.             if($args{
  195.                 $result call_user_func($pluginFx$args);
  196.             else {
  197.                 $result call_user_func($pluginFx);
  198.             }
  199.         catch(Exception $e{
  200.             throw new Exception($e->getMessage());
  201.         }
  202.         return $result;
  203.     }
  204.  
  205.     // }}}
  206. }
  207.  
  208. ?>

Documentation generated on Tue, 23 Oct 2007 11:32:05 +0200 by phpDocumentor 1.4.0