/source/includes/classes/plugin.php

http://prosporous.googlecode.com/ · PHP · 75 lines · 45 code · 1 blank · 29 comment · 7 complexity · 55c84af0ee8c91e1fab0dc3395340de2 MD5 · raw file

  1. <?php
  2. /*
  3. * edite in NetBeans of IDE at version 7.0.1.
  4. */
  5. /**
  6. * ??????
  7. * ????????????
  8. *
  9. * @author Zuolong Wang
  10. */
  11. class Plugin {
  12. private static $_pluginDir = 'Plug-in';
  13. private static $_instance = null;
  14. private static $_plugins = array();
  15. /**
  16. *?????
  17. * 2011-11-22 ????? '.php'
  18. * @var static
  19. */
  20. private static $_ext = '.php';
  21. private function __construct() {
  22. //???
  23. defined('BIC_ROOT') || define('BIC_ROOT',substr(dirname(__FILE__),0,-16));
  24. define('DS',DIRECTORY_SEPARATOR);
  25. $this -> _pluginPath = BIC_ROOT . self :: $_pluginDir;
  26. }
  27. public static function init(){
  28. if (is_null(self :: $_instance)){
  29. self :: $_instance = new self();
  30. }
  31. return self :: $_instance;
  32. }
  33. public function add($hook,$pluginName){
  34. self :: $_plugins[$hook][] = $pluginName;
  35. }
  36. /**
  37. *????hook???????,???????
  38. * @param type $hook
  39. * @return type
  40. */
  41. public function trigger($hook){
  42. if (!isset(self :: $_plugins[$hook])) return;
  43. foreach (self :: $_plugins[$hook] as $plugin){
  44. //????
  45. if (is_dir($this -> _pluginPath . $plugin)){
  46. $plugin_file = $this -> _pluginPath . $plugin . DS . $plugin . self :: $_ext;
  47. }else{
  48. $plugin_file = $this -> _pluginPath . DS . $plugin . self :: $_ext;
  49. }
  50. //?????
  51. if (is_string($plugin) && preg_match("/^[\w\-\/]+/mi",$plugin) && is_file($plugin_file)){
  52. require $plugin_file;
  53. /**
  54. * ???trigger()??????????????doAction()???????
  55. */
  56. $params = array_slice(func_get_args(),1);
  57. call_user_func_array(array(new $plugin(),'doAction'),$params);
  58. }else{
  59. self :: log('???????????????? ?'.$plugin_file);
  60. }
  61. }
  62. }
  63. /**
  64. *??????
  65. * @param type $msg
  66. */
  67. private static function log($msg){
  68. echo $msg;
  69. }
  70. }
  71. interface IPlugin{
  72. function doAction();
  73. }
  74. ?>