1 | <?php |
||
20 | class Application extends Container implements HttpKernelInterface |
||
21 | { |
||
22 | const ENV_PRODUCTION = 'production'; |
||
23 | const ENV_DEVELOPMENT = 'development'; |
||
24 | const ENV_TEST = 'test'; |
||
25 | |||
26 | /** |
||
27 | * @staticvar array |
||
28 | */ |
||
29 | protected static $baseConfig = [ |
||
30 | 'app' => [ |
||
31 | 'title' => 'Infuse', |
||
32 | 'ssl' => false, |
||
33 | 'port' => 80, |
||
34 | ], |
||
35 | 'services' => [ |
||
36 | // these services are required but can be overriden |
||
37 | 'exception_handler' => 'Infuse\Services\ExceptionHandler', |
||
38 | 'locale' => 'Infuse\Services\Locale', |
||
39 | 'logger' => 'Infuse\Services\Logger', |
||
40 | 'method_not_allowed_handler' => 'Infuse\Services\MethodNotAllowedHandler', |
||
41 | 'not_found_handler' => 'Infuse\Services\NotFoundHandler', |
||
42 | 'php_error_handler' => 'Infuse\Services\PhpErrorHandler', |
||
43 | 'router' => 'Infuse\Services\Router', |
||
44 | 'route_resolver' => 'Infuse\Services\RouteResolver', |
||
45 | 'view_engine' => 'Infuse\Services\ViewEngine', |
||
46 | ], |
||
47 | 'i18n' => [ |
||
48 | 'locale' => 'en', |
||
49 | ], |
||
50 | 'console' => [ |
||
51 | 'commands' => [], |
||
52 | ], |
||
53 | ]; |
||
54 | |||
55 | /** |
||
56 | * @staticvar Application |
||
57 | */ |
||
58 | private static $default; |
||
59 | |||
60 | /** |
||
61 | * @var SplStack |
||
62 | */ |
||
63 | private $middleware; |
||
64 | |||
65 | /** |
||
66 | * @param array $settings |
||
67 | * @param string $environment |
||
68 | */ |
||
69 | public function __construct(array $settings = [], $environment = self::ENV_DEVELOPMENT) |
||
118 | |||
119 | /** |
||
120 | * Gets the last created Application instance. |
||
121 | * |
||
122 | * @return Application |
||
123 | */ |
||
124 | public static function getDefault() |
||
128 | |||
129 | //////////////////////// |
||
130 | // ROUTING |
||
131 | //////////////////////// |
||
132 | |||
133 | /** |
||
134 | * Adds a handler to the routing table for a given GET route. |
||
135 | * |
||
136 | * @param string $route path pattern |
||
137 | * @param mixed $handler route handler |
||
138 | * |
||
139 | * @return self |
||
140 | */ |
||
141 | public function get($route, $handler) |
||
147 | |||
148 | /** |
||
149 | * Adds a handler to the routing table for a given POST route. |
||
150 | * |
||
151 | * @param string $route path pattern |
||
152 | * @param mixed $handler route handler |
||
153 | * |
||
154 | * @return self |
||
155 | */ |
||
156 | public function post($route, $handler) |
||
162 | |||
163 | /** |
||
164 | * Adds a handler to the routing table for a given PUT route. |
||
165 | * |
||
166 | * @param string $route path pattern |
||
167 | * @param mixed $handler route handler |
||
168 | * |
||
169 | * @return self |
||
170 | */ |
||
171 | public function put($route, $handler) |
||
177 | |||
178 | /** |
||
179 | * Adds a handler to the routing table for a given DELETE route. |
||
180 | * |
||
181 | * @param string $route path pattern |
||
182 | * @param mixed $handler route handler |
||
183 | * |
||
184 | * @return self |
||
185 | */ |
||
186 | public function delete($route, $handler) |
||
192 | |||
193 | /** |
||
194 | * Adds a handler to the routing table for a given PATCH route. |
||
195 | * |
||
196 | * @param string $route path pattern |
||
197 | * @param mixed $handler route handler |
||
198 | * |
||
199 | * @return self |
||
200 | */ |
||
201 | public function patch($route, $handler) |
||
207 | |||
208 | /** |
||
209 | * Adds a handler to the routing table for a given OPTIONS route. |
||
210 | * |
||
211 | * @param string $route path pattern |
||
212 | * @param mixed $handler route handler |
||
213 | * |
||
214 | * @return self |
||
215 | */ |
||
216 | public function options($route, $handler) |
||
222 | |||
223 | /** |
||
224 | * Adds a handler to the routing table for a given route. |
||
225 | * |
||
226 | * @param string $method HTTP method |
||
227 | * @param string $route path pattern |
||
228 | * @param mixed $handler route handler |
||
229 | * |
||
230 | * @return self |
||
231 | */ |
||
232 | public function map($method, $route, $handler) |
||
238 | |||
239 | //////////////////////// |
||
240 | // REQUESTS |
||
241 | //////////////////////// |
||
242 | |||
243 | /** |
||
244 | * Runs the application. |
||
245 | * |
||
246 | * @return self |
||
247 | */ |
||
248 | public function run() |
||
256 | |||
257 | /** |
||
258 | * Builds a response to an incoming request by routing |
||
259 | * it through the application. |
||
260 | * |
||
261 | * @param Request $req |
||
262 | * |
||
263 | * @return Response |
||
264 | */ |
||
265 | public function handleRequest(Request $req) |
||
298 | |||
299 | //////////////////////// |
||
300 | // MIDDLEWARE |
||
301 | //////////////////////// |
||
302 | |||
303 | /** |
||
304 | * Adds middleware to the application. |
||
305 | * |
||
306 | * The middleware must be callable / invokable with the following |
||
307 | * method signature: |
||
308 | * middlewareFn(Request $req, Response $res, callable $next) |
||
309 | * |
||
310 | * Middleware is called in LIFO order. Each middleware step |
||
311 | * is expected to return a Response object. In order to continue |
||
312 | * processing the middleware chain then call $next($req, $res). |
||
313 | * If a middleware is to be the final step then simply return |
||
314 | * a Response object instead of calling $next. |
||
315 | * |
||
316 | * @param callable $middleware |
||
317 | * |
||
318 | * @return self |
||
319 | */ |
||
320 | public function middleware(callable $middleware) |
||
326 | |||
327 | /** |
||
328 | * Runs the middleware chain. |
||
329 | * |
||
330 | * @param Request $req |
||
331 | * @param Response $res |
||
332 | * |
||
333 | * @return Response $res |
||
334 | */ |
||
335 | public function runMiddleware(Request $req, Response $res) |
||
341 | |||
342 | /** |
||
343 | * Initializes the middleware stack. |
||
344 | * |
||
345 | * @return SplStack |
||
346 | */ |
||
347 | private function initMiddleware() |
||
356 | |||
357 | /** |
||
358 | * Calls the next middleware in the chain. |
||
359 | * DO NOT call directly. |
||
360 | * |
||
361 | * @param Request $req |
||
362 | * @param Response $res |
||
363 | * |
||
364 | * @return Response |
||
365 | */ |
||
366 | public function nextMiddleware(Request $req, Response $res) |
||
380 | |||
381 | //////////////////////// |
||
382 | // Console |
||
383 | //////////////////////// |
||
384 | |||
385 | /** |
||
386 | * Gets a console instance for this application. |
||
387 | * |
||
388 | * @return \Infuse\Console\Application |
||
389 | */ |
||
390 | public function getConsole() |
||
394 | |||
395 | //////////////////////// |
||
396 | // Magic Methods |
||
397 | //////////////////////// |
||
398 | |||
399 | public function __get($k) |
||
403 | |||
404 | public function __isset($k) |
||
408 | |||
409 | //////////////////////// |
||
410 | // HttpKernelInterface |
||
411 | //////////////////////// |
||
412 | |||
413 | public function handle(SymfonyRequest $request, $type = self::MASTER_REQUEST, $catch = true) |
||
420 | } |
||
421 |