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