Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit fb4bd35

Browse filesBrowse files
committed
refactored the controller manager, moved generic parts to the HttpKernel component
1 parent d0be781 commit fb4bd35
Copy full SHA for fb4bd35

File tree

Expand file treeCollapse file tree

4 files changed

+93
-23
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+93
-23
lines changed

‎src/Symfony/Bundle/FrameworkBundle/Controller/ControllerManager.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Controller/ControllerManager.php
+30-7Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
namespace Symfony\Bundle\FrameworkBundle\Controller;
44

55
use Symfony\Components\HttpKernel\LoggerInterface;
6-
use Symfony\Components\DependencyInjection\ContainerInterface;
6+
use Symfony\Components\HttpKernel\Controller\ControllerManagerInterface;
77
use Symfony\Components\HttpKernel\HttpKernelInterface;
88
use Symfony\Components\HttpFoundation\Request;
9+
use Symfony\Components\DependencyInjection\ContainerInterface;
910

1011
/*
1112
* This file is part of the Symfony framework.
@@ -23,7 +24,7 @@
2324
* @subpackage Bundle_FrameworkBundle
2425
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
2526
*/
26-
class ControllerManager
27+
class ControllerManager implements ControllerManagerInterface
2728
{
2829
protected $container;
2930
protected $logger;
@@ -112,16 +113,28 @@ public function render($controller, array $options = array())
112113
}
113114

114115
/**
115-
* Creates the Controller instance associated with the controller string
116+
* Returns the Controller instance associated with a Request.
117+
*
118+
* This method looks for a '_controller' request parameter that represents
119+
* the controller name (a string like BlogBundle:Post:index).
116120
*
117-
* @param string $controller A controller name (a string like BlogBundle:Post:index)
121+
* @param \Symfony\Components\HttpFoundation\Request $request A Request instance
118122
*
119-
* @return array An array composed of the Controller instance and the Controller method
123+
* @return mixed|Boolean A PHP callable representing the Controller,
124+
* or false if this manager is not able to determine the controller
120125
*
121126
* @throws \InvalidArgumentException|\LogicException If the controller can't be found
122127
*/
123-
public function findController($controller)
128+
public function getController(Request $request)
124129
{
130+
if (!$controller = $request->path->get('_controller')) {
131+
if (null !== $this->logger) {
132+
$this->logger->err('Unable to look for the controller as the "_controller" parameter is missing');
133+
}
134+
135+
return false;
136+
}
137+
125138
list($bundle, $controller, $action) = explode(':', $controller);
126139
$bundle = strtr($bundle, array('/' => '\\'));
127140
$class = null;
@@ -154,6 +167,7 @@ public function findController($controller)
154167
}
155168

156169
$controller = new $class($this->container);
170+
$controller->setRequest($request);
157171

158172
$method = $action.'Action';
159173
if (!method_exists($controller, $method)) {
@@ -168,10 +182,19 @@ public function findController($controller)
168182
}
169183

170184
/**
185+
* Returns the arguments to pass to the controller.
186+
*
187+
* @param \Symfony\Components\HttpFoundation\Request $request A Request instance
188+
* @param mixed $controller A PHP callable
189+
*
171190
* @throws \RuntimeException When value for argument given is not provided
172191
*/
173-
public function getMethodArguments(array $path, $controller, $method)
192+
public function getMethodArguments(Request $request, $controller)
174193
{
194+
$path = $request->path->all();
195+
196+
list($controller, $method) = $controller;
197+
175198
$r = new \ReflectionObject($controller);
176199
$arguments = array();
177200
foreach ($r->getMethod($method)->getParameters() as $param) {

‎src/Symfony/Bundle/FrameworkBundle/Resources/config/web.xml

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Resources/config/web.xml
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<parameters>
88
<parameter key="request_listener.class">Symfony\Bundle\FrameworkBundle\RequestListener</parameter>
99
<parameter key="controller_manager.class">Symfony\Bundle\FrameworkBundle\Controller\ControllerManager</parameter>
10-
<parameter key="controller_loader_listener.class">Symfony\Bundle\FrameworkBundle\Controller\ControllerLoaderListener</parameter>
10+
<parameter key="controller_loader_listener.class">Symfony\Components\HttpKernel\Controller\ControllerLoaderListener</parameter>
1111
<parameter key="router.class">Symfony\Components\Routing\Router</parameter>
1212
<parameter key="response_listener.class">Symfony\Components\HttpKernel\ResponseListener</parameter>
1313
<parameter key="exception_listener.class">Symfony\Bundle\FrameworkBundle\Controller\ExceptionListener</parameter>

‎src/Symfony/Bundle/FrameworkBundle/Controller/ControllerLoaderListener.php renamed to ‎src/Symfony/Components/HttpKernel/Controller/ControllerLoaderListener.php

Copy file name to clipboardExpand all lines: src/Symfony/Components/HttpKernel/Controller/ControllerLoaderListener.php
+5-15Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
<?php
22

3-
namespace Symfony\Bundle\FrameworkBundle\Controller;
3+
namespace Symfony\Components\HttpKernel\Controller;
44

5-
use Symfony\Components\HttpKernel\LoggerInterface;
65
use Symfony\Components\EventDispatcher\EventDispatcher;
76
use Symfony\Components\EventDispatcher\Event;
87

@@ -26,12 +25,10 @@
2625
class ControllerLoaderListener
2726
{
2827
protected $manager;
29-
protected $logger;
3028

31-
public function __construct(ControllerManager $manager, LoggerInterface $logger = null)
29+
public function __construct(ControllerManagerInterface $manager)
3230
{
3331
$this->manager = $manager;
34-
$this->logger = $logger;
3532
}
3633

3734
/**
@@ -55,20 +52,13 @@ public function resolve(Event $event)
5552
{
5653
$request = $event->getParameter('request');
5754

58-
if (!$controller = $request->path->get('_controller')) {
59-
if (null !== $this->logger) {
60-
$this->logger->err('Unable to look for the controller as the "_controller" parameter is missing');
61-
}
62-
55+
if (false === $controller = $this->manager->getController($request)) {
6356
return false;
6457
}
6558

66-
list($controller, $method) = $this->manager->findController($controller);
67-
$controller->setRequest($request);
68-
69-
$arguments = $this->manager->getMethodArguments($request->path->all(), $controller, $method);
59+
$arguments = $this->manager->getMethodArguments($request, $controller);
7060

71-
$event->setReturnValue(array(array($controller, $method), $arguments));
61+
$event->setReturnValue(array($controller, $arguments));
7262

7363
return true;
7464
}
+57Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
namespace Symfony\Components\HttpKernel\Controller;
4+
5+
use Symfony\Components\HttpFoundation\Request;
6+
7+
/*
8+
* This file is part of the Symfony framework.
9+
*
10+
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
11+
*
12+
* This source file is subject to the MIT license that is bundled
13+
* with this source code in the file LICENSE.
14+
*/
15+
16+
/**
17+
* A ControllerManagerInterface implementation knows how to determine the
18+
* controller to execute based on a Request object.
19+
*
20+
* It can also determine the arguments to pass to the Controller.
21+
*
22+
* A Controller can be any valid PHP callable.
23+
*
24+
* @package Symfony
25+
* @subpackage Bundle_FrameworkBundle
26+
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
27+
*/
28+
interface ControllerManagerInterface
29+
{
30+
/**
31+
* Returns the Controller instance associated with a Request.
32+
*
33+
* As several managers can exist for a single application, a manager must
34+
* return false when it is not able to determine the controller.
35+
*
36+
* The manager must only throw an exception when it should be able to load
37+
* controller but cannot because of some errors made by the developer.
38+
*
39+
* @param \Symfony\Components\HttpFoundation\Request $request A Request instance
40+
*
41+
* @return mixed|Boolean A PHP callable representing the Controller,
42+
* or false if this manager is not able to determine the controller
43+
*
44+
* @throws \InvalidArgumentException|\LogicException If the controller can't be found
45+
*/
46+
public function getController(Request $request);
47+
48+
/**
49+
* Returns the arguments to pass to the controller.
50+
*
51+
* @param \Symfony\Components\HttpFoundation\Request $request A Request instance
52+
* @param mixed $controller A PHP callable
53+
*
54+
* @throws \RuntimeException When value for argument given is not provided
55+
*/
56+
public function getMethodArguments(Request $request, $controller);
57+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.