|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Knp\RadBundle\Routing\Conventional\Generator; |
|
4
|
|
|
|
|
5
|
|
|
use Knp\RadBundle\Routing\Conventional\Config; |
|
6
|
|
|
use Knp\RadBundle\Routing\Conventional\Generator; |
|
7
|
|
|
|
|
8
|
|
|
class ControllerName implements Generator |
|
9
|
|
|
{ |
|
10
|
|
|
private $map = array( |
|
11
|
|
|
'create' => 'new', |
|
12
|
|
|
'update' => 'edit', |
|
13
|
|
|
); |
|
14
|
|
|
|
|
15
|
|
|
public function __construct(array $map = null) |
|
16
|
|
|
{ |
|
17
|
|
|
$this->map = $map ?: $this->map; |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
public function generate(Config $config) |
|
21
|
|
|
{ |
|
22
|
|
|
$controller = $this->getControllerName($config); |
|
23
|
|
|
$name = isset($this->map[$config->name]) ? $this->map[$config->name] : $config->name; |
|
24
|
|
|
|
|
25
|
|
|
return sprintf('%s:%s%s', $controller, $name, $this->getActionSuffix($config)); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
private function getActionSuffix(Config $config) |
|
29
|
|
|
{ |
|
30
|
|
|
if ($config->getController()) { |
|
31
|
|
|
return 'Action'; |
|
32
|
|
|
} |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
private function getControllerName(Config $config) |
|
36
|
|
|
{ |
|
37
|
|
|
if ($config->getController()) { |
|
38
|
|
|
return $config->getController(); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
return $this->generateControllerHierarchy($config); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
private function generateControllerHierarchy(Config $config) |
|
45
|
|
|
{ |
|
46
|
|
|
$parts = array(); |
|
47
|
|
|
if ($config->parent) { |
|
48
|
|
|
$parts[] = $this->generateControllerHierarchy($config->parent); |
|
49
|
|
|
} |
|
50
|
|
|
$parts[] = $this->getPrefix($config); |
|
51
|
|
|
|
|
52
|
|
|
return implode('/', array_filter($parts)); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* prefix is the name (by default). This name can be of form: "App:Something" |
|
57
|
|
|
* |
|
58
|
|
|
* @return string |
|
59
|
|
|
**/ |
|
60
|
|
|
private function getPrefix(Config $config) |
|
61
|
|
|
{ |
|
62
|
|
|
$parts = explode(':', $config->name); |
|
63
|
|
|
|
|
64
|
|
|
if (1 === count($parts)) { |
|
65
|
|
|
return; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
if ($config->parent && 2 === count($parts)) { |
|
69
|
|
|
array_shift($parts); |
|
70
|
|
|
return implode('/', $parts); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
if (!$config->parent) { |
|
74
|
|
|
return implode(':', $parts); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|