|
2 | 2 |
|
3 | 3 | namespace Symfony\Component\HttpKernel;
|
4 | 4 |
|
5 |
| -use Symfony\Component\EventDispatcher\Event; |
6 | 5 | use Symfony\Component\EventDispatcher\EventDispatcher;
|
| 6 | +use Symfony\Component\DependencyInjection\ContainerInterface; |
7 | 7 | use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface;
|
8 |
| -use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
9 | 8 | use Symfony\Component\HttpFoundation\Request;
|
10 |
| -use Symfony\Component\HttpFoundation\Response; |
| 9 | +use Symfony\Component\HttpKernel\HttpKernelInterface; |
11 | 10 |
|
12 | 11 | /*
|
13 | 12 | * This file is part of the Symfony package.
|
|
23 | 22 | *
|
24 | 23 | * @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
25 | 24 | */
|
26 |
| -class HttpKernel implements HttpKernelInterface |
| 25 | +class HttpKernel extends BaseHttpKernel |
27 | 26 | {
|
28 |
| - protected $dispatcher; |
29 |
| - protected $resolver; |
30 |
| - protected $request; |
| 27 | + protected $container; |
31 | 28 |
|
32 | 29 | /**
|
33 | 30 | * Constructor
|
34 | 31 | *
|
| 32 | + * @param ContainerInterface $container An ContainerInterface instance |
35 | 33 | * @param EventDispatcher $dispatcher An event dispatcher instance
|
36 | 34 | * @param ControllerResolverInterface $resolver A ControllerResolverInterface instance
|
37 | 35 | */
|
38 |
| - public function __construct(EventDispatcher $dispatcher, ControllerResolverInterface $resolver) |
| 36 | + public function __construct(ContainerInterface $container, EventDispatcher $dispatcher, ControllerResolverInterface $resolver) |
39 | 37 | {
|
40 |
| - $this->dispatcher = $dispatcher; |
41 |
| - $this->resolver = $resolver; |
42 |
| - } |
| 38 | + $this->container = $container; |
43 | 39 |
|
44 |
| - /** |
45 |
| - * Gets the Request instance associated with the master request. |
46 |
| - * |
47 |
| - * @return Request A Request instance |
48 |
| - */ |
49 |
| - public function getRequest() |
50 |
| - { |
51 |
| - return $this->request; |
| 40 | + parent::__construct($dispatcher, $resolver); |
52 | 41 | }
|
53 | 42 |
|
54 | 43 | /**
|
55 |
| - * Handles a Request to convert it to a Response. |
56 |
| - * |
57 |
| - * All exceptions are caught, and a core.exception event is notified |
58 |
| - * for user management. |
59 |
| - * |
60 |
| - * @param Request $request A Request instance |
61 |
| - * @param integer $type The type of the request (one of HttpKernelInterface::MASTER_REQUEST or HttpKernelInterface::SUB_REQUEST) |
62 |
| - * @param Boolean $raw Whether to catch exceptions or not |
63 |
| - * |
64 |
| - * @return Response A Response instance |
65 |
| - * |
66 |
| - * @throws \Exception When an Exception occurs during processing |
67 |
| - * and couldn't be caught by event processing or $raw is true |
| 44 | + * {@inheritdoc} |
68 | 45 | */
|
69 | 46 | public function handle(Request $request = null, $type = HttpKernelInterface::MASTER_REQUEST, $raw = false)
|
70 | 47 | {
|
71 | 48 | if (null === $request) {
|
72 |
| - $request = new Request(); |
| 49 | + $request = $this->container->get('request'); |
| 50 | + } else { |
| 51 | + $this->container->set('request', $request); |
73 | 52 | }
|
74 | 53 |
|
75 | 54 | if (HttpKernelInterface::MASTER_REQUEST === $type) {
|
76 | 55 | $this->request = $request;
|
77 | 56 | }
|
78 | 57 |
|
79 |
| - try { |
80 |
| - return $this->handleRaw($request, $type); |
81 |
| - } catch (\Exception $e) { |
82 |
| - if (true === $raw) { |
83 |
| - throw $e; |
84 |
| - } |
85 |
| - |
86 |
| - // exception |
87 |
| - $event = $this->dispatcher->notifyUntil(new Event($this, 'core.exception', array('request_type' => $type, 'request' => $request, 'exception' => $e))); |
88 |
| - if ($event->isProcessed()) { |
89 |
| - return $this->filterResponse($event->getReturnValue(), $request, 'A "core.exception" listener returned a non response object.', $type); |
90 |
| - } |
91 |
| - |
92 |
| - throw $e; |
93 |
| - } |
94 |
| - } |
95 |
| - |
96 |
| - /** |
97 |
| - * Handles a request to convert it to a response. |
98 |
| - * |
99 |
| - * Exceptions are not caught. |
100 |
| - * |
101 |
| - * @param Request $request A Request instance |
102 |
| - * @param integer $type The type of the request (one of HttpKernelInterface::MASTER_REQUEST or HttpKernelInterface::SUB_REQUEST) |
103 |
| - * |
104 |
| - * @return Response A Response instance |
105 |
| - * |
106 |
| - * @throws \LogicException If one of the listener does not behave as expected |
107 |
| - * @throws NotFoundHttpException When controller cannot be found |
108 |
| - */ |
109 |
| - protected function handleRaw(Request $request, $type = self::MASTER_REQUEST) |
110 |
| - { |
111 |
| - // request |
112 |
| - $event = $this->dispatcher->notifyUntil(new Event($this, 'core.request', array('request_type' => $type, 'request' => $request))); |
113 |
| - if ($event->isProcessed()) { |
114 |
| - return $this->filterResponse($event->getReturnValue(), $request, 'A "core.request" listener returned a non response object.', $type); |
115 |
| - } |
116 |
| - |
117 |
| - // load controller |
118 |
| - if (false === $controller = $this->resolver->getController($request)) { |
119 |
| - throw new NotFoundHttpException('Unable to find the controller.'); |
120 |
| - } |
121 |
| - |
122 |
| - $event = $this->dispatcher->filter(new Event($this, 'core.controller', array('request_type' => $type, 'request' => $request)), $controller); |
123 |
| - $controller = $event->getReturnValue(); |
124 |
| - |
125 |
| - // controller must be a callable |
126 |
| - if (!is_callable($controller)) { |
127 |
| - throw <
F438
span class=pl-k>new \LogicException(sprintf('The controller must be a callable (%s).', var_export($controller, true))); |
128 |
| - } |
129 |
| - |
130 |
| - // controller arguments |
131 |
| - $arguments = $this->resolver->getArguments($request, $controller); |
132 |
| - |
133 |
| - // call controller |
134 |
| - $retval = call_user_func_array($controller, $arguments); |
135 |
| - |
136 |
| - // view |
137 |
| - $event = $this->dispatcher->filter(new Event($this, 'core.view', array('request_type' => $type, 'request' => $request)), $retval); |
138 |
| - |
139 |
| - return $this->filterResponse($event->getReturnValue(), $request, sprintf('The controller must return a response (instead of %s).', is_object($event->getReturnValue()) ? 'an object of class '.get_class($event->getReturnValue()) : is_array($event->getReturnValue()) ? 'an array' : str_replace("\n", '', var_export($event->getReturnValue(), true))), $type); |
140 |
| - } |
| 58 | + $response = parent::handle($request, $type, $raw); |
141 | 59 |
|
142 |
| - /** |
143 |
| - * Filters a response object. |
144 |
| - * |
145 |
| - * @param Response $response A Response instance |
146 |
| - * @param string $message A error message in case the response is not a Response object |
147 |
| - * @param integer $type The type of the request (one of HttpKernelInterface::MASTER_REQUEST or HttpKernelInterface::SUB_REQUEST) |
148 |
| - * |
149 |
| - * @return Response The filtered Response instance |
150 |
| - * |
151 |
| - * @throws \RuntimeException if the passed object is not a Response instance |
152 |
| - */ |
153 |
| - protected function filterResponse($response, $request, $message, $type) |
154 |
| - { |
155 |
| - if (!$response instanceof Response) { |
156 |
| - throw new \RuntimeException($message); |
157 |
| - } |
158 |
| - |
159 |
| - $event = $this->dispatcher->filter(new Event($this, 'core.response', array('request_type' => $type, 'request' => $request)), $response); |
160 |
| - $response = $event->getReturnValue(); |
161 |
| - |
162 |
| - if (!$response instanceof Response) { |
163 |
| - throw new \RuntimeException('A "core.response" listener returned a non response object.'); |
164 |
| - } |
| 60 | + $this->container->set('request', $this->request); |
165 | 61 |
|
166 | 62 | return $response;
|
167 | 63 | }
|
|
0 commit comments