forked from tbuteler/clumsy-sitemap
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit e0fcdf4
Showing
9 changed files
with
287 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
/vendor | ||
composer.phar | ||
composer.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
language: php | ||
|
||
php: | ||
- 5.3 | ||
- 5.4 | ||
- 5.5 | ||
- 5.6 | ||
- hhvm | ||
|
||
before_script: | ||
- composer self-update | ||
- composer install --prefer-source --no-interaction --dev | ||
|
||
script: phpunit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{ | ||
"name": "clumsy/sitemap", | ||
"description": "Sitemap generator for Laravel projects", | ||
"license": "proprietary", | ||
"authors": [ | ||
{ | ||
"name": "Tomas Buteler", | ||
"email": "tbuteler@gmail.com" | ||
} | ||
], | ||
"require": { | ||
"lib-curl": "*", | ||
"laravel/framework": ">=5.1" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"Clumsy\\Sitemap\\": "src/" | ||
} | ||
}, | ||
"extra": { | ||
"branch-alias": { | ||
"dev-master": "0.1.0-dev" | ||
} | ||
}, | ||
"minimum-stability": "dev", | ||
"prefer-stable": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit backupGlobals="false" | ||
backupStaticAttributes="false" | ||
colors="true" | ||
convertErrorsToExceptions="true" | ||
convertNoticesToExceptions="true" | ||
convertWarningsToExceptions="true" | ||
processIsolation="false" | ||
stopOnFailure="false" | ||
syntaxCheck="false" | ||
> | ||
<testsuites> | ||
<testsuite name="Package Test Suite"> | ||
<directory suffix=".php">./tests/</directory> | ||
</testsuite> | ||
</testsuites> | ||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php | ||
|
||
namespace Clumsy\Sitemap; | ||
|
||
use Illuminate\Routing\Controller as BaseController; | ||
|
||
class Controller extends BaseController | ||
{ | ||
protected $sitemap; | ||
|
||
protected $groups; | ||
|
||
public function __construct() | ||
{ | ||
$this->sitemap = new Sitemap; | ||
} | ||
|
||
protected function missing() | ||
{ | ||
return abort(404); | ||
} | ||
|
||
protected function addLink($link, $lastmod = null, $priority = null, $changefreq = null) | ||
{ | ||
$this->sitemap->addLink($link, $lastmod, $priority, $changefreq); | ||
} | ||
|
||
protected function addGroup(array $group) | ||
{ | ||
// If array is not associative and has a link key, then use the whole group as links array | ||
$links = array_get($group, 'links', $group); | ||
|
||
$lastmod = array_get($group, 'lastmod'); | ||
$priority = array_get($group, 'priority'); | ||
$changefreq = array_get($group, 'changefreq'); | ||
|
||
foreach ($links as $link) { | ||
$this->addLink($link, $lastmod, $priority, $changefreq); | ||
} | ||
} | ||
|
||
protected function parseGroups() | ||
{ | ||
if (array_get($this->groups, 'links')) { | ||
return $this->addGroup($this->groups); | ||
} | ||
|
||
foreach ($this->groups as $group) { | ||
|
||
if (is_array($group)) { | ||
$this->addGroup($group); | ||
continue; | ||
} | ||
|
||
$this->addLink($group); | ||
} | ||
} | ||
|
||
public function render() | ||
{ | ||
$path = app_path(config('clumsy.sitemap.path')); | ||
|
||
try { | ||
|
||
$this->groups = require $path; | ||
|
||
} catch (\Exception $e) { | ||
|
||
return $this->missing(); | ||
} | ||
|
||
if (!is_array($this->groups) || !count($this->groups)) { | ||
return $this->missing(); | ||
} | ||
|
||
$this->parseGroups(); | ||
|
||
return response($this->sitemap)->header('Content-Type', 'application/xml'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
namespace Clumsy\Sitemap; | ||
|
||
class Sitemap | ||
{ | ||
protected $xml; | ||
|
||
protected $schemas = [ | ||
'xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"', | ||
'xmlns:image="http://www.google.com/schemas/sitemap-image/1.1"', | ||
'xmlns:video="http://www.google.com/schemas/sitemap-video/1.1"', | ||
]; | ||
|
||
protected $links = []; | ||
|
||
protected function wrap($string, $tag) | ||
{ | ||
return "<{$tag}>{$string}</{$tag}>"; | ||
} | ||
|
||
public function addSchema($schema) | ||
{ | ||
$this->schemas[] = $schema; | ||
} | ||
|
||
public function addLink($link, $lastmod = null, $priority = null, $changefreq = null) | ||
{ | ||
$this->links[] = compact('link', 'lastmod', 'priority', 'changefreq'); | ||
} | ||
|
||
public function renderLink(array $link) | ||
{ | ||
$xml = $this->wrap(array_get($link, 'link'), 'loc'); | ||
|
||
$optional = [ | ||
'lastmod', | ||
'changefreq', | ||
'priority', | ||
]; | ||
|
||
foreach ($optional as $key) { | ||
if (array_get($link, $key)) { | ||
$xml .= $this->wrap($link[$key], $key); | ||
} | ||
} | ||
|
||
return $this->wrap($xml, 'url'); | ||
} | ||
|
||
public function render() | ||
{ | ||
$this->xml = '<?xml version="1.0" encoding="UTF-8"?>'; | ||
$this->xml .= '<urlset '.implode(' ', $this->schemas).'>'; | ||
|
||
foreach ($this->links as $link) { | ||
$this->xml .= $this->renderLink($link); | ||
} | ||
|
||
$this->xml .= '</urlset>'; | ||
|
||
return $this->xml; | ||
} | ||
|
||
public function __toString() | ||
{ | ||
return $this->render(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
namespace Clumsy\Sitemap; | ||
|
||
use Exception; | ||
use Illuminate\Support\ServiceProvider; | ||
|
||
class SitemapServiceProvider extends ServiceProvider | ||
{ | ||
/** | ||
* Indicates if loading of the provider is deferred. | ||
* | ||
* @var bool | ||
*/ | ||
protected $defer = false; | ||
|
||
protected $endpoint; | ||
|
||
/** | ||
* Register the service provider. | ||
* | ||
* @return void | ||
*/ | ||
public function register() | ||
{ | ||
$this->mergeConfigFrom(__DIR__.'/config.php', 'clumsy.sitemap'); | ||
} | ||
|
||
/** | ||
* Boot the service provider. | ||
* | ||
* @return void | ||
*/ | ||
public function boot() | ||
{ | ||
$this->publishes([ | ||
__DIR__.'/config.php' => config_path('clumsy/sitemap.php'), | ||
], 'config'); | ||
|
||
$this->registerRoute(); | ||
} | ||
|
||
/** | ||
* Get the services provided by the provider. | ||
* | ||
* @return array | ||
*/ | ||
public function provides() | ||
{ | ||
return []; | ||
} | ||
|
||
public function registerRoute() | ||
{ | ||
$this->app['router']->get('sitemap.xml', [ | ||
'as' => 'clumsy.sitemap', | ||
'middleware' => $this->app['config']->get('clumsy.sitemap.middleware'), | ||
'uses' => '\Clumsy\Sitemap\Controller@render', | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
/* | ||
|-------------------------------------------------------------------------- | ||
| Clumsy Sitemap settings | ||
|-------------------------------------------------------------------------- | ||
| | ||
| | ||
*/ | ||
|
||
return [ | ||
|
||
'path' => 'Http/sitemap.php', | ||
|
||
'middleware' => [], | ||
]; |
Empty file.