1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13:
14: namespace Cake\Core;
15:
16: use InvalidArgumentException;
17: use ReflectionClass;
18:
19: 20: 21: 22: 23: 24:
25: class BasePlugin implements PluginInterface
26: {
27: 28: 29: 30: 31:
32: protected $bootstrapEnabled = true;
33:
34: 35: 36: 37: 38:
39: protected $routesEnabled = true;
40:
41: 42: 43: 44: 45:
46: protected $middlewareEnabled = true;
47:
48: 49: 50: 51: 52:
53: protected $consoleEnabled = true;
54:
55: 56: 57: 58: 59:
60: protected $path;
61:
62: 63: 64: 65: 66:
67: protected $classPath;
68:
69: 70: 71: 72: 73:
74: protected $configPath;
75:
76: 77: 78: 79: 80:
81: protected $name;
82:
83: 84: 85: 86: 87:
88: public function __construct(array $options = [])
89: {
90: foreach (static::VALID_HOOKS as $key) {
91: if (isset($options[$key])) {
92: $this->{"{$key}Enabled"} = (bool)$options[$key];
93: }
94: }
95: foreach (['name', 'path', 'classPath', 'configPath'] as $path) {
96: if (isset($options[$path])) {
97: $this->{$path} = $options[$path];
98: }
99: }
100:
101: $this->initialize();
102: }
103:
104: 105: 106:
107: public function initialize()
108: {
109: }
110:
111: 112: 113:
114: public function getName()
115: {
116: if ($this->name) {
117: return $this->name;
118: }
119: $parts = explode('\\', get_class($this));
120: array_pop($parts);
121: $this->name = implode('/', $parts);
122:
123: return $this->name;
124: }
125:
126: 127: 128:
129: public function getPath()
130: {
131: if ($this->path) {
132: return $this->path;
133: }
134: $reflection = new ReflectionClass($this);
135: $path = dirname($reflection->getFileName());
136:
137:
138: if (substr($path, -3) === 'src') {
139: $path = substr($path, 0, -3);
140: }
141: $this->path = rtrim($path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
142:
143: return $this->path;
144: }
145:
146: 147: 148:
149: public function getConfigPath()
150: {
151: if ($this->configPath) {
152: return $this->configPath;
153: }
154: $path = $this->getPath();
155:
156: return $path . 'config' . DIRECTORY_SEPARATOR;
157: }
158:
159: 160: 161:
162: public function getClassPath()
163: {
164: if ($this->classPath) {
165: return $this->classPath;
166: }
167: $path = $this->getPath();
168:
169: return $path . 'src' . DIRECTORY_SEPARATOR;
170: }
171:
172: 173: 174:
175: public function enable($hook)
176: {
177: $this->checkHook($hook);
178: $this->{"{$hook}Enabled}"} = true;
179:
180: return $this;
181: }
182:
183: 184: 185:
186: public function disable($hook)
187: {
188: $this->checkHook($hook);
189: $this->{"{$hook}Enabled"} = false;
190:
191: return $this;
192: }
193:
194: 195: 196:
197: public function isEnabled($hook)
198: {
199: $this->checkHook($hook);
200:
201: return $this->{"{$hook}Enabled"} === true;
202: }
203:
204: 205: 206: 207: 208: 209: 210:
211: protected function checkHook($hook)
212: {
213: if (!in_array($hook, static::VALID_HOOKS)) {
214: throw new InvalidArgumentException(
215: "`$hook` is not a valid hook name. Must be one of " . implode(', ', static::VALID_HOOKS)
216: );
217: }
218: }
219:
220: 221: 222:
223: public function routes($routes)
224: {
225: $path = $this->getConfigPath() . 'routes.php';
226: if (file_exists($path)) {
227: require $path;
228: }
229: }
230:
231: 232: 233:
234: public function bootstrap(PluginApplicationInterface $app)
235: {
236: $bootstrap = $this->getConfigPath() . 'bootstrap.php';
237: if (file_exists($bootstrap)) {
238: require $bootstrap;
239: }
240: }
241:
242: 243: 244:
245: public function console($commands)
246: {
247: return $commands->addMany($commands->discoverPlugin($this->getName()));
248: }
249:
250: 251: 252:
253: public function middleware($middleware)
254: {
255: return $middleware;
256: }
257: }
258: