: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright 2005-2011, Cake Software Foundation, Inc. (https://cakefoundation.org)
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @link https://cakephp.org CakePHP(tm) Project
* @license https://opensource.org/licenses/mit-license.php MIT License
use InvalidArgumentException;
* Every plugin should extends from this class or implement the interfaces and
* include a plugin class in it's src root folder.
class BasePlugin implements PluginInterface
* Do bootstrapping or not
protected $bootstrapEnabled = true;
protected $routesEnabled = true;
protected $middlewareEnabled = true;
protected $consoleEnabled = true;
* The path to this plugin.
* The class path for this plugin.
* The config path for this plugin.
* The name of this plugin
* @param array $options Options
public function __construct(array $options = [])
foreach (static::VALID_HOOKS as $key) {
if (isset($options[$key])) {
$this->{"{$key}Enabled"} = (bool)$options[$key];
foreach (['name', 'path', 'classPath', 'configPath'] as $path) {
if (isset($options[$path])) {
$this->{$path} = $options[$path];
public function initialize()
public function getName()
$parts = explode('\\', get_class($this));
$this->name = implode('/', $parts);
public function getPath()
$reflection = new ReflectionClass($this);
$path = dirname($reflection->getFileName());
if (substr($path, -3) === 'src') {
$path = substr($path, 0, -3);
$this->path = rtrim($path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
public function getConfigPath()
return $this->configPath;
$path = $this->getPath();
return $path . 'config' . DIRECTORY_SEPARATOR;
public function getClassPath()
$path = $this->getPath();
return $path . 'src' . DIRECTORY_SEPARATOR;
public function enable($hook)
$this->{"{$hook}Enabled}"} = true;
public function disable($hook)
$this->{"{$hook}Enabled"} = false;
public function isEnabled($hook)
return $this->{"{$hook}Enabled"} === true;
* Check if a hook name is valid
* @param string $hook The hook name to check
* @throws \InvalidArgumentException on invalid hooks
protected function checkHook($hook)
if (!in_array($hook, static::VALID_HOOKS)) {
throw new InvalidArgumentException(
"`$hook` is not a valid hook name. Must be one of " . implode(', ', static::VALID_HOOKS)
public function routes($routes)
$path = $this->getConfigPath() . 'routes.php';
if (file_exists($path)) {
public function bootstrap(PluginApplicationInterface $app)
$bootstrap = $this->getConfigPath() . 'bootstrap.php';
if (file_exists($bootstrap)) {
public function console($commands)
return $commands->addMany($commands->discoverPlugin($this->getName()));
public function middleware($middleware)