Skip to content
Permalink
Browse files

Initial commit

  • Loading branch information...
tbuteler committed Mar 6, 2016
0 parents commit e0fcdf42d4ba10e7ae6106f8cd3d9d1aa170b3c0
Showing with 287 additions and 0 deletions.
  1. +3 −0 .gitignore
  2. +14 −0 .travis.yml
  3. +27 −0 composer.json
  4. +17 −0 phpunit.xml
  5. +80 −0 src/Controller.php
  6. +69 −0 src/Sitemap.php
  7. +61 −0 src/SitemapServiceProvider.php
  8. +16 −0 src/config.php
  9. 0 tests/.gitkeep
@@ -0,0 +1,3 @@
/vendor
composer.phar
composer.lock
@@ -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
@@ -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
}
@@ -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>
@@ -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');
}
}
@@ -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();
}
}
@@ -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',
]);
}
}
@@ -0,0 +1,16 @@
<?php
/*
|--------------------------------------------------------------------------
| Clumsy Sitemap settings
|--------------------------------------------------------------------------
|
|
*/
return [
'path' => 'Http/sitemap.php',
'middleware' => [],
];
No changes.

0 comments on commit e0fcdf4

Please sign in to comment.
You can’t perform that action at this time.