PluginManager.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. /**
  3. * This file is part of amfPHP
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the license that is bundled
  8. * with this package in the file license.txt.
  9. */
  10. /**
  11. * Loads plugins for Amfphp. Plugins consist of a folder in the plugins folder. The folder and the class
  12. * should all have the same name. The file containing the class should be named with the class name with the '.php' suffix added.
  13. * It is the loaded class' responsability to load any other resources that the plugin needs from the same folder.
  14. * A plugin interacts with Amfphp by using the Amfphp_Core_FilterManager to register its functions
  15. * to be called at specific times with specific parameters during execution.
  16. * It's a singleton, so use getInstance
  17. *
  18. * @package Amfphp_Core
  19. * @author Ariel Sommeria-Klein
  20. */
  21. class Amfphp_Core_PluginManager {
  22. /**
  23. * protected instance of singleton
  24. * @var Amfphp_Core_PluginManager
  25. *
  26. */
  27. protected static $instance = NULL;
  28. /**
  29. * plugin instances
  30. * @var array
  31. */
  32. protected $pluginInstances;
  33. /**
  34. * constructor
  35. */
  36. protected function __construct() {
  37. $this->pluginInstances = array();
  38. }
  39. /**
  40. * gives access to the singleton
  41. * @return Amfphp_Core_PluginManager
  42. */
  43. public static function getInstance() {
  44. if (self::$instance == NULL) {
  45. self::$instance = new Amfphp_Core_PluginManager();
  46. }
  47. return self::$instance;
  48. }
  49. /**
  50. * load the plugins
  51. * @param array $pluginFolders where to load the plugins from. Absolute paths. For example Amfphp/Plugins/
  52. * @param array $pluginsConfig optional. an array containing the plugin configuration, using the plugin name as key.
  53. * @param array $sharedConfig optional. if both a specific config and a shared config are available, concatenate them to create the plugin config.
  54. * Otherwise use whatever is not null
  55. * @param array $disabledPlugins optional. an array of names of plugins to disable
  56. */
  57. public function loadPlugins($pluginFolders, array $pluginsConfig = null, array $sharedConfig = null, array $disabledPlugins = null) {
  58. foreach ($pluginFolders as $pluginsFolderRootPath) {
  59. if (!is_dir($pluginsFolderRootPath)) {
  60. throw new Amfphp_Core_Exception('invalid path for loading plugins at ' . $pluginsFolderRootPath);
  61. }
  62. $folderContent = scandir($pluginsFolderRootPath);
  63. foreach ($folderContent as $pluginName) {
  64. if (!is_dir($pluginsFolderRootPath . '/' . $pluginName)) {
  65. continue;
  66. }
  67. //avoid system folders
  68. if ($pluginName[0] == '.') {
  69. continue;
  70. }
  71. //check first if plugin is disabled
  72. $shouldLoadPlugin = true;
  73. if ($disabledPlugins) {
  74. foreach ($disabledPlugins as $disabledPlugin) {
  75. if ($disabledPlugin == $pluginName) {
  76. $shouldLoadPlugin = false;
  77. }
  78. }
  79. }
  80. if (!$shouldLoadPlugin) {
  81. continue;
  82. }
  83. if (!class_exists($pluginName, false)) {
  84. require_once $pluginsFolderRootPath . '/' . $pluginName . '/' . $pluginName . '.php';
  85. }
  86. $pluginConfig = array();
  87. if ($pluginsConfig && isset($pluginsConfig[$pluginName])) {
  88. $pluginConfig = $pluginsConfig[$pluginName];
  89. }
  90. if ($sharedConfig) {
  91. $pluginConfig += $sharedConfig;
  92. }
  93. $pluginInstance = new $pluginName($pluginConfig);
  94. $this->pluginInstances[] = $pluginInstance;
  95. }
  96. }
  97. }
  98. }
  99. ?>