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