88use Bolt \Extension \SimpleExtension ;
99use Bolt \Legacy \Content ;
1010use Carbon \Carbon ;
11+ use Silex \Application ;
1112use Silex \ControllerCollection ;
1213use Symfony \Component \HttpFoundation \Response ;
1314use Symfony \Component \Routing \Generator \UrlGeneratorInterface ;
2122class SitemapExtension extends SimpleExtension
2223{
2324 /**
24- * Route for regular sitemap.
25- *
26- * @return Response
25+ * {@inheritdoc}
2726 */
28- public function sitemap ( $ xml = false )
27+ protected function registerServices ( Application $ app )
2928 {
30- $ config = $ this ->getConfig ();
31- $ body = $ this ->renderTemplate ($ config ['template ' ], ['entries ' => $ this ->getLinks ()]);
32-
33- return new Response ($ body , Response::HTTP_OK );
29+ $ app ['sitemap.config ' ] = $ app ->share (
30+ function () {
31+ return $ this ->getConfig ();
32+ }
33+ );
34+ $ app ['sitemap.links ' ] = $ app ->share (
35+ function ($ app ) {
36+ return $ this ->getLinks ();
37+ }
38+ );
39+ $ app ['sitemap.controller ' ] = $ app ->share (
40+ function ($ app ) {
41+ return new Controller \Sitemap ();
42+ }
43+ );
3444 }
3545
3646 /**
3747 * Twig function returns sitemap.
3848 *
39- * @return Response
49+ * @return array
4050 */
4151 protected function registerTwigFunctions (){
4252 return [
4353 'sitemapEntries ' => 'twigGetLinks '
4454 ];
4555 }
4656
47- /**
48- * Route for XML based sitemap.
49- *
50- * @return Response
51- */
52- public function sitemapXml ()
53- {
54- $ config = $ this ->getConfig ();
55- $ body = $ this ->renderTemplate ($ config ['xml_template ' ], ['entries ' => $ this ->getLinks ()]);
56-
57- $ response = new Response ($ body , Response::HTTP_OK );
58- $ response ->headers ->set ('Content-Type ' , 'application/xml; charset=utf-8 ' );
59-
60- return $ response ;
61- }
62-
6357 /**
6458 * {@inheritdoc}
6559 */
@@ -72,7 +66,7 @@ protected function registerAssets()
7266 ->setCallback (function () {
7367 $ app = $ this ->getContainer ();
7468 $ snippet = sprintf (
75- '<link rel="sitemap" type="application/xml" title="Sitemap" href="%ssitemap .xml"> ' ,
69+ '<link rel="sitemap" type="application/xml" title="Sitemap" href="%s/sitemap .xml"> ' ,
7670 $ app ['url_generator ' ]->generate ('homepage ' , [], UrlGeneratorInterface::ABSOLUTE_URL )
7771 );
7872
@@ -85,17 +79,6 @@ protected function registerAssets()
8579 ];
8680 }
8781
88- /**
89- * {@inheritdoc}
90- *
91- * Set up the routes for the sitemap.
92- */
93- protected function registerFrontendRoutes (ControllerCollection $ collection )
94- {
95- $ collection ->match ('sitemap ' , [$ this , 'sitemap ' ]);
96- $ collection ->match ('sitemap.xml ' , [$ this , 'sitemapXml ' ]);
97- }
98-
9982 /**
10083 * {@inheritdoc}
10184 */
@@ -113,6 +96,30 @@ public function twigGetLinks(){
11396 return $ this ->getLinks ();
11497 }
11598
99+
100+ /**
101+ * {@inheritdoc}
102+ */
103+
104+ protected function registerTwigPaths ()
105+ {
106+ return [
107+ 'templates ' ,
108+ ];
109+ }
110+
111+ /**
112+ * {@inheritdoc}
113+ */
114+ protected function registerFrontendControllers ()
115+ {
116+ $ app = $ this ->getContainer ();
117+
118+ return [
119+ '/ ' => $ app ['sitemap.controller ' ],
120+ ];
121+ }
122+
116123 /**
117124 * Get an array of links.
118125 *
@@ -154,7 +161,7 @@ private function getLinks()
154161 ];
155162 }else {
156163 $ links [] = [
157- 'link ' => $ rootPath . $ contentType ['slug ' ],
164+ 'link ' => $ rootPath . ' / ' . $ contentType ['slug ' ],
158165 'title ' => $ contentType ['name ' ],
159166 'depth ' => 1 ,
160167 ];
0 commit comments