Skip to content

Commit 961be0a

Browse files
committed
Extract sitemap generation to dedicated classes
1 parent ce5af80 commit 961be0a

File tree

6 files changed

+381
-83
lines changed

6 files changed

+381
-83
lines changed

app/Controllers/SitemapController.php

Lines changed: 9 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -2,97 +2,28 @@
22

33
namespace Controllers;
44

5-
use Illuminate\Support\Facades\App;
6-
use Illuminate\Support\Facades\URL;
7-
use Illuminate\Support\Facades\Response;
8-
use Tricks\Repositories\TagRepositoryInterface;
9-
use Tricks\Repositories\TrickRepositoryInterface;
10-
use Tricks\Repositories\CategoryRepositoryInterface;
5+
use Tricks\Services\Sitemap\Builder;
116

127
class SitemapController extends BaseController
138
{
149
/**
15-
* Trick repository.
10+
* Sitemap builder instance.
1611
*
17-
* @var \Tricks\Repositories\TrickRepositoryInterface
12+
* @var \Tricks\Services\Sitemap\Builder
1813
*/
19-
protected $tricks;
20-
21-
/**
22-
* Tag repository.
23-
*
24-
* @var \Tricks\Repositories\TagRepositoryInterface
25-
*/
26-
protected $tags;
27-
28-
/**
29-
* Category repository.
30-
*
31-
* @var \Tricks\Repositories\CategoryRepositoryInterface
32-
*/
33-
protected $categories;
34-
35-
/**
36-
* Sitemap.
37-
*
38-
* @var \Tricks\Repositories\TrickRepositoryInterface
39-
*/
40-
protected $sitemap;
14+
protected $builder;
4115

4216
/**
4317
* Create a new SitemapController instance.
4418
*
45-
* @param \Tricks\Repositories\TrickRepositoryInterface $tricks
19+
* @param \Tricks\Services\Sitemap\Builder $builder
4620
* @return void
4721
*/
48-
public function __construct(
49-
TrickRepositoryInterface $tricks,
50-
CategoryRepositoryInterface $categories,
51-
TagRepositoryInterface $tags
52-
) {
53-
parent::__construct();
54-
55-
$this->tricks = $tricks;
56-
$this->tags = $tags;
57-
$this->categories = $categories;
58-
59-
$this->sitemap = App::make("sitemap");
60-
}
61-
62-
/**
63-
* Add URLs to static pages.
64-
*
65-
* @return void
66-
*/
67-
public function addStaticRoutes()
68-
{
69-
$this->sitemap->add(URL::to('/'), '2013-11-16T12:30:00+02:00', '1.0', 'daily');
70-
$this->sitemap->add(URL::to('about'), '2013-11-16T12:30:00+02:00', '0.7', 'monthly');
71-
$this->sitemap->add(URL::to('categories'), '2013-11-16T12:30:00+02:00', '0.7', 'monthly');
72-
$this->sitemap->add(URL::to('tags'), '2013-11-16T12:30:00+02:00', '0.7', 'monthly');
73-
$this->sitemap->add(URL::to('login'), '2013-11-16T12:30:00+02:00', '0.8', 'weekly');
74-
$this->sitemap->add(URL::to('register'), '2013-11-16T12:30:00+02:00', '0.8', 'weekly');
75-
76-
return $this->sitemap;
77-
}
78-
79-
/**
80-
* Add URLs to dynamic pages of the site.
81-
*
82-
* @return void
83-
*/
84-
public function addDynamicRoutes()
22+
public function __construct(Builder $builder)
8523
{
86-
$tricks = $this->tricks->findAllForSitemap();
87-
foreach($tricks as $trick) $this->sitemap->add(URL::to("tricks/{$trick->slug}"), $trick->created_at, '0.9', 'weekly');
88-
89-
$categories = $this->categories->findAll();
90-
foreach($categories as $category) $this->sitemap->add(URL::to("categories/{$category->slug}"), $category->created_at, '0.9', 'daily');
91-
92-
$tags = $this->tags->findAll();
93-
foreach($tags as $tag) $this->sitemap->add(URL::to("tags/{$tag->slug}"), $tag->created_at, '0.9', 'daily');
24+
parent::__construct();
9425

95-
return $this->sitemap;
26+
$this->builder = $builder;
9627
}
9728

9829
/**
@@ -102,9 +33,6 @@ public function addDynamicRoutes()
10233
*/
10334
public function getIndex()
10435
{
105-
$sitemap = $this->addStaticRoutes();
106-
$sitemap = $this->addDynamicRoutes();
107-
108-
return $sitemap->render('xml');
36+
return $this->builder->render();
10937
}
11038
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Tricks\Providers;
4+
5+
use Illuminate\Support\ServiceProvider;
6+
use Tricks\Services\Sitemap\Builder;
7+
use Tricks\Services\Sitemap\DataProvider;
8+
9+
class SitemapServiceProvider extends ServiceProvider
10+
{
11+
/**
12+
* Register the service provider.
13+
*
14+
* @return void
15+
*/
16+
public function register()
17+
{
18+
$this->app->alias('sitemap', 'Roumen\Sitemap\Sitemap');
19+
}
20+
}
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
<?php
2+
3+
namespace Tricks\Services\Sitemap;
4+
5+
use Roumen\Sitemap\Sitemap;
6+
use Illuminate\Config\Repository;
7+
8+
class Builder
9+
{
10+
/**
11+
* The type of sitemap to build.
12+
*
13+
* @var string
14+
*/
15+
protected $type = 'xml';
16+
17+
/**
18+
* Config repository instance.
19+
*
20+
* @var \Illuminate\Config\Repository
21+
*/
22+
protected $config;
23+
24+
/**
25+
* The sitemap generator instance.
26+
*
27+
* @var \Roumen\Sitemap\Sitemap
28+
*/
29+
protected $sitemap;
30+
31+
/**
32+
* The data provider instance.
33+
*
34+
* @var \Tricks\Services\Sitemap\DataProvider
35+
*/
36+
protected $provider;
37+
38+
/**
39+
* Create a new sitemap builder instance.
40+
*
41+
* @param \Roumen\Sitemap\Sitemap $sitemap
42+
* @param \Tricks\Services\Sitemap\DataProvider $provider
43+
* @param \Illuminate\Config\Repository $config
44+
* @return void
45+
*/
46+
public function __construct(Sitemap $sitemap, DataProvider $provider, Repository $config)
47+
{
48+
$this->sitemap = $sitemap;
49+
$this->provider = $provider;
50+
$this->config = $config;
51+
}
52+
53+
/**
54+
* Set the type of sitemap to build.
55+
*
56+
* @param string $type
57+
* @return void
58+
*/
59+
public function setType($type)
60+
{
61+
$this->type = strtolower($type);
62+
}
63+
64+
/**
65+
* Build the sitemap.
66+
*
67+
* @return \Illuminate\Http\Response
68+
*/
69+
public function render()
70+
{
71+
$this->addStaticPages();
72+
73+
foreach ($this->getTypes() as $type => $config) {
74+
$this->addDynamicData($type, $config);
75+
}
76+
77+
return $this->sitemap->render($this->type);
78+
}
79+
80+
/**
81+
* Add the static pages to the sitemap
82+
*
83+
* @return void
84+
*/
85+
protected function addStaticPages()
86+
{
87+
$pages = $this->provider->getStaticPages();
88+
89+
foreach ($pages as $page) {
90+
$this->sitemap->add($page['url'], null, $page['priority'], $page['freq']);
91+
}
92+
}
93+
94+
/**
95+
* Get the dynamic data types.
96+
*
97+
* @return array
98+
*/
99+
protected function getTypes()
100+
{
101+
return $this->config->get('sitemap');
102+
}
103+
104+
/**
105+
* Add the dynamic data of the given type to the sitemap.
106+
*
107+
* @param string $type
108+
* @param array $config
109+
* @return void
110+
*/
111+
protected function addDynamicData($type, $config)
112+
{
113+
foreach ($this->getItems($type) as $item) {
114+
$url = $this->getItemUrl($item, $type);
115+
$lastMod = $item->{$config['lastMod']};
116+
117+
$this->sitemap->add($url, $lastMod, $config['priority'], $config['freq']);
118+
}
119+
}
120+
121+
/**
122+
* Get the dynamic items from the data provider.
123+
*
124+
* @param string $type
125+
* @return \Illuminate\Support\Collection
126+
*/
127+
protected function getItems($type)
128+
{
129+
$method = $this->getDataMethodName($type);
130+
131+
return $this->provider->$method();
132+
}
133+
134+
/**
135+
* Get the name of the data method.
136+
*
137+
* @param string $type
138+
* @return string
139+
*/
140+
protected function getDataMethodName($type)
141+
{
142+
return 'get' . studly_case($type);
143+
}
144+
145+
/**
146+
* Get the url of the given item.
147+
*
148+
* @param mixed $item
149+
* @param string $type
150+
* @return string
151+
*/
152+
protected function getItemUrl($item, $type)
153+
{
154+
$method = $this->getUrlMethodName($type);
155+
156+
return $this->provider->$method($item);
157+
}
158+
159+
/**
160+
* Get the name of the url method.
161+
*
162+
* @param string $type
163+
* @return string
164+
*/
165+
protected function getUrlMethodName($type)
166+
{
167+
return 'get' . studly_case(str_singular($type)) . 'Url';
168+
}
169+
}

0 commit comments

Comments
 (0)