Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
tbuteler committed Mar 6, 2016
0 parents commit e0fcdf4
Show file tree
Hide file tree
Showing 9 changed files with 287 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/vendor
composer.phar
composer.lock
14 changes: 14 additions & 0 deletions .travis.yml
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
27 changes: 27 additions & 0 deletions composer.json
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
}
17 changes: 17 additions & 0 deletions phpunit.xml
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>
80 changes: 80 additions & 0 deletions src/Controller.php
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');
}
}
69 changes: 69 additions & 0 deletions src/Sitemap.php
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();
}
}
61 changes: 61 additions & 0 deletions src/SitemapServiceProvider.php
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',
]);
}
}
16 changes: 16 additions & 0 deletions src/config.php
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 added tests/.gitkeep
Empty file.

0 comments on commit e0fcdf4

Please sign in to comment.