Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 73ab687

Browse files
committed
moved ControllerResolver methods to HttpKernel (makes more sense)
1 parent c68501c commit 73ab687

File tree

10 files changed

+162
-159
lines changed

10 files changed

+162
-159
lines changed

src/Symfony/Bundle/FrameworkBundle/Controller/ControllerResolver.php

Lines changed: 0 additions & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@
1313

1414
use Symfony\Component\HttpKernel\Log\LoggerInterface;
1515
use Symfony\Component\HttpKernel\Controller\ControllerResolver as BaseControllerResolver;
16-
use Symfony\Component\HttpKernel\HttpKernelInterface;
17-
use Symfony\Component\HttpFoundation\Request;
18-
use Symfony\Component\EventDispatcher\Event;
1916
use Symfony\Component\DependencyInjection\ContainerInterface;
2017
use Symfony\Bundle\FrameworkBundle\Controller\ControllerNameParser;
2118
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
@@ -29,7 +26,6 @@ class ControllerResolver extends BaseControllerResolver
2926
{
3027
protected $container;
3128
protected $parser;
32-
protected $esiSupport;
3329

3430
/**
3531
* Constructor.
@@ -83,137 +79,4 @@ protected function createController($controller)
8379

8480
return array($controller, $method);
8581
}
86-
87-
/**
88-
* Forwards the request to another controller.
89-
*
90-
* @param string $controller The controller name (a string like BlogBundle:Post:index)
91-
* @param array $attributes An array of request attributes
92-
* @param array $query An array of request query parameters
93-
*
94-
* @return Response A Response instance
95-
*/
96-
public function forward($controller, array $attributes = array(), array $query = array())
97-
{
98-
$attributes['_controller'] = $controller;
99-
$subRequest = $this->container->get('request')->duplicate($query, null, $attributes);
100-
101-
return $this->container->get('kernel')->handle($subRequest, HttpKernelInterface::SUB_REQUEST);
102-
}
103-
104-
/**
105-
* Renders a Controller and returns the Response content.
106-
*
107-
* Note that this method generates an esi:include tag only when both the standalone
108-
* option is set to true and the request has ESI capability (@see Symfony\Component\HttpKernel\Cache\ESI).
109-
*
110-
* Available options:
111-
*
112-
* * attributes: An array of request attributes (only when the first argument is a controller)
113-
* * query: An array of request query parameters (only when the first argument is a controller)
114-
* * ignore_errors: true to return an empty string in case of an error
115-
* * alt: an alternative controller to execute in case of an error (can be a controller, a URI, or an array with the controller, the attributes, and the query arguments)
116-
* * standalone: whether to generate an esi:include tag or not when ESI is supported
117-
* * comment: a comment to add when returning an esi:include tag
118-
*
119-
* @param string $controller A controller name to execute (a string like BlogBundle:Post:index), or a relative URI
120-
* @param array $options An array of options
121-
*
122-
* @return string The Response content
123-
*/
124-
public function render($controller, array $options = array())
125-
{
126-
$options = array_merge(array(
127-
'attributes' => array(),
128-
'query' => array(),
129-
'ignore_errors' => !$this->container->getParameter('kernel.debug'),
130-
'alt' => array(),
131-
'standalone' => false,
132-
'comment' => '',
133-
), $options);
134-
135-
if (!is_array($options['alt'])) {
136-
$options['alt'] = array($options['alt']);
137-
}
138-
139-
if (null === $this->esiSupport) {
140-
$this->esiSupport = $this->container->has('esi') && $this->container->get('esi')->hasSurrogateEsiCapability($this->container->get('request'));
141-
}
142-
143-
if ($this->esiSupport && $options['standalone']) {
144-
$uri = $this->generateInternalUri($controller, $options['attributes'], $options['query']);
145-
146-
$alt = '';
147-
if ($options['alt']) {
148-
$alt = $this->generateInternalUri($options['alt'][0], isset($options['alt'][1]) ? $options['alt'][1] : array(), isset($options['alt'][2]) ? $options['alt'][2] : array());
149-
}
150-
151-
return $this->container->get('esi')->renderIncludeTag($uri, $alt, $options['ignore_errors'], $options['comment']);
152-
}
153-
154-
$request = $this->container->get('request');
155-
156-
// controller or URI?
157-
if (0 === strpos($controller, '/')) {
158-
$subRequest = Request::create($controller, 'get', array(), $request->cookies->all(), array(), $request->server->all());
159-
$subRequest->setSession($request->getSession());
160-
} else {
161-
$options['attributes']['_controller'] = $controller;
162-
$options['attributes']['_format'] = $request->getRequestFormat();
163-
$subRequest = $request->duplicate($options['query'], null, $options['attributes']);
164-
}
165-
166-
try {
167-
$response = $this->container->get('kernel')->handle($subRequest, HttpKernelInterface::SUB_REQUEST, true);
168-
169-
if (200 != $response->getStatusCode()) {
170-
throw new \RuntimeException(sprintf('Error when rendering "%s" (Status code is %s).', $request->getUri(), $response->getStatusCode()));
171-
}
172-
173-
return $response->getContent();
174-
} catch (\Exception $e) {
175-
if ($options['alt']) {
176-
$alt = $options['alt'];
177-
unset($options['alt']);
178-
$options['attributes'] = isset($alt[1]) ? $alt[1] : array();
179-
$options['query'] = isset($alt[2]) ? $alt[2] : array();
180-
181-
return $this->render($alt[0], $options);
182-
}
183-
184-
if (!$options['ignore_errors']) {
185-
throw $e;
186-
}
187-
}
188-
}
189-
190-
/**
191-
* Generates an internal URI for a given controller.
192-
*
193-
* This method uses the "_internal" route, which should be available.
194-
*
195-
* @param string $controller A controller name to execute (a string like BlogBundle:Post:index), or a relative URI
196-
* @param array $attributes An array of request attributes
197-
* @param array $query An array of request query parameters
198-
*
199-
* @return string An internal URI
200-
*/
201-
public function generateInternalUri($controller, array $attributes = array(), array $query = array())
202-
{
203-
if (0 === strpos($controller, '/')) {
204-
return $controller;
205-
}
206-
207-
$uri = $this->container->get('router')->generate('_internal', array(
208-
'controller' => $controller,
209-
'path' => $attributes ? http_build_query($attributes) : 'none',
210-
'_format' => $this->container->get('request')->getRequestFormat(),
211-
), true);
212-
213-
if ($query) {
214-
$uri = $uri.'?'.http_build_query($query);
215-
}
216-
217-
return $uri;
218-
}
21982
}

src/Symfony/Bundle/FrameworkBundle/HttpKernel.php

Lines changed: 141 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,17 @@
1717
class HttpKernel extends BaseHttpKernel
1818
{
1919
protected $container;
20+
protected $esiSupport;
2021

21-
public function __construct(ContainerInterface $container, BaseEventDispatcher $eventDispatcher, ControllerResolverInterface $controllerResolver)
22+
public function __construct(ContainerInterface $container, ControllerResolverInterface $controllerResolver)
2223
{
23-
parent::__construct($eventDispatcher, $controllerResolver);
24-
2524
$this->container = $container;
25+
$this->resolver = $controllerResolver;
26+
}
27+
28+
public function setEventDispatcher(BaseEventDispatcher $dispatcher)
29+
{
30+
$this->dispatcher = $dispatcher;
2631
}
2732

2833
public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true)
@@ -42,4 +47,137 @@ public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQ
4247

4348
return $response;
4449
}
50+
51+
/**
52+
* Forwards the request to another controller.
53+
*
54+
* @param string $controller The controller name (a string like BlogBundle:Post:index)
55+
* @param array $attributes An array of request attributes
56+
* @param array $query An array of request query parameters
57+
*
58+
* @return Response A Response instance
59+
*/
60+
public function forward($controller, array $attributes = array(), array $query = array())
61+
{
62+
$attributes['_controller'] = $controller;
63+
$subRequest = $this->container->get('request')->duplicate($query, null, $attributes);
64+
65+
return $this->handle($subRequest, HttpKernelInterface::SUB_REQUEST);
66+
}
67+
68+
/**
69+
* Renders a Controller and returns the Response content.
70+
*
71+
* Note that this method generates an esi:include tag only when both the standalone
72+
* option is set to true and the request has ESI capability (@see Symfony\Component\HttpKernel\Cache\ESI).
73+
*
74+
* Available options:
75+
*
76+
* * attributes: An array of request attributes (only when the first argument is a controller)
77+
* * query: An array of request query parameters (only when the first argument is a controller)
78+
* * ignore_errors: true to return an empty string in case of an error
79+
* * alt: an alternative controller to execute in case of an error (can be a controller, a URI, or an array with the controller, the attributes, and the query arguments)
80+
* * standalone: whether to generate an esi:include tag or not when ESI is supported
81+
* * comment: a comment to add when returning an esi:include tag
82+
*
83+
* @param string $controller A controller name to execute (a string like BlogBundle:Post:index), or a relative URI
84+
* @param array $options An array of options
85+
*
86+
* @return string The Response content
87+
*/
88+
public function render($controller, array $options = array())
89+
{
90+
$options = array_merge(array(
91+
'attributes' => array(),
92+
'query' => array(),
93+
'ignore_errors' => !$this->container->getParameter('kernel.debug'),
94+
'alt' => array(),
95+
'standalone' => false,
96+
'comment' => '',
97+
), $options);
98+
99+
if (!is_array($options['alt'])) {
100+
$options['alt'] = array($options['alt']);
101+
}
102+
103+
if (null === $this->esiSupport) {
104+
$this->esiSupport = $this->container->has('esi') && $this->container->get('esi')->hasSurrogateEsiCapability($this->container->get('request'));
105+
}
106+
107+
if ($this->esiSupport && $options['standalone']) {
108+
$uri = $this->generateInternalUri($controller, $options['attributes'], $options['query']);
109+
110+
$alt = '';
111+
if ($options['alt']) {
112+
$alt = $this->generateInternalUri($options['alt'][0], isset($options['alt'][1]) ? $options['alt'][1] : array(), isset($options['alt'][2]) ? $options['alt'][2] : array());
113+
}
114+
115+
return $this->container->get('esi')->renderIncludeTag($uri, $alt, $options['ignore_errors'], $options['comment']);
116+
}
117+
118+
$request = $this->container->get('request');
119+
120+
// controller or URI?
121+
if (0 === strpos($controller, '/')) {
122+
$subRequest = Request::create($controller, 'get', array(), $request->cookies->all(), array(), $request->server->all());
123+
$subRequest->setSession($request->getSession());
124+
} else {
125+
$options['attributes']['_controller'] = $controller;
126+
$options['attributes']['_format'] = $request->getRequestFormat();
127+
$subRequest = $request->duplicate($options['query'], null, $options['attributes']);
128+
}
129+
130+
try {
131+
$response = $this->handle($subRequest, HttpKernelInterface::SUB_REQUEST, true);
132+
133+
if (200 != $response->getStatusCode()) {
134+
throw new \RuntimeException(sprintf('Error when rendering "%s" (Status code is %s).', $request->getUri(), $response->getStatusCode()));
135+
}
136+
137+
return $response->getContent();
138+
} catch (\Exception $e) {
139+
if ($options['alt']) {
140+
$alt = $options['alt'];
141+
unset($options['alt']);
142+
$options['attributes'] = isset($alt[1]) ? $alt[1] : array();
143+
$options['query'] = isset($alt[2]) ? $alt[2] : array();
144+
145+
return $this->render($alt[0], $options);
146+
}
147+
148+
if (!$options['ignore_errors']) {
149+
throw $e;
150+
}
151+
}
152+
}
153+
154+
/**
155+
* Generates an internal URI for a given controller.
156+
*
157+
* This method uses the "_internal" route, which should be available.
158+
*
159+
* @param string $controller A controller name to execute (a string like BlogBundle:Post:index), or a relative URI
160+
* @param array $attributes An array of request attributes
161+
* @param array $query An array of request query parameters
162+
*
163+
* @return string An internal URI
164+
*/
165+
public function generateInternalUri($controller, array $attributes = array(), array $query = array())
166+
{
167+
if (0 === strpos($controller, '/')) {
168+
return $controller;
169+
}
170+
171+
$uri = $this->container->get('router')->generate('_internal', array(
172+
'controller' => $controller,
173+
'path' => $attributes ? http_build_query($attributes) : 'none',
174+
'_format' => $this->container->get('request')->getRequestFormat(),
175+
), true);
176+
177+
if ($query) {
178+
$uri = $uri.'?'.http_build_query($query);
179+
}
180+
181+
return $uri;
182+
}
45183
}

src/Symfony/Bundle/FrameworkBundle/Resources/config/services.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323

2424
<service id="http_kernel" class="%http_kernel.class%">
2525
<argument type="service" id="service_container" />
26-
<argument type="service" id="event_dispatcher" />
2726
<argument type="service" id="controller_resolver" />
27+
<call method="setEventDispatcher"><argument type="service" id="event_dispatcher" /></call>
2828
</service>
2929

3030
<service id="response" class="%response.class%" scope="prototype">

src/Symfony/Bundle/FrameworkBundle/Resources/config/templating.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@
9292

9393
<service id="templating.helper.actions" class="%templating.helper.actions.class%">
9494
<tag name="templating.helper" alias="actions" />
95-
<argument type="service" id="controller_resolver" />
95+
<argument type="service" id="http_kernel" />
9696
</service>
9797

9898
<service id="templating.helper.code" class="%templating.helper.code.class%">

src/Symfony/Bundle/FrameworkBundle/Templating/Helper/ActionsHelper.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
namespace Symfony\Bundle\FrameworkBundle\Templating\Helper;
1313

1414
use Symfony\Component\Templating\Helper\Helper;
15-
use Symfony\Bundle\FrameworkBundle\Controller\ControllerResolver;
15+
use Symfony\Bundle\FrameworkBundle\HttpKernel;
1616

1717
/**
1818
* ActionsHelper manages action inclusions.
@@ -21,16 +21,16 @@
2121
*/
2222
class ActionsHelper extends Helper
2323
{
24-
protected $resolver;
24+
protected $kernel;
2525

2626
/**
2727
* Constructor.
2828
*
29-
* @param Constructor $resolver A ControllerResolver instance
29+
* @param HttpKernel $kernel A HttpKernel instance
3030
*/
31-
public function __construct(ControllerResolver $resolver)
31+
public function __construct(HttpKernel $kernel)
3232
{
33-
$this->resolver = $resolver;
33+
$this->kernel = $kernel;
3434
}
3535

3636
/**
@@ -53,7 +53,7 @@ public function output($controller, array $attributes = array(), array $options
5353
* @param array $attributes An array of request attributes
5454
* @param array $options An array of options
5555
*
56-
* @see Symfony\Bundle\FrameworkBundle\Controller\ControllerResolver::render()
56+
* @see Symfony\Bundle\FrameworkBundle\HttpKernel::render()
5757
*/
5858
public function render($controller, array $attributes = array(), array $options = array())
5959
{
@@ -64,7 +64,7 @@ public function render($controller, array $attributes = array(), array $options
6464
$options['query'] = $options['query'];
6565
}
6666

67-
return $this->resolver->render($controller, $options);
67+
return $this->kernel->render($controller, $options);
6868
}
6969

7070
/**

0 commit comments

Comments
 (0)