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