PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/application/models/PluginManager.class.php

https://github.com/cj/Project-Pier
PHP | 183 lines | 112 code | 23 blank | 48 comment | 29 complexity | b214d793bf92c1b714a56b7499bbcc9f MD5 | raw file
  1. <?php
  2. /**
  3. * PluginManager
  4. *
  5. * This file contains logic that has been borrowed from my favourite
  6. * publishing platform; WordPress
  7. * The implementation is only a part reproduction and has been modified
  8. * to suit the ProjectPier application.
  9. * @author Mark Brennand
  10. * @link http://www.activeingredient.com.au
  11. *
  12. * The plugin architecture supports both actions and filters. The difference
  13. * between these is a matter of input; all actions on the same hook receive
  14. * the same input regardless of order, filters received modified input from
  15. * previous filters on the same hook.
  16. *
  17. * @see application/plugins.php
  18. *
  19. * @version 1.0
  20. * @http://www.projectpier.org/
  21. */
  22. class PluginManager {
  23. /**
  24. * List of filters and actions
  25. *
  26. * @var array
  27. */
  28. var $filter_table;
  29. var $included;
  30. function init() {
  31. if (isset($this) && ($this instanceof PluginManager)) {
  32. $this->included = array();
  33. $this->filter_table = array();
  34. $activated_plugins = Plugins::getActivatedPlugins();
  35. // now load each plugin
  36. foreach(array_keys($activated_plugins) as $name) {
  37. include_once 'plugins/'.$name.'/init.php';
  38. } // foreach
  39. // TODO : cleanup up old activated plugins without valid file??
  40. } else {
  41. PluginManager::instance()->init();
  42. } // if
  43. } // init
  44. function add_filter($tag, $function_to_add, $priority = 10, $accepted_args = 1) {
  45. if ( isset($this->filter_table[$tag][$priority]) ) {
  46. foreach($this->filter_table[$tag][$priority] as $filter) {
  47. if ( $filter['function'] == $function_to_add ) {
  48. return false;
  49. } // if
  50. } // foreach
  51. } // if
  52. $this->filter_table[$tag][$priority][] = array('function'=>$function_to_add,
  53. 'accepted_args'=>$accepted_args);
  54. return true;
  55. } // add_filter
  56. function remove_filter($tag, $function_to_remove, $priority = 10) {
  57. $toret = false;
  58. if ( isset($this->filter_table[$tag][$priority]) ) {
  59. foreach($this->filter_table[$tag][$priority] as $filter) {
  60. if ( $filter['function'] != $function_to_remove ) {
  61. $new_function_list[] = $filter;
  62. } else {
  63. $toret = true;
  64. } // if
  65. } // foreach
  66. $this->filter_table[$tag][$priority] = $new_function_list;
  67. } // if
  68. return $toret;
  69. } // remove_filter
  70. function do_action($tag,$arg='') {
  71. if ( !isset($this->filter_table[$tag]) ) {
  72. return;
  73. } else {
  74. ksort($this->filter_table[$tag]);
  75. } // if
  76. $args = array();
  77. if ( is_array($arg) && 1 == count($arg) && is_object($arg[0]) ) {
  78. $args[] =& $arg[0];
  79. } else {
  80. $args[] = $arg;
  81. } // if
  82. for ( $a = 2; $a < func_num_args(); $a++ ) {
  83. $args[] = func_get_arg($a);
  84. } // for
  85. foreach ($this->filter_table[$tag] as $priority => $functions) {
  86. if ( !is_null($functions) ) {
  87. foreach($functions as $f) {
  88. trace(__FILE__,"call_user_func_array({$f['function']},..");
  89. call_user_func_array($f['function'], array_slice($args, 0, (int)$f['accepted_args']));
  90. } // foreach
  91. } // if
  92. } // foreach
  93. } // do_action
  94. function apply_filters($tag,$value) {
  95. $args = func_get_args();
  96. if ( !isset($this->filter_table[$tag]) ) {
  97. return $value;
  98. } else {
  99. ksort($this->filter_table[$tag]);
  100. } // if
  101. foreach ($this->filter_table[$tag] as $priority => $functions) {
  102. if ( !is_null($functions) ) {
  103. foreach($functions as $f) {
  104. $args[1] = $value;
  105. $value = call_user_func_array($f['function'], array_slice($args, 1,(int)$f['accepted_args']));
  106. } // foreach
  107. } // if
  108. } // foreach
  109. return $value;
  110. } // apply_filters
  111. function useHelper($plugin_name, $helper) {
  112. //$helper_file = APPLICATION_PATH . "/plugins/$plugin_name/helpers/$helper.php";
  113. $helper_file = "plugins/$plugin_name/helpers/$helper.php";
  114. // If we have it include, else throw exception
  115. if (is_file($helper_file)) {
  116. include_once $helper_file;
  117. return true;
  118. } else {
  119. throw new FileDnxError($helper_file, "Helper '$helper' for plugin '$plugin_name' does not exists (expected location '$helper_file')");
  120. } // if
  121. } // useHelper
  122. /**
  123. * Use specific library. This function will look in plugin directory.
  124. * If it doesn't find requested library class, then LibraryDnxError
  125. * will be raised
  126. *
  127. * @access public
  128. * @param string $library Library name
  129. * @return null
  130. * @throws LibraryDnxError
  131. */
  132. static function useLibrary($plugin_name, $library) {
  133. //$library_path = APPLICATION_PATH . "/plugins/$plugin_name/library/$library/";
  134. $library_path = "plugins/$plugin_name/library/$library/";
  135. if (file_exists($library_path) && is_dir($library_path)) {
  136. // Call init library file if it exists
  137. $library_init_file = $library_path . $library . '.php';
  138. if (is_file($library_init_file)) {
  139. include_once $library_init_file;
  140. } // if
  141. return true;
  142. } // if
  143. throw new LibraryDnxError($library);
  144. } // useLibrary
  145. /**
  146. * Return single PluginManager instance
  147. *
  148. * @access public
  149. * @param void
  150. * @return PluginManager
  151. */
  152. static function instance() {
  153. static $instance;
  154. if (!($instance instanceof PluginManager )) {
  155. $instance = new PluginManager();
  156. } // if
  157. return $instance;
  158. } // instance
  159. }
  160. ?>