BasePlugin.php 6.6 KB

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