1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the DpnXmlSitemapBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) Björn Fromme <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the Resources/meta/LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Dpn\XmlSitemapBundle\Sitemap; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\Routing\Exception\MissingMandatoryParametersException; |
15
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
16
|
|
|
use Symfony\Component\Routing\RouterInterface; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @author Kevin Bond <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class RouteOptionGenerator implements GeneratorInterface |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var \Symfony\Component\Routing\RouterInterface |
25
|
|
|
*/ |
26
|
|
|
protected $router; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param \Symfony\Component\Routing\RouterInterface $router |
30
|
|
|
*/ |
31
|
9 |
|
public function __construct(RouterInterface $router) |
32
|
|
|
{ |
33
|
9 |
|
$this->router = $router; |
34
|
9 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @return Entry[] |
38
|
|
|
* |
39
|
|
|
* @throws \InvalidArgumentException |
40
|
|
|
*/ |
41
|
9 |
|
public function generate() |
42
|
|
|
{ |
43
|
9 |
|
$entries = array(); |
44
|
9 |
|
$collection = $this->router->getRouteCollection(); |
45
|
|
|
|
46
|
9 |
|
foreach ($collection->all() as $name => $route) { |
47
|
|
|
/** @var \Symfony\Component\Routing\Route $route */ |
48
|
9 |
|
$option = $route->getOption('sitemap'); |
49
|
|
|
|
50
|
9 |
|
if (true !== $option && true !== is_array($option)) { |
51
|
8 |
|
continue; |
52
|
|
|
} |
53
|
|
|
|
54
|
9 |
|
$options = array(); |
55
|
|
|
|
56
|
9 |
|
if (true === is_array($option)) { |
57
|
1 |
|
$options = $option; |
58
|
1 |
|
} |
59
|
|
|
|
60
|
|
|
try { |
61
|
9 |
|
$url = $this->router->generate($name, array(), UrlGeneratorInterface::ABSOLUTE_URL); |
62
|
9 |
|
} catch (MissingMandatoryParametersException $e) { |
63
|
1 |
|
throw new \InvalidArgumentException(sprintf('The route "%s" cannot have the sitemap option because it requires parameters', $name)); |
64
|
|
|
} |
65
|
|
|
|
66
|
8 |
|
$entries[] = new Entry( |
67
|
8 |
|
$url, |
68
|
8 |
|
true === isset($options['lastmod']) ? $options['lastmod'] : null, |
69
|
8 |
|
true === isset($options['changefreq']) ? $options['changefreq'] : null, |
70
|
8 |
|
true === isset($options['priority']) ? $options['priority'] : null |
71
|
8 |
|
); |
72
|
8 |
|
} |
73
|
|
|
|
74
|
8 |
|
return $entries; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|