1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sonata\SeoBundle\EventListener; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\HttpKernel\Event\FilterControllerEvent; |
6
|
|
|
use Symfony\Component\Yaml\Yaml; |
7
|
|
|
|
8
|
|
|
class ConfigurationListener |
9
|
|
|
{ |
10
|
|
|
/** @var Sonata\SeoBundle\Seo\SeoPage $sonata */ |
11
|
|
|
private $sonata; |
12
|
|
|
|
13
|
|
|
/** @var Symfony\Component\HttpKernel\Config\FileLocator $kernel */ |
14
|
|
|
private $fileLocator; |
15
|
|
|
|
16
|
|
|
/** @var Symfony\Component\HttpKernel\Log\LoggerInterface $logger */ |
17
|
|
|
private $logger; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* __construct. |
21
|
|
|
* |
22
|
|
|
* @param Symfony\Component\DependencyInjection\ContainerInterface $container |
|
|
|
|
23
|
|
|
*/ |
24
|
|
|
public function __construct( |
25
|
|
|
Symfony\Component\HttpKernel\Config\FileLocator $fileLocator, |
26
|
|
|
Sonata\SeoBundle\Seo\SeoPage $sonata, |
27
|
|
|
Symfony\Component\HttpKernel\Log\LoggerInterface $logger |
28
|
|
|
) { |
29
|
|
|
$this->sonata = $sonata; |
30
|
|
|
$this->logger = $logger; |
31
|
|
|
$this->fileLocator = $fileLocator; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* onKernelController. |
36
|
|
|
* |
37
|
|
|
* @param Symfony\Component\HttpKernel\Event\FilterControllerEvent $event |
38
|
|
|
*/ |
39
|
|
|
public function onKernelController(FilterControllerEvent $event) |
40
|
|
|
{ |
41
|
|
|
$controller = $event->getController(); |
42
|
|
|
|
43
|
|
|
// $controller passed can be either a class or a Closure. This is not |
44
|
|
|
// usual in Symfony but it may happen. If it is a class, it comes in |
45
|
|
|
// array format |
46
|
|
|
if (!is_array($controller)) { |
47
|
|
|
return; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
// Get the request with namespace and others information we need and |
51
|
|
|
// construct the bundle namespace |
52
|
|
|
$request = $this->getRequest($event); |
53
|
|
|
|
54
|
|
|
$namespace = array('_namespace', '_bundle'); |
55
|
|
|
foreach ($namespace as $key => $value) { |
56
|
|
|
$namespace[$key] = $request->attributes->get($value); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
array_unshift($namespace, '@'); |
60
|
|
|
$namespace = implode('', $namespace); |
61
|
|
|
$namespace = array($namespace, 'Resources', 'config', 'sonata.yml'); |
62
|
|
|
$namespace = implode('/', $namespace); |
63
|
|
|
|
64
|
|
|
// Get the configuration file from the configuration into the |
65
|
|
|
// corresponding bundle |
66
|
|
|
try { |
67
|
|
|
$path = $this->fileLocator->locate($namespace); |
68
|
|
|
} catch (\InvalidArgumentException $e) { |
69
|
|
|
// Nothing to do because there's no SEO file |
70
|
|
|
return; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
try { |
74
|
|
|
$config = Yaml::parse($path); |
75
|
|
|
} catch (Symfony\Component\Yaml\Exception\ParseException $e) { |
|
|
|
|
76
|
|
|
// Do nothing on error. Just log error |
77
|
|
|
$this->logger->err('YAML returns a parse error', array('exception' => $e)); |
78
|
|
|
|
79
|
|
|
return; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$controller = $request->attributes->get('_controller'); |
83
|
|
|
$action = $request->attributes->get('_action'); |
84
|
|
|
|
85
|
|
|
if (array_key_exists($controller, $config)) { |
86
|
|
|
$config = $config[$controller]; |
87
|
|
|
|
88
|
|
|
if (array_key_exists($action, $config)) { |
89
|
|
|
$config = $config[$action]; |
90
|
|
|
$title = $this->getConfiguration($config, 'title'); |
91
|
|
|
$metas = $this->getConfiguration($config, 'metas'); |
92
|
|
|
|
93
|
|
|
if (!empty($title)) { |
94
|
|
|
$this->sonata->setTitle($title); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
foreach ($metas as $key => $values) { |
98
|
|
|
foreach ($values as $head => $value) { |
99
|
|
|
$this->sonata->addMeta($key, $head, $value); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Return the configuration if exists. |
108
|
|
|
* |
109
|
|
|
* @param array $config |
110
|
|
|
* @param string $key |
111
|
|
|
* |
112
|
|
|
* @return array |
113
|
|
|
*/ |
114
|
|
|
private function getConfiguration(array $config, $key) |
115
|
|
|
{ |
116
|
|
|
if (array_key_exists($key, $config)) { |
117
|
|
|
return $config[$key]; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return array(); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Return generated request. |
125
|
|
|
* |
126
|
|
|
* @param Symfony\Component\HttpKernel\Event\FilterControllerEvent $event |
127
|
|
|
* |
128
|
|
|
* @return Symfony\Component\HttpFoundation\Request; |
|
|
|
|
129
|
|
|
*/ |
130
|
|
|
private function getRequest(FilterControllerEvent $event) |
131
|
|
|
{ |
132
|
|
|
$request = $event->getRequest(); |
133
|
|
|
|
134
|
|
|
// Get the controller full namespace in order to get informations |
135
|
|
|
$controller = $request->attributes->get('_controller'); |
136
|
|
|
$pattern = '/^(.*)\\\(.*Bundle)\\\Controller\\\(.*Controller)::(.*Action)$/'; |
137
|
|
|
|
138
|
|
|
preg_match($pattern, $controller, $matches); |
139
|
|
|
$request->attributes->set('_namespace', $matches[1]); |
140
|
|
|
$request->attributes->set('_bundle', $matches[2]); |
141
|
|
|
$request->attributes->set('_controller', $matches[3]); |
142
|
|
|
$request->attributes->set('_action', $matches[4]); |
143
|
|
|
|
144
|
|
|
return $request; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.