BasePlugin.php 5.2 KB

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