/
BaseApplication.php
257 lines (230 loc) · 6.96 KB
/
BaseApplication.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
<?php
/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* 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
* @since 3.3.0
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Http;
use Cake\Core\ConsoleApplicationInterface;
use Cake\Core\HttpApplicationInterface;
use Cake\Core\Plugin;
use Cake\Core\PluginApplicationInterface;
use Cake\Core\PluginInterface;
use Cake\Event\EventApplicationInterface;
use Cake\Event\EventDispatcherInterface;
use Cake\Event\EventDispatcherTrait;
use Cake\Event\EventManager;
use Cake\Event\EventManagerInterface;
use Cake\Routing\DispatcherFactory;
use Cake\Routing\Router;
use InvalidArgumentException;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
/**
* Base class for application classes.
*
* The application class is responsible for bootstrapping the application,
* and ensuring that middleware is attached. It is also invoked as the last piece
* of middleware, and delegates request/response handling to the correct controller.
*/
abstract class BaseApplication implements
ConsoleApplicationInterface,
EventApplicationInterface,
EventDispatcherInterface,
HttpApplicationInterface,
PluginApplicationInterface
{
use EventDispatcherTrait;
/**
* @var string Contains the path of the config directory
*/
protected $configDir;
/**
* Plugin Collection
*
* @var \Cake\Core\PluginCollection
*/
protected $plugins;
/**
* Constructor
*
* @param string $configDir The directory the bootstrap configuration is held in.
* @param \Cake\Event\EventManagerInterface $eventManager Application event manager instance.
*/
public function __construct($configDir, EventManagerInterface $eventManager = null)
{
$this->configDir = $configDir;
$this->plugins = Plugin::getCollection();
$this->_eventManager = $eventManager ?: EventManager::instance();
}
/**
* @param \Cake\Http\MiddlewareQueue $middleware The middleware queue to set in your App Class
* @return \Cake\Http\MiddlewareQueue
*/
abstract public function middleware($middleware);
/**
* {@inheritDoc}
*/
public function pluginEvents()
{
$events = $this->getEventManager();
foreach ($this->plugins->with('events') as $plugin) {
$plugin->events($events);
}
}
/**
* {@inheritDoc}
*/
public function pluginMiddleware($middleware)
{
foreach ($this->plugins->with('middleware') as $plugin) {
$middleware = $plugin->middleware($middleware);
}
return $middleware;
}
/**
* {@inheritDoc}
*/
public function addPlugin($name, array $config = [])
{
if (is_string($name)) {
$plugin = $this->makePlugin($name, $config);
} else {
$plugin = $name;
}
$this->plugins->add($plugin);
return $this;
}
/**
* Get the plugin collection in use.
*
* @return \Cake\Core\PluginCollection
*/
public function getPlugins()
{
return $this->plugins;
}
/**
* Create a plugin instance from a classname and configuration
*
* @param string $name The plugin classname
* @param array $config Configuration options for the plugin
* @return \Cake\Core\PluginInterface
*/
public function makePlugin($name, array $config)
{
if (!class_exists($name)) {
throw new InvalidArgumentException("The `{$name}` plugin cannot be found");
}
$plugin = new $name($config);
if (!$plugin instanceof PluginInterface) {
throw new InvalidArgumentException("The `{$name}` plugin does not implement Cake\Core\PluginInterface.");
}
return $plugin;
}
/**
* {@inheritDoc}
*/
public function bootstrap()
{
require_once $this->configDir . '/bootstrap.php';
}
/**
* {@inheritDoc}
*/
public function pluginBootstrap()
{
foreach ($this->plugins->with('bootstrap') as $plugin) {
$plugin->bootstrap();
}
}
/**
* {@inheritDoc}
*
* By default this will load `config/routes.php` for ease of use and backwards compatibility.
*
* @param \Cake\Routing\RouteBuilder $routes A route builder to add routes into.
* @return void
*/
public function routes($routes)
{
if (!Router::$initialized) {
require $this->configDir . '/routes.php';
// Prevent routes from being loaded again
Router::$initialized = true;
}
}
/**
* {@inheritDoc}
*/
public function pluginRoutes($routes)
{
foreach ($this->plugins->with('routes') as $plugin) {
$plugin->routes($routes);
}
return $routes;
}
/**
* Define the console commands for an application.
*
* By default all commands in CakePHP, plugins and the application will be
* loaded using conventions based names.
*
* @param \Cake\Console\CommandCollection $commands The CommandCollection to add commands into.
* @return \Cake\Console\CommandCollection The updated collection.
*/
public function console($commands)
{
return $commands->addMany($commands->autoDiscover());
}
/**
* {@inheritDoc}
*/
public function pluginConsole($commands)
{
foreach ($this->plugins->with('console') as $plugin) {
$commands = $plugin->console($commands);
}
return $commands;
}
/**
* {@inheritDoc}
*/
public function events(EventManagerInterface $eventManager)
{
return $eventManager;
}
/**
* Invoke the application.
*
* - Convert the PSR response into CakePHP equivalents.
* - Create the controller that will handle this request.
* - Invoke the controller.
*
* @param \Psr\Http\Message\ServerRequestInterface $request The request
* @param \Psr\Http\Message\ResponseInterface $response The response
* @param callable $next The next middleware
* @return \Psr\Http\Message\ResponseInterface
*/
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, $next)
{
return $this->getDispatcher()->dispatch($request, $response);
}
/**
* Get the ActionDispatcher.
*
* @return \Cake\Http\ActionDispatcher
*/
protected function getDispatcher()
{
return new ActionDispatcher(null, $this->getEventManager(), DispatcherFactory::filters());
}
}