/
ControllerNameConverter.php
131 lines (112 loc) · 4.52 KB
/
ControllerNameConverter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
namespace Symfony\Bundle\FrameworkBundle\Controller;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\HttpKernel\Log\LoggerInterface;
/*
* This file is part of the Symfony framework.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
/**
* ControllerNameConverter converts controller from the short notation a:b:c
* (BlogBundle:Post:index) to a fully-qualified class::method string
* (Bundle\BlogBundle\Controller\PostController::indexAction); and the other
* way around.
*
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/
class ControllerNameConverter
{
protected $kernel;
protected $logger;
protected $namespaces;
/**
* Constructor.
*
* @param Kernel $kernel A Kernel instance
* @param LoggerInterface $logger A LoggerInterface instance
*/
public function __construct(Kernel $kernel, LoggerInterface $logger = null)
{
$this->kernel = $kernel;
$this->logger = $logger;
$this->namespaces = array_keys($kernel->getBundleDirs());
}
/**
* Converts a class::method string to the short notation a:b:c.
*
* @param string $controller A controler (class::method)
*
* @return string A short notation controller (a:b:c)
*/
public function toShortNotation($controller)
{
if (2 != count($parts = explode('::', $controller))) {
throw new \InvalidArgumentException(sprintf('The "%s" controller is not a valid class::method controller string.', $controller));
}
list($class, $method) = $parts;
if ('Action' != substr($method, -6)) {
throw new \InvalidArgumentException(sprintf('The "%s::%s" method does not look like a controller action (it does not end with Action)', $class, $method));
}
$action = substr($method, 0, -6);
if (!preg_match('/Controller\\\(.*)Controller$/', $class, $match)) {
throw new \InvalidArgumentException(sprintf('The "%s" class does not look like a controller class (it does not end with Controller)', $class));
}
$controller = $match[1];
$bundle = null;
$namespace = substr($class, 0, strrpos($class, '\\'));
foreach ($this->namespaces as $prefix) {
if (0 === $pos = strpos($namespace, $prefix)) {
// -11 to remove the \Controller suffix (11 characters)
$bundle = substr($namespace, strlen($prefix) + 1, -11);
}
}
if (null === $bundle) {
throw new \InvalidArgumentException(sprintf('The "%s" class does not belong to a known bundle namespace.', $class));
}
return $bundle.':'.$controller.':'.$action;
}
/**
* Converts a short notation a:b:c ro a class::method.
*
* @param string $controller A short notation controller (a:b:c)
*
* @param string A controler (class::method)
*/
public function fromShortNotation($controller)
{
if (3 != count($parts = explode(':', $controller))) {
throw new \InvalidArgumentException(sprintf('The "%s" controller is not a valid a:b:c controller string.', $controller));
}
list($bundle, $controller, $action) = $parts;
$bundle = strtr($bundle, array('/' => '\\'));
$class = null;
$logs = array();
foreach ($this->namespaces as $namespace) {
$try = $namespace.'\\'.$bundle.'\\Controller\\'.$controller.'Controller';
if (!class_exists($try)) {
if (null !== $this->logger) {
$logs[] = sprintf('Failed finding controller "%s:%s" from namespace "%s" (%s)', $bundle, $controller, $namespace, $try);
}
} else {
if (!$this->kernel->isClassInActiveBundle($try)) {
throw new \LogicException(sprintf('To use the "%s" controller, you first need to enable the Bundle "%s" in your Kernel class.', $try, $namespace.'\\'.$bundle));
}
$class = $try;
break;
}
}
if (null === $class) {
if (null !== $this->logger) {
foreach ($logs as $log) {
$this->logger->info($log);
}
}
throw new \InvalidArgumentException(sprintf('Unable to find controller "%s:%s".', $bundle, $controller));
}
return $class.'::'.$action.'Action';
}
}