Skip to content

Commit

Permalink
Allows to have url with or without base url
Browse files Browse the repository at this point in the history
  • Loading branch information
BeneRoch committed Aug 9, 2018
1 parent 5f1535d commit 688a81e
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 25 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ properties are renderable by objects. Let's take the example below:
"footer_sitemap": {
"l10n": true,
"check_active_routes": true,
"relative_urls": false,
"objects": {
"boilerplate/object/section": {
"label": "{{title}}",
Expand Down Expand Up @@ -119,6 +120,26 @@ Given the settings above:
$builder = $container['charcoal/sitemap/builder'];
$sitemap = $builder->build('footer_sitemap'); // footer_sitemap is the ident of the settings you want.
```
You can also use the `SitemapBuilderAwareTrait`, which includes the setter and getter for the sitemap builder, in order
to use it with minimal code in every necessary class.


### Options

| Key | Values | Default | Description
|:--- |:---: |:---: |---
|`l10n` | true/false | false | Defines if the sitemap includes all the languages defined in the configurations
| `check_active_routes` | true/false | false | Checks on every object if the route is active (using isActiveRoute)
| `relative_urls` | true/false | false | Returns either the absolute URL or the relative URL
| `locale` | string | Current locale | You can choose the locale for the sitemap. (Unless l10n)
| `objects` | object class string | n/a | The objects and their configurations
| `objects` |- |- |-
| `filters` | Array | n/a | Filters for the list
| `orders` | Array | n/a | Orders for the list
| `children` | Array | n/a | Uses the same options as `objects`
| `url` | string | {{url}} | Renderable URL, in case the method is not `url`
| `label` | string | {{title}} | Renderable label, in case it's not `title`
| `data` | Array | n/a | Any order data you want in. This is recursively rendered on the object

### Sitemap.xml
This contrib provides a route for `sitemap.xml` that dynamically loads the `xml` config and outputs it
Expand Down
12 changes: 0 additions & 12 deletions src/Charcoal/Sitemap/ExampleInterface.php

This file was deleted.

31 changes: 31 additions & 0 deletions src/Charcoal/Sitemap/Mixin/SitemapBuilderAwareTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Charcoal\Sitemap\Mixin;

use Charcoal\Sitemap\Service\Builder;

trait SitemapBuilderAwareTrait
{
/**
* @var Builder
*/
protected $sitemapBuilder;

/**
* @return Builder
*/
public function sitemapBuilder()
{
return $this->sitemapBuilder;
}

/**
* @param Builder $sitemapBuilder
* @return SitemapBuilderAwareTrait
*/
public function setSitemapBuilder($sitemapBuilder)
{
$this->sitemapBuilder = $sitemapBuilder;
return $this;
}
}
49 changes: 36 additions & 13 deletions src/Charcoal/Sitemap/Service/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@

namespace Charcoal\Sitemap\Service;

use Charcoal\Factory\FactoryInterface;
use Charcoal\Loader\CollectionLoader;
use Charcoal\Object\CategoryInterface;
use Charcoal\Object\HierarchicalInterface;
use Charcoal\Object\RoutableInterface;
use Charcoal\Translator\TranslatorAwareTrait;
use Charcoal\View\ViewableInterface;
use InvalidArgumentException;
use RuntimeException;

// From 'charcoal-factory'
use Charcoal\Factory\FactoryInterface;

// From 'charcoal-core'
use Charcoal\Loader\CollectionLoader;

// From 'charcoal-object'
use Charcoal\Object\CategoryInterface;
use Charcoal\Object\HierarchicalInterface;
use Charcoal\Object\RoutableInterface;

// From 'charcoal-translator'
use Charcoal\Translator\TranslatorAwareTrait;

// From 'charcoal-view'
use Charcoal\View\ViewableInterface;

/**
* Sitemap builder from object hierarchy
Expand Down Expand Up @@ -117,6 +117,7 @@ protected function defaultOptions()
'locale' => $this->translator()->getLocale(),
'l10n' => true,
'check_active_routes' => true,
'relative_urls' => true,
'objects' => [
'label' => '{{title}}',
'url' => '{{url}}',
Expand Down Expand Up @@ -175,6 +176,9 @@ public function build($ident = 'default')
if (!isset($options['check_active_routes'])) {
$options['check_active_routes'] = $defaults['check_active_routes'];
}
if (!isset($options['relative_urls'])) {
$options['relative_urls'] = $defaults['relative_urls'];
}
$out[] = $this->buildObject($class, $options);
}

Expand All @@ -198,11 +202,13 @@ protected function buildObject($class, $options, ViewableInterface $parent = nul
}
}

// Loadin the actual objects from the predefined settings
$factory = $this->modelFactory();
$obj = $factory->create($class);

$loader = $this->collectionLoader()->setModel($obj);

// From the filters
if (isset($options['filters'])) {
$filters = $options['filters'];
if ($parent) {
Expand All @@ -211,6 +217,7 @@ protected function buildObject($class, $options, ViewableInterface $parent = nul
$loader->addFilters($filters);
}

// From the orders
if (isset($options['orders'])) {
$orders = $options['orders'];
if ($parent) {
Expand All @@ -219,6 +226,7 @@ protected function buildObject($class, $options, ViewableInterface $parent = nul
$loader->addOrders($orders);
}

// From the category / hierarchichal property
$category = ($obj instanceof CategoryInterface);
$hierarchical = ($obj instanceof HierarchicalInterface);
if ($hierarchical || $category) {
Expand All @@ -229,21 +237,26 @@ protected function buildObject($class, $options, ViewableInterface $parent = nul
}
}

// Loading
$list = $loader->load();

// Processing the objects and rendering data
$out = [];
$children = isset($options['children']) ? $options['children'] : [];
$level++;

// Options
$l10n = $options['l10n'];
$locale = $options['locale'];
$defaultLocale = $options['locale'];
$checkActiveRoutes = $options['check_active_routes'];
$relativeUrls = $options['relative_urls'];

$availableLocales = $l10n ? $this->translator()->availableLocales() : [$locale];
// Locales
$availableLocales = $l10n ? $this->translator()->availableLocales() : [$defaultLocale];

foreach ($availableLocales as $locale) {

$currentLocale = $locale;
// Get opposite languages locales
$oppositeLang = [];
foreach ($availableLocales as $l) {
if ($l == $locale) {
Expand All @@ -252,12 +265,15 @@ protected function buildObject($class, $options, ViewableInterface $parent = nul
$oppositeLang[] = $l;
}

// Set the local to the current locale before looping the list.
$this->translator()->setLocale($locale);
foreach ($list as $object) {
// When checking active routes, do not display routes that are not active
if ($checkActiveRoutes && $object instanceof RoutableInterface && !$object->isActiveRoute()) {
continue;
}

// Hierarchical (children, when defined)
$cs = [];
if (!empty($children)) {
foreach ($children as $cname => $opts) {
Expand All @@ -266,27 +282,34 @@ protected function buildObject($class, $options, ViewableInterface $parent = nul
}
}

// Url template, relative or absolute?
$urlTemplate = $relativeUrls ? $options['url'] : '{{#withBaseUrl}}' . $options['url'] . '{{/withBaseUrl}}';

$tmp = [
'label' => trim($this->renderData($object, $options['label'])),
'url' => trim($this->renderData($object, '{{#withBaseUrl}}' . $options['url'] . '{{/withBaseUrl}}')),
'url' => trim($this->renderData($object, $urlTemplate)),
'children' => $cs,
'data' => $this->renderData($object, $options['data']),
'level' => $level,
'lang' => $locale
];

// If you need a priority, fix your own rules
$priority = '';
if (isset($options['priority']) && $options['priority']) {
$priority = $this->renderData($object, (string)$options['priority']);
}
$tmp['priority'] = $priority;

// If you need a date of last modification, fix your own rules
$last = '';
if (isset($options['last_modified']) && $options['last_modified']) {
$last = $this->renderData($object, $options['last_modified']);
}
$tmp['last_modified'] = $last;

// Opposite Languages
// Meant to be alternate, thus the lack of data rendering
$alternates = [];
foreach ($oppositeLang as $ol) {
$this->translator()->setLocale($ol);
Expand All @@ -302,15 +325,15 @@ protected function buildObject($class, $options, ViewableInterface $parent = nul

$tmp['alternates'] = $alternates;

$this->translator()->setLocale($currentLocale);
$this->translator()->setLocale($locale);
$out[] = $tmp;
}
}


return $out;
}

/**
* Recursive data renderer
*
Expand Down

0 comments on commit 688a81e

Please sign in to comment.