1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of Symplify |
7
|
|
|
* Copyright (c) 2015 Tomas Votruba (http://tomasvotruba.cz). |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Symplify\ControllerAutowire\HttpKernel\Controller; |
11
|
|
|
|
12
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\ControllerNameParser; |
13
|
|
|
use Symfony\Component\DependencyInjection\ContainerAwareInterface; |
14
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
15
|
|
|
use Symfony\Component\HttpFoundation\Request; |
16
|
|
|
use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface; |
17
|
|
|
|
18
|
|
|
final class ControllerResolver implements ControllerResolverInterface |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var ControllerResolverInterface |
22
|
|
|
*/ |
23
|
|
|
private $controllerResolver; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var ContainerInterface |
27
|
|
|
*/ |
28
|
|
|
private $container; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var ControllerNameParser |
32
|
|
|
*/ |
33
|
|
|
private $controllerNameParser; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var string[] |
37
|
|
|
*/ |
38
|
|
|
private $controllerClassMap; |
39
|
|
|
|
40
|
9 |
|
public function __construct( |
41
|
|
|
ControllerResolverInterface $controllerResolver, |
42
|
|
|
ContainerInterface $container, |
43
|
|
|
ControllerNameParser $controllerNameParser |
44
|
|
|
) { |
45
|
9 |
|
$this->controllerResolver = $controllerResolver; |
46
|
9 |
|
$this->container = $container; |
47
|
9 |
|
$this->controllerNameParser = $controllerNameParser; |
48
|
9 |
|
} |
49
|
|
|
|
50
|
7 |
|
public function setControllerClassMap(array $controllerClassMap) |
51
|
|
|
{ |
52
|
7 |
|
$this->controllerClassMap = array_flip($controllerClassMap); |
53
|
7 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @return false|array|callable |
57
|
|
|
*/ |
58
|
8 |
|
public function getController(Request $request) |
59
|
|
|
{ |
60
|
8 |
|
if (! $controllerName = $request->attributes->get('_controller')) { |
61
|
1 |
|
return false; |
62
|
|
|
} |
63
|
|
|
|
64
|
7 |
|
list($class, $method) = $this->splitControllerClassAndMethod($controllerName); |
65
|
|
|
|
66
|
7 |
|
if (! isset($this->controllerClassMap[$class])) { |
67
|
5 |
|
return $this->controllerResolver->getController($request); |
68
|
|
|
} |
69
|
|
|
|
70
|
2 |
|
$controller = $this->getControllerService($class); |
71
|
2 |
|
$controller = $this->decorateControllerWithContainer($controller); |
72
|
|
|
|
73
|
2 |
|
return [$controller, $method]; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* {@inheritdoc} |
78
|
|
|
*/ |
79
|
1 |
|
public function getArguments(Request $request, $controller) |
80
|
|
|
{ |
81
|
1 |
|
return $this->controllerResolver->getArguments($request, $controller); |
|
|
|
|
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return object |
86
|
|
|
*/ |
87
|
2 |
|
private function getControllerService(string $class) |
88
|
|
|
{ |
89
|
2 |
|
$serviceName = $this->controllerClassMap[$class]; |
90
|
|
|
|
91
|
2 |
|
return $this->container->get($serviceName); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param object $controller |
96
|
|
|
* |
97
|
|
|
* @return object |
98
|
|
|
*/ |
99
|
2 |
|
private function decorateControllerWithContainer($controller) |
100
|
|
|
{ |
101
|
2 |
|
if ($controller instanceof ContainerAwareInterface) { |
102
|
1 |
|
$controller->setContainer($this->container); |
103
|
|
|
} |
104
|
|
|
|
105
|
2 |
|
return $controller; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @return string[] |
110
|
|
|
*/ |
111
|
7 |
|
private function splitControllerClassAndMethod(string $controllerName) : array |
112
|
|
|
{ |
113
|
7 |
|
if (false !== strpos($controllerName, '::')) { |
114
|
3 |
|
return explode('::', $controllerName, 2); |
115
|
4 |
|
} elseif (substr_count($controllerName, ':') === 2) { |
116
|
|
|
$controllerName = $this->controllerNameParser->parse($controllerName); |
117
|
|
|
|
118
|
|
|
return explode('::', $controllerName, 2); |
119
|
4 |
|
} elseif (false !== strpos($controllerName, ':')) { |
120
|
4 |
|
return explode(':', $controllerName, 2); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.