Skip to content

Commit 682aa78

Browse files
committed
Merge branch 'feature/configurable-fields' into develop
2 parents 9c47291 + 7fc7af9 commit 682aa78

File tree

8 files changed

+107
-17
lines changed

8 files changed

+107
-17
lines changed

docs/laravel-4/basics.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,21 @@ Now run following terminal command from your Laravel 4 App root:
4343

4444
### Configuration
4545

46-
If you want to define a fixed protocol and domain you'll need to configure this package.
46+
Here's a quick overview of the options SEO Aggregator provides:
47+
48+
* Fix Protocol and Host
49+
50+
* Eloquent Model Field Settings
51+
52+
Have a look at the file src/config/config.php for more information.
53+
54+
#### How can I customize the Configuration?
4755

4856
Publish the configuration so you can make changes:
4957

5058
php artisan config:publish hettiger/seo-aggregator
5159

52-
Once this is done you configuration file is located here:
60+
Once this is done your configuration file is located here:
5361

5462
app/config/packages/hettiger/seo-aggregator/config.php
5563

docs/laravel-4/sitemap.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,10 @@ accessed once needed.
3434
// Add the Collection with a URL Prefix (The prefix can be omitted)
3535
Sitemap::addCollection($collection, 'prefix');
3636

37-
You can do this anywhere in your App but be aware... The Eloquent Model must have a field called 'slug' and the
38-
'updated_at' field for this to work. If you run into trouble you could always do a foreach() with single Links
39-
thought...
37+
You can do this anywhere in your App but be aware... The Eloquent Model must have fields providing the data for the
38+
`<loc>...</loc>` and `<lastmod>...</lastmod>` tags. (Defaults are `'slug'` and `'updated_at'`) You can set the field names in
39+
the configuration if your database schema differs from the defaults. If you run into trouble you could always do a
40+
`foreach()` with single Links thought...
4041

4142
#### Output will be something like this:
4243

readme.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ If you have any Problems with the Documentation please open an Issue.
2525

2626
### Require with Composer – Current Suggestion:
2727

28+
// composer.json
29+
2830
"require": {
2931
"php": ">=5.3.0",
3032
"hettiger/seo-aggregator": "1.0.*",

src/Hettiger/SeoAggregator/Interfaces/SitemapInterface.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ interface SitemapInterface {
66
* @param HelpersInterface $helpers
77
* @param string $protocol
88
* @param null|string $host
9+
* @param array $field_names
910
* @return SitemapInterface
1011
*/
11-
function __construct($helpers, $protocol = 'http', $host = null);
12+
function __construct($helpers, $protocol = 'http', $host = null, $field_names = array());
1213

1314
/**
1415
* Add a link to the sitemap

src/Hettiger/SeoAggregator/Providers/SeoAggregatorServiceProvider.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@ public function register()
3737
{
3838
$protocol = Config::get('seo-aggregator::protocol');
3939
$host = Config::get('seo-aggregator::host');
40+
$field_names = Config::get('seo-aggregator::fields');
4041

41-
return new Sitemap(new Helpers, $protocol, $host);
42+
return new Sitemap(new Helpers, $protocol, $host, $field_names);
4243
});
4344

4445
App::bind('seo-aggregator.robots', function()

src/Hettiger/SeoAggregator/Sitemap.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class Sitemap implements SitemapInterface {
1414

1515
protected $protocol;
1616
protected $host;
17+
protected $field_names;
1718

1819
protected $links;
1920
protected $collections;
@@ -23,14 +24,16 @@ class Sitemap implements SitemapInterface {
2324
* @param HelpersInterface $helpers
2425
* @param string $protocol
2526
* @param null|string $host
27+
* @param array $field_names
2628
* @return Sitemap
2729
*/
28-
function __construct($helpers, $protocol = 'http', $host = null)
30+
function __construct($helpers, $protocol = 'http', $host = null, $field_names = array())
2931
{
3032
$this->helpers = $helpers;
3133

3234
$this->protocol = $protocol;
3335
$this->host = $host;
36+
$this->field_names = $field_names;
3437
}
3538

3639
/**
@@ -83,6 +86,9 @@ protected function addLine($line)
8386
*/
8487
protected function iterateCollection($collection)
8588
{
89+
$loc = $this->field_names['loc'];
90+
$lastmod = $this->field_names['lastmod'];
91+
8692
foreach ( $collection as $link ) {
8793
if ( ! is_null($link->prefix) ) {
8894
$link->prefix .= '/';
@@ -91,12 +97,12 @@ protected function iterateCollection($collection)
9197
$this->addLine('<url>');
9298
$this->addLine('<loc>');
9399
$this->addLine($this->helpers->url(
94-
$link->prefix . $link->slug,
100+
$link->prefix . $link->$loc,
95101
$this->protocol,
96102
$this->host
97103
));
98104
$this->addLine('</loc>');
99-
$this->addLine('<lastmod>' . date_format($link->updated_at, 'Y-m-d') . '</lastmod>');
105+
$this->addLine('<lastmod>' . date_format($link->$lastmod, 'Y-m-d') . '</lastmod>');
100106
$this->addLine('</url>');
101107
}
102108
}

src/config/config.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,24 @@
1919

2020
'host' => null, // 'domain.tld' or null to make the app guess
2121

22+
/*
23+
|--------------------------------------------------------------------------
24+
| Eloquent Model Field Settings
25+
|--------------------------------------------------------------------------
26+
|
27+
| When you generate a sitemap from an Eloquent Model, SEO Aggregator will
28+
| automatically get all required data from your Model. Here you may specify
29+
| the field names SEO Aggregator should be looking for. Be aware, if the
30+
| fields you set here don't exist SEO Aggregator will fail.
31+
|
32+
*/
33+
34+
'fields' => array(
35+
36+
'loc' => 'slug',
37+
'lastmod' => 'updated_at',
38+
39+
),
40+
41+
2242
);

tests/SitemapTest.php

Lines changed: 58 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,10 @@ public function test_can_request_sitemap_xml_with_multiple_links_providing_proto
152152
public function test_can_request_sitemap_xml_with_one_collection()
153153
{
154154
$this->helpers->shouldReceive('url')->andReturn('url');
155-
$sitemap = new Sitemap($this->helpers);
155+
$sitemap = new Sitemap($this->helpers, 'http', null, array(
156+
'loc' => 'slug',
157+
'lastmod' => 'updated_at'
158+
));
156159

157160
$date_time = new DateTime('now');
158161
$date_time_formatted = date_format($date_time, 'Y-m-d');
@@ -187,7 +190,10 @@ public function test_can_request_sitemap_xml_with_one_collection()
187190
public function test_can_request_sitemap_xml_with_one_collection_providing_protocol_and_host()
188191
{
189192
$helpers = new Helpers;
190-
$sitemap = new Sitemap($helpers, 'https', 'domain.tld');
193+
$sitemap = new Sitemap($helpers, 'https', 'domain.tld', array(
194+
'loc' => 'slug',
195+
'lastmod' => 'updated_at'
196+
));
191197

192198
$date_time = new DateTime('now');
193199
$date_time_formatted = date_format($date_time, 'Y-m-d');
@@ -222,7 +228,10 @@ public function test_can_request_sitemap_xml_with_one_collection_providing_proto
222228
public function test_can_request_sitemap_xml_with_one_collection_and_prefix()
223229
{
224230
$helpers = new Helpers;
225-
$sitemap = new Sitemap($helpers, 'https', 'domain.tld');
231+
$sitemap = new Sitemap($helpers, 'https', 'domain.tld', array(
232+
'loc' => 'slug',
233+
'lastmod' => 'updated_at'
234+
));
226235

227236
$date_time = new DateTime('now');
228237
$date_time_formatted = date_format($date_time, 'Y-m-d');
@@ -257,7 +266,10 @@ public function test_can_request_sitemap_xml_with_one_collection_and_prefix()
257266
public function test_can_request_sitemap_xml_with_multiple_collections()
258267
{
259268
$this->helpers->shouldReceive('url')->andReturn('url');
260-
$sitemap = new Sitemap($this->helpers);
269+
$sitemap = new Sitemap($this->helpers, 'http', null, array(
270+
'loc' => 'slug',
271+
'lastmod' => 'updated_at'
272+
));
261273

262274
$date_time = new DateTime('now');
263275
$date_time_formatted = date_format($date_time, 'Y-m-d');
@@ -312,7 +324,10 @@ public function test_can_request_sitemap_xml_with_multiple_collections()
312324
public function test_can_request_sitemap_xml_with_multiple_collections_providing_protocol_and_host()
313325
{
314326
$helpers = new Helpers;
315-
$sitemap = new Sitemap($helpers, 'https', 'domain.tld');
327+
$sitemap = new Sitemap($helpers, 'https', 'domain.tld', array(
328+
'loc' => 'slug',
329+
'lastmod' => 'updated_at'
330+
));
316331

317332
$date_time = new DateTime('now');
318333
$date_time_formatted = date_format($date_time, 'Y-m-d');
@@ -367,7 +382,10 @@ public function test_can_request_sitemap_xml_with_multiple_collections_providing
367382
public function test_can_request_sitemap_xml_with_multiple_collections_and_multiple_prefixes()
368383
{
369384
$helpers = new Helpers;
370-
$sitemap = new Sitemap($helpers, 'https', 'domain.tld');
385+
$sitemap = new Sitemap($helpers, 'https', 'domain.tld', array(
386+
'loc' => 'slug',
387+
'lastmod' => 'updated_at'
388+
));
371389

372390
$date_time = new DateTime('now');
373391
$date_time_formatted = date_format($date_time, 'Y-m-d');
@@ -422,7 +440,10 @@ public function test_can_request_sitemap_xml_with_multiple_collections_and_multi
422440
public function test_can_request_sitemap_xml_with_a_mix_of_all_features()
423441
{
424442
$helpers = new Helpers;
425-
$sitemap = new Sitemap($helpers, 'https', 'domain.tld');
443+
$sitemap = new Sitemap($helpers, 'https', 'domain.tld', array(
444+
'loc' => 'slug',
445+
'lastmod' => 'updated_at'
446+
));
426447

427448
$date_time = new DateTime('now');
428449
$date_time_formatted = date_format($date_time, 'Y-m-d');
@@ -460,4 +481,34 @@ public function test_can_request_sitemap_xml_with_a_mix_of_all_features()
460481
$this->assertEquals($e, $a);
461482
}
462483

484+
public function test_can_set_field_names()
485+
{
486+
$helpers = new Helpers;
487+
$sitemap = new Sitemap($helpers, 'http', 'domain.tld', array(
488+
'loc' => 'loc',
489+
'lastmod' => 'lastmod'
490+
));
491+
492+
$date_time = new DateTime('now');
493+
$date_time_formatted = date_format($date_time, 'Y-m-d');
494+
495+
$collection = new ArrayObject;
496+
$collection->append((object) array(
497+
'loc' => 'foo',
498+
'lastmod' => $date_time
499+
));
500+
501+
$sitemap->addCollection($collection);
502+
503+
$e = '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'
504+
. '<url>'
505+
. '<loc>http://domain.tld/foo</loc>'
506+
. '<lastmod>' . $date_time_formatted . '</lastmod>'
507+
. '</url>'
508+
. '</urlset>';
509+
$a = $sitemap->getSitemapXml();
510+
511+
$this->assertEquals($e, $a);
512+
}
513+
463514
}

0 commit comments

Comments
 (0)