1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14:
15: namespace Cake\Http;
16:
17: use Cake\Core\App;
18: use Cake\Core\BasePlugin;
19: use Cake\Core\ConsoleApplicationInterface;
20: use Cake\Core\HttpApplicationInterface;
21: use Cake\Core\Plugin;
22: use Cake\Core\PluginApplicationInterface;
23: use Cake\Core\PluginInterface;
24: use Cake\Event\EventDispatcherTrait;
25: use Cake\Event\EventManager;
26: use Cake\Event\EventManagerInterface;
27: use Cake\Routing\DispatcherFactory;
28: use Cake\Routing\Router;
29: use InvalidArgumentException;
30: use Psr\Http\Message\ResponseInterface;
31: use Psr\Http\Message\ServerRequestInterface;
32:
33: 34: 35: 36: 37: 38: 39:
40: abstract class BaseApplication implements
41: ConsoleApplicationInterface,
42: HttpApplicationInterface,
43: PluginApplicationInterface
44: {
45:
46: use EventDispatcherTrait;
47:
48: 49: 50:
51: protected $configDir;
52:
53: 54: 55: 56: 57:
58: protected $plugins;
59:
60: 61: 62: 63: 64: 65:
66: public function __construct($configDir, EventManagerInterface $eventManager = null)
67: {
68: $this->configDir = $configDir;
69: $this->plugins = Plugin::getCollection();
70: $this->_eventManager = $eventManager ?: EventManager::instance();
71: }
72:
73: 74: 75: 76:
77: abstract public function middleware($middleware);
78:
79: 80: 81:
82: public function pluginMiddleware($middleware)
83: {
84: foreach ($this->plugins->with('middleware') as $plugin) {
85: $middleware = $plugin->middleware($middleware);
86: }
87:
88: return $middleware;
89: }
90:
91: 92: 93:
94: public function addPlugin($name, array $config = [])
95: {
96: if (is_string($name)) {
97: $plugin = $this->makePlugin($name, $config);
98: } else {
99: $plugin = $name;
100: }
101: if (!$plugin instanceof PluginInterface) {
102: throw new InvalidArgumentException(sprintf(
103: "The `%s` plugin does not implement Cake\Core\PluginInterface.",
104: get_class($plugin)
105: ));
106: }
107: $this->plugins->add($plugin);
108:
109: return $this;
110: }
111:
112: 113: 114: 115: 116:
117: public function getPlugins()
118: {
119: return $this->plugins;
120: }
121:
122: 123: 124: 125: 126: 127: 128:
129: protected function makePlugin($name, array $config)
130: {
131: $className = $name;
132: if (strpos($className, '\\') === false) {
133: $className = str_replace('/', '\\', $className) . '\\' . 'Plugin';
134: }
135: if (class_exists($className)) {
136: return new $className($config);
137: }
138:
139: if (!isset($config['path'])) {
140: $config['path'] = $this->plugins->findPath($name);
141: }
142: $config['name'] = $name;
143:
144: return new BasePlugin($config);
145: }
146:
147: 148: 149:
150: public function bootstrap()
151: {
152: require_once $this->configDir . '/bootstrap.php';
153: }
154:
155: 156: 157:
158: public function pluginBootstrap()
159: {
160: foreach ($this->plugins->with('bootstrap') as $plugin) {
161: $plugin->bootstrap($this);
162: }
163: }
164:
165: 166: 167: 168: 169: 170: 171: 172:
173: public function routes($routes)
174: {
175: if (!Router::$initialized) {
176:
177: Router::$initialized = true;
178:
179: require $this->configDir . '/routes.php';
180: }
181: }
182:
183: 184: 185:
186: public function pluginRoutes($routes)
187: {
188: foreach ($this->plugins->with('routes') as $plugin) {
189: $plugin->routes($routes);
190: }
191:
192: return $routes;
193: }
194:
195: 196: 197: 198: 199: 200: 201: 202: 203:
204: public function console($commands)
205: {
206: return $commands->addMany($commands->autoDiscover());
207: }
208:
209: 210: 211:
212: public function pluginConsole($commands)
213: {
214: foreach ($this->plugins->with('console') as $plugin) {
215: $commands = $plugin->console($commands);
216: }
217:
218: return $commands;
219: }
220:
221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232:
233: public function __invoke(ServerRequestInterface $request, ResponseInterface $response, $next)
234: {
235: return $this->getDispatcher()->dispatch($request, $response);
236: }
237:
238: 239: 240: 241: 242:
243: protected function getDispatcher()
244: {
245: return new ActionDispatcher(null, $this->getEventManager(), DispatcherFactory::filters());
246: }
247: }
248: