1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* |
5
|
|
|
* (c) Yaroslav Honcharuk <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Yarhon\RouteGuardBundle\Controller; |
12
|
|
|
|
13
|
|
|
use Symfony\Component\HttpKernel\KernelInterface; |
14
|
|
|
use Yarhon\RouteGuardBundle\Exception\InvalidArgumentException; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* ControllerNameConverter holds convert methods for controller names in deprecated formats. |
18
|
|
|
* We can't just use the original code for the following reasons: |
19
|
|
|
* - convertBundleNotation: in order not to use classes from symfony/framework-bundle |
20
|
|
|
* - convertServiceNotation: no separate converter exists in symfony/http-kernel. |
21
|
|
|
* |
22
|
|
|
* @author Yaroslav Honcharuk <[email protected]> |
23
|
|
|
* @author Fabien Potencier <[email protected]> |
24
|
|
|
* |
25
|
|
|
* @codeCoverageIgnore |
26
|
|
|
*/ |
27
|
|
|
class ControllerNameConverter |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var KernelInterface |
31
|
|
|
*/ |
32
|
|
|
private $kernel; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param KernelInterface $kernel |
36
|
|
|
*/ |
37
|
|
|
public function __construct(KernelInterface $kernel) |
38
|
|
|
{ |
39
|
|
|
$this->kernel = $kernel; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param string $controller |
44
|
|
|
* |
45
|
|
|
* @return string |
46
|
|
|
*/ |
47
|
|
|
public function convert($controller) |
48
|
|
|
{ |
49
|
|
|
$controller = $this->convertBundleNotation($controller); |
50
|
|
|
$controller = $this->convertServiceNotation($controller); |
51
|
|
|
|
52
|
|
|
return $controller; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @see \Symfony\Component\HttpKernel\Controller\ContainerControllerResolver::createController Original source |
57
|
|
|
* |
58
|
|
|
* @param string $controller A service:method notation controller |
59
|
|
|
* |
60
|
|
|
* @return string A string in the service::method notation |
61
|
|
|
*/ |
62
|
|
|
private function convertServiceNotation($controller) |
63
|
|
|
{ |
64
|
|
|
if (1 === substr_count($controller, ':')) { |
65
|
|
|
$controller = str_replace(':', '::', $controller); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return $controller; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @see \Symfony\Bundle\FrameworkBundle\Controller\ControllerResolver::createController Original source |
73
|
|
|
* |
74
|
|
|
* @param string $controller |
75
|
|
|
* |
76
|
|
|
* @return string |
77
|
|
|
* |
78
|
|
|
* @throws InvalidArgumentException |
79
|
|
|
*/ |
80
|
|
|
private function convertBundleNotation($controller) |
81
|
|
|
{ |
82
|
|
|
if (false === strpos($controller, '::') && 2 === substr_count($controller, ':')) { |
83
|
|
|
try { |
84
|
|
|
$controller = $this->parseBundleNotation($controller); |
85
|
|
|
} catch (\InvalidArgumentException $e) { |
86
|
|
|
throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $controller; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @see \Symfony\Bundle\FrameworkBundle\Controller\ControllerNameParser::parse Original source |
95
|
|
|
* |
96
|
|
|
* Converts a short notation a:b:c to a class::method. |
97
|
|
|
* |
98
|
|
|
* @param string $controller A short notation controller (a:b:c) |
99
|
|
|
* |
100
|
|
|
* @return string A string in the class::method notation |
101
|
|
|
* |
102
|
|
|
* @throws \InvalidArgumentException When the specified bundle is not enabled or the controller cannot be found |
103
|
|
|
*/ |
104
|
|
|
private function parseBundleNotation($controller) |
105
|
|
|
{ |
106
|
|
|
$parts = explode(':', $controller); |
107
|
|
|
|
108
|
|
|
$originalController = $controller; |
109
|
|
|
list($bundleName, $controller, $action) = $parts; |
110
|
|
|
$controller = str_replace('/', '\\', $controller); |
111
|
|
|
|
112
|
|
|
try { |
113
|
|
|
$bundle = $this->kernel->getBundle($bundleName); |
114
|
|
|
} catch (\InvalidArgumentException $e) { |
115
|
|
|
throw new \InvalidArgumentException(sprintf( |
116
|
|
|
'The "%s" (from the _controller value "%s") does not exist or is not enabled in your kernel!', |
117
|
|
|
$bundleName, $originalController |
118
|
|
|
), 0, $e); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
$try = $bundle->getNamespace().'\\Controller\\'.$controller.'Controller'; |
122
|
|
|
if (class_exists($try)) { |
123
|
|
|
return $try.'::'.$action.'Action'; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
throw new \InvalidArgumentException(sprintf( |
127
|
|
|
'The _controller value "%s:%s:%s" maps to a "%s" class, but this class was not found. Create this class or check the spelling of the class and its namespace.', |
128
|
|
|
$bundleName, $controller, $action, $try) |
129
|
|
|
); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|