8000 deprecate bundle:controller:action and service:method notation · symfony/symfony@841d03c · GitHub
[go: up one dir, main page]

Skip to content

Commit 841d03c

Browse files
committed
deprecate bundle:controller:action and service:method notation
1 parent 5391e17 commit 841d03c

File tree

4 files changed

+27
-60
lines changed

4 files changed

+27
-60
lines changed

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,14 @@ protected function createController($controller)
3838
if (false === strpos($controller, '::') && 2 === substr_count($controller, ':')) {
3939
// controller in the a:b:c notation then
4040
$controller = $this->parser->parse($controller);
41-
}
42-
43-
$resolvedController = parent::createController($controller);
4441

45-
if (1 === substr_count($controller, ':') && is_array($resolvedController)) {
46-
$resolvedController[0] = $this->configureController($resolvedController[0]);
42+
@trigger_error(sprintf(
43+
'Referencing controllers with the bundle:controller:action notation is deprecated since version 4.1 and will be removed in 5.0. Use %s instead.',
44+
$controller
45+
), E_USER_DEPRECATED);
4746
}
4847

49 8000 -
return $resolvedController;
48+
return parent::createController($controller);
5049
}
5150

5251
/**

src/Symfony/Component/HttpKernel/Controller/ContainerControllerResolver.php

Lines changed: 8 additions & 36 deletions
57
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use Psr\Container\ContainerInterface;
1515
use Psr\Log\LoggerInterface;
1616
use Symfony\Component\DependencyInjection\Container;
17-
use Symfony\Component\HttpFoundation\Request;
1817

1918
/**
2019
* A controller resolver searching for a controller in a psr-11 container when using the "service:method" notation.
@@ -33,44 +32,17 @@ public function __construct(ContainerInterface $container, LoggerInterface $logg
3332
parent::__construct($logger);
3433
}
3534

36-
/**
37-
* Returns a callable for the given controller.
38-
*
39-
* @param string $controller A Controller string
40-
*
41-
* @return mixed A PHP callable
42-
*
43-
* @throws \LogicException When the name could not be parsed
44-
* @throws \InvalidArgumentException When the controller class does not exist
45-
*/
4635
protected function createController($controller)
4736
{
48-
if (false !== strpos($controller, '::')) {
49-
return parent::createController($controller);
50-
}
51-
52-
$method = null;
53-
if (1 == substr_count($controller, ':')) {
54-
// controller in the "service:method" notation
55-
list($controller, $method) = explode(':', $controller, 2);
56-
}
57-
58-
if (!$this->container->has($controller)) {
59-
$this->throwExceptionIfControllerWasRemoved($controller);
60-
61-
throw new \LogicException(sprintf('Controller not found: service "%s" does not exist.', $controller));
62-
}
63-
64-
$service = $this->container->get($controller);
65-
if (null !== $method) {
66-
return array($service, $method);
67-
}
68-
69-
if (!method_exists($service, '__invoke')) {
70-
throw new \LogicException(sprintf('Controller "%s" cannot be called without a method name. Did you forget an "__invoke" method?', $controller));
37+
if (1 === substr_count($controller, ':')) {
38+
$controller = str_replace(':', '::', $controller);
39+
@trigger_error(sprintf(
40+
'Referencing controllers with a single colon is deprecated since version 4.1 and will be removed in 5.0. Use %s instead.',
41+
$controller
42+
), E_USER_DEPRECATED);
7143
}
7244

73-
return $service;
45+
return parent::createController($controller);
7446
}
7547

7648
/**
@@ -84,7 +56,7 @@ protected function instantiateController($class)
8456

85
try {
8658
return parent::instantiateController($class);
87-
} catch (\ArgumentCountError $e) {
59+
} catch (\ArgumentCountError | \InvalidArgumentException $e) {
8860
}
8961

9062
$this->throwExceptionIfControllerWasRemoved($class, $e);

src/Symfony/Component/HttpKernel/Controller/ControllerResolver.php

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616

1717
/**
1818
* This implementation uses the '_controller' request attribute to determine
19-
* the controller to execute and uses the request attributes to determine
20-
* the controller method arguments.
19+
* the controller to execute.
2120
*
2221
* @author Fabien Potencier <fabien@symfony.com>
2322
*/
@@ -32,9 +31,6 @@ public function __construct(LoggerInterface $logger = null)
3231

3332
/**
3433
* {@inheritdoc}
35-
*
36-
* This method looks for a '_controller' request attribute that represents
37-
* the controller name (a string like ClassName::MethodName).
3834
*/
3935
public function getController(Request $request)
4036
{
@@ -66,12 +62,8 @@ public function getController(Request $request)
6662
throw new \InvalidArgumentException(sprintf('Controller "%s" for URI "%s" is not callable.', get_class($controller), $request->getPathInfo()));
6763
}
6864

69-
if (false === strpos($controller, ':')) {
70-
if (method_exists($controller, '__invoke')) {
71-
return $this->instantiateController($controller);
72-
} elseif (function_exists($controller)) {
73-
return $controller;
74-
}
65+
if (function_exists($controller)) {
66+
return $controller;
7567
}
7668

7769
$callable = $this->createController($controller);
@@ -95,15 +87,11 @@ public function getController(Request $request)
9587
protected function createController($controller)
9688
{
9789
if (false === strpos($controller, '::')) {
98-
throw new \InvalidArgumentException(sprintf('Unable to find controller "%s".', $controller));
90+
return $this->instantiateController($controller);
9991
}
10092

10193
list($class, $method) = explode('::', $controller, 2);
10294

103-
if (!class_exists($class)) {
104-
throw new \InvalidArgumentException(sprintf('Class "%s" does not exist.', $class));
105-
}
106-
10795
return array($this->instantiateController($class), $method);
10896
}
10997

@@ -116,6 +104,10 @@ protected function createController($controller)
116104
*/
117105
protected function instantiateController($class)
118106
{
107+
if (!class_exists($class)) {
108+
throw new \InvalidArgumentException(sprintf('Class "%s" does not exist.', $class));
109+
}
110+
119111
return new $class();
120112
}
121113

@@ -135,6 +127,12 @@ private function getControllerError($callable)
135127
}
136128
}
137129

130+
if (is_object($callable)) {
131+
if (!method_exists($callable, '__invoke')) {
132+
return sprintf('Controller class "%s" cannot be called without a method name. Did you forget an "__invoke" method?', get_class($callable));
133+
}
134+
}
135+
138136
if (!is_array($callable)) {
139137
return sprintf('Invalid type for controller given, expected string or array, got "%s".', gettype($callable));
140138
}

src/Symfony/Component/HttpKernel/Controller/ControllerResolverInterface.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
* A ControllerResolverInterface implementation knows how to determine the
1818
* controller to execute based on a Request object.
1919
*
20-
* It can also determine the arguments to pass to the Controller.
21-
*
2220
* A Controller can be any valid PHP callable.
2321
*
2422
* @author Fabien Potencier <fabien@symfony.com>

0 commit comments

Comments
 (0)
0