|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Knp\RadBundle\View; |
|
4
|
|
|
|
|
5
|
|
|
use Knp\RadBundle\AppBundle\BundleGuesser; |
|
6
|
|
|
use Knp\RadBundle\HttpFoundation\RequestManipulator; |
|
7
|
|
|
use Knp\RadBundle\View\NameDeducer\NoControllerNameException; |
|
8
|
|
|
use Knp\RadBundle\View\NameDeducer\NotInBundleException; |
|
9
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
10
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\ControllerNameParser; |
|
11
|
|
|
use Symfony\Component\Templating\EngineInterface; |
|
12
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
13
|
|
|
|
|
14
|
|
|
class NameDeducer |
|
15
|
|
|
{ |
|
16
|
|
|
private $container; |
|
17
|
|
|
private $templating; |
|
18
|
|
|
private $parser; |
|
19
|
|
|
private $bundleGuesser; |
|
20
|
|
|
private $requestManipulator; |
|
21
|
|
|
private $engine; |
|
22
|
|
|
|
|
23
|
|
|
public function __construct(ContainerInterface $container, EngineInterface $templating, ControllerNameParser $parser, BundleGuesser $bundleGuesser, RequestManipulator $requestManipulator = null, $engine = 'twig') |
|
24
|
|
|
{ |
|
25
|
|
|
$this->container = $container; |
|
26
|
|
|
$this->templating = $templating; |
|
27
|
|
|
$this->parser = $parser; |
|
28
|
|
|
$this->bundleGuesser = $bundleGuesser; |
|
29
|
|
|
$this->requestManipulator = $requestManipulator ?: new RequestManipulator; |
|
30
|
|
|
$this->engine = $engine; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function deduce(Request $request) |
|
34
|
|
|
{ |
|
35
|
|
|
if ($this->requestManipulator->hasAttribute($request, 'view')) { |
|
36
|
|
|
$view = $this->requestManipulator->getAttribute($request, 'view'); |
|
37
|
|
|
|
|
38
|
|
|
return sprintf('%s.%s.%s', $view, $request->getRequestFormat(), $this->engine); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
return $this->deduceViewName($request); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
private function deduceViewName(Request $request) |
|
46
|
|
|
{ |
|
47
|
|
|
if (false === $this->requestManipulator->hasAttribute($request, '_controller')) { |
|
48
|
|
|
throw new NoControllerNameException(); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
$controller = $this->requestManipulator->getAttribute($request, '_controller'); |
|
52
|
|
|
|
|
53
|
|
|
if (2 === count($exploded = explode(':', $controller))) { |
|
54
|
|
|
return $this->getName($request, $this->getNormalizedNameFromService($controller)); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
if (false === strpos($controller, '::')) { |
|
58
|
|
|
return $this->getName($request, $this->parser->parse($controller)); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
return $this->getName($request, $controller); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
private function getName(Request $request, $controller) |
|
65
|
|
|
{ |
|
66
|
|
|
list($class, $method) = explode('::', $controller, 2); |
|
67
|
|
|
if (!$this->bundleGuesser->hasBundleForClass($class)) { |
|
68
|
|
|
throw new NotInBundleException($class); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
$group = preg_replace(array('#^.*\\Controller\\\\#', '#Controller$#'), '', $class); |
|
72
|
|
|
$group = str_replace('\\', '/', $group); |
|
73
|
|
|
$view = preg_replace('/Action$/', '', $method); |
|
74
|
|
|
$bundle = $this->bundleGuesser->getBundleForClass($class); |
|
75
|
|
|
|
|
76
|
|
|
return sprintf('%s:%s:%s.%s.%s', $bundle->getName(), $group, $view, $request->getRequestFormat(), $this->engine); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
private function getNormalizedNameFromService($controller) |
|
80
|
|
|
{ |
|
81
|
|
|
list($service, $method) = explode(':', $controller); |
|
82
|
|
|
$class = get_class($this->container->get($service)); |
|
83
|
|
|
|
|
84
|
|
|
return sprintf('%s::%s', $class, $method); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|