BasePlugin.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright 2005-2011, Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * Redistributions of files must retain the above copyright notice.
  8. *
  9. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  10. * @link https://cakephp.org CakePHP(tm) Project
  11. * @since 3.6.0
  12. * @license https://opensource.org/licenses/mit-license.php MIT License
  13. */
  14. namespace Cake\Core;
  15. use Cake\Event\EventManagerInterface;
  16. use InvalidArgumentException;
  17. use ReflectionClass;
  18. /**
  19. * Base Plugin Class
  20. *
  21. * Every plugin should extends from this class or implement the interfaces and
  22. * include a plugin class in it's src root folder.
  23. */
  24. class BasePlugin implements PluginInterface
  25. {
  26. /**
  27. * Do bootstrapping or not
  28. *
  29. * @var bool
  30. */
  31. protected $bootstrapEnabled = true;
  32. /**
  33. * Are events enabled.
  34. *
  35. * @var bool
  36. */
  37. protected $eventsEnabled = true;
  38. /**
  39. * Load routes or not
  40. *
  41. * @var bool
  42. */
  43. protected $routesEnabled = true;
  44. /**
  45. * Enable middleware
  46. *
  47. * @var bool
  48. */
  49. protected $middlewareEnabled = true;
  50. /**
  51. * Console middleware
  52. *
  53. * @var bool
  54. */
  55. protected $consoleEnabled = true;
  56. /**
  57. * The path to this plugin.
  58. *
  59. * @var string
  60. */
  61. protected $path;
  62. /**
  63. * The class path for this plugin.
  64. *
  65. * @var string
  66. */
  67. protected $classPath;
  68. /**
  69. * The config path for this plugin.
  70. *
  71. * @var string
  72. */
  73. protected $configPath;
  74. /**
  75. * The name of this plugin
  76. *
  77. * @var string
  78. */
  79. protected $name;
  80. /**
  81. * Constructor
  82. *
  83. * @param array $options Options
  84. */
  85. public function __construct(array $options = [])
  86. {
  87. foreach (static::VALID_HOOKS as $key) {
  88. if (isset($options[$key])) {
  89. $this->{"{$key}Enabled"} = (bool)$options[$key];
  90. }
  91. }
  92. foreach (['name', 'path', 'classPath', 'configPath'] as $path) {
  93. if (isset($options[$path])) {
  94. $this->{$path} = $options[$path];
  95. }
  96. }
  97. $this->initialize();
  98. }
  99. /**
  100. * {@inheritdoc}
  101. */
  102. public function initialize()
  103. {
  104. }
  105. /**
  106. * {@inheritDoc}
  107. */
  108. public function getName()
  109. {
  110. if ($this->name) {
  111. return $this->name;
  112. }
  113. $parts = explode('\\', get_class($this));
  114. array_pop($parts);
  115. $this->name = implode('/', $parts);
  116. return $this->name;
  117. }
  118. /**
  119. * {@inheritDoc}
  120. */
  121. public function getPath()
  122. {
  123. if ($this->path) {
  124. return $this->path;
  125. }
  126. $reflection = new ReflectionClass($this);
  127. $path = dirname($reflection->getFileName());
  128. // Trim off src
  129. if (substr($path, -3) === 'src') {
  130. $path = substr($path, 0, -3);
  131. }
  132. $this->path = rtrim($path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
  133. return $this->path;
  134. }
  135. /**
  136. * {@inheritDoc}
  137. */
  138. public function getConfigPath()
  139. {
  140. if ($this->configPath) {
  141. return $this->configPath;
  142. }
  143. $path = $this->getPath();
  144. return $path . 'config' . DIRECTORY_SEPARATOR;
  145. }
  146. /**
  147. * {@inheritDoc}
  148. */
  149. public function getClassPath()
  150. {
  151. if ($this->classPath) {
  152. return $this->classPath;
  153. }
  154. $path = $this->getPath();
  155. return $path . 'src' . DIRECTORY_SEPARATOR;
  156. }
  157. /**
  158. * {@inheritdoc}
  159. */
  160. public function enable($hook)
  161. {
  162. $this->checkHook($hook);
  163. $this->{"{$hook}Enabled}"} = true;
  164. return $this;
  165. }
  166. /**
  167. * {@inheritdoc}
  168. */
  169. public function disable($hook)
  170. {
  171. $this->checkHook($hook);
  172. $this->{"{$hook}Enabled"} = false;
  173. return $this;
  174. }
  175. /**
  176. * {@inheritdoc}
  177. */
  178. public function isEnabled($hook)
  179. {
  180. $this->checkHook($hook);
  181. return $this->{"{$hook}Enabled"} === true;
  182. }
  183. /**
  184. * Check if a hook name is valid
  185. *
  186. * @param string $hook The hook name to check
  187. * @throws \InvalidArgumentException on invalid hooks
  188. * @return void
  189. */
  190. protected function checkHook($hook)
  191. {
  192. if (!in_array($hook, static::VALID_HOOKS)) {
  193. throw new InvalidArgumentException(
  194. "`$hook` is not a valid hook name. Must be one of " . implode(', ', static::VALID_HOOKS)
  195. );
  196. }
  197. }
  198. /**
  199. * {@inheritdoc}
  200. */
  201. public function routes($routes)
  202. {
  203. $path = $this->getConfigPath() . DS . 'routes.php';
  204. if (file_exists($path)) {
  205. require $path;
  206. }
  207. }
  208. /**
  209. * {@inheritdoc}
  210. */
  211. public function bootstrap(PluginApplicationInterface $app)
  212. {
  213. $bootstrap = $this->getConfigPath() . DS . 'bootstrap.php';
  214. if (file_exists($bootstrap)) {
  215. require $bootstrap;
  216. }
  217. }
  218. /**
  219. * {@inheritdoc}
  220. */
  221. public function console($commands)
  222. {
  223. return $commands;
  224. }
  225. /**
  226. * {@inheritdoc}
  227. */
  228. public function middleware($middleware)
  229. {
  230. return $middleware;
  231. }
  232. }