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\Controller; |
13
|
|
|
|
14
|
|
|
use Dpn\XmlSitemapBundle\Sitemap\SitemapManager; |
15
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
16
|
|
|
use Symfony\Component\HttpFoundation\Request; |
17
|
|
|
use Symfony\Component\HttpFoundation\Response; |
18
|
|
|
use Symfony\Component\Routing\RouterInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Controller class |
22
|
|
|
* |
23
|
|
|
* @author Björn Fromme <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
class SitemapController |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var \Dpn\XmlSitemapBundle\Sitemap\SitemapManager |
29
|
|
|
*/ |
30
|
|
|
protected $manager; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var \Symfony\Component\Routing\RouterInterface |
34
|
|
|
*/ |
35
|
|
|
protected $router; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var int |
39
|
|
|
*/ |
40
|
|
|
protected $httpCache; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param \Dpn\XmlSitemapBundle\Sitemap\SitemapManager $manager |
44
|
|
|
* @param \Symfony\Component\Routing\RouterInterface $router |
45
|
|
|
* @param int $httpCache |
46
|
|
|
*/ |
47
|
7 |
|
public function __construct(SitemapManager $manager, RouterInterface $router, $httpCache = null) |
48
|
|
|
{ |
49
|
7 |
|
$this->manager = $manager; |
50
|
7 |
|
$this->router = $router; |
51
|
7 |
|
$this->httpCache = $httpCache; |
52
|
7 |
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
56
|
|
|
*/ |
57
|
3 |
|
public function sitemapAction() |
58
|
|
|
{ |
59
|
3 |
|
if ($this->manager->getNumberOfSitemaps() > 1) { |
60
|
|
|
// redirect to /sitemap1.xml |
61
|
1 |
|
return new RedirectResponse($this->router->generate('dpn_xml_sitemap_number', array('number' => 1))); |
62
|
|
|
} |
63
|
|
|
|
64
|
2 |
|
return $this->createResponse($this->manager->renderSitemap()); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param int $number |
69
|
|
|
* |
70
|
|
|
* @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response |
71
|
|
|
*/ |
72
|
4 |
|
public function sitemapNumberAction($number) |
73
|
|
|
{ |
74
|
4 |
|
$total = $this->manager->getNumberOfSitemaps(); |
75
|
|
|
|
76
|
4 |
|
if (1 === $total) { |
77
|
|
|
// redirect to /sitemap.xml |
78
|
1 |
|
return new RedirectResponse($this->router->generate('dpn_xml_sitemap')); |
79
|
|
|
} |
80
|
|
|
|
81
|
3 |
|
if ($number > $total) { |
82
|
|
|
// redirect to /sitemap{n}.xml |
83
|
1 |
|
return new RedirectResponse($this->router->generate('dpn_xml_sitemap_number', array('number' => $total))); |
84
|
|
|
} |
85
|
|
|
|
86
|
3 |
|
return $this->createResponse($this->manager->renderSitemap($number)); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param Request $request |
91
|
|
|
* |
92
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
93
|
|
|
*/ |
94
|
2 |
|
public function sitemapIndexAction(Request $request) |
95
|
|
|
{ |
96
|
2 |
|
return $this->createResponse($this->manager->renderSitemapIndex($request->getSchemeAndHttpHost())); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param string $template |
101
|
|
|
* |
102
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
103
|
|
|
*/ |
104
|
7 |
|
protected function createResponse($template) |
105
|
|
|
{ |
106
|
7 |
|
$response = new Response($template); |
107
|
|
|
|
108
|
7 |
|
if (null !== $this->httpCache && 0 < $this->httpCache) { |
109
|
3 |
|
$response->setSharedMaxAge($this->httpCache); |
110
|
3 |
|
} |
111
|
|
|
|
112
|
7 |
|
return $response; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|