Skip to content
This repository was archived by the owner on May 3, 2019. It is now read-only.

Commit

Permalink
FEATURE: Implemented blacklist to exclude paths from seo routing
Browse files Browse the repository at this point in the history
  • Loading branch information
johannessteu authored May 4, 2018
2 parents ad805cc + 7039573 commit c65cd96
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 8 deletions.
41 changes: 41 additions & 0 deletions Classes/BlacklistTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
namespace Yeebase\SEO\Routing;

/**
* This file is part of the Yeebase.SEO.Routing package.
*
* (c) 2018 yeebase media GmbH
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/

use Neos\Flow\Annotations as Flow;
use Psr\Http\Message\UriInterface;

trait BlacklistTrait
{
/**
* @Flow\InjectConfiguration(package="Yeebase.SEO.Routing", path="blacklist")
* @var array
*/
protected $blacklist;

/**
* @param UriInterface $uri
* @return bool
*/
protected function matchesBlacklist(UriInterface $uri): bool
{
$path = $uri->getPath();
foreach ($this->blacklist as $rawPattern => $active) {
$pattern = '/' . str_replace('/', '\/', $rawPattern) . '/';
if ($active === true && preg_match($pattern, $path) === 1) {
return true;
}
}

return false;
}
}
7 changes: 4 additions & 3 deletions Classes/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
namespace Yeebase\SEO\Routing;

/**
* This file is part of the Yeebase.Readiness package.
* This file is part of the Yeebase.SEO.Routing package.
*
* (c) 2018 yeebase media GmbH
*
Expand All @@ -17,6 +17,8 @@

class Router extends \Neos\Flow\Mvc\Routing\Router
{
use BlacklistTrait;

/**
* Resolves the current uri and ensures that a trailing slash is present
*
Expand All @@ -28,8 +30,7 @@ public function resolve(ResolveContext $resolveContext): UriInterface
/** @var Uri $uri */
$uri = parent::resolve($resolveContext);

$info = pathinfo($uri);
if (!isset($info['extension'])) {
if ($this->matchesBlacklist($uri) === false && isset(pathinfo($uri)['extension']) === false) {
// $uri needs to be reparsed, because the path often contains the query
$parsedUri = new Uri((string)$uri);
$parsedUri->setPath(rtrim($parsedUri->getPath(), '/') . '/');
Expand Down
12 changes: 7 additions & 5 deletions Classes/RoutingComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
namespace Yeebase\SEO\Routing;

/**
* This file is part of the Yeebase.Readiness package.
* This file is part of the Yeebase.SEO.Routing package.
*
* (c) 2018 yeebase media GmbH
*
Expand All @@ -18,6 +18,8 @@

class RoutingComponent extends \Neos\Flow\Mvc\Routing\RoutingComponent
{
use BlacklistTrait;

/**
* The original routing component uses the concret router, not the interface
* so it has to be overwritten here
Expand All @@ -41,11 +43,11 @@ class RoutingComponent extends \Neos\Flow\Mvc\Routing\RoutingComponent
public function handle(ComponentContext $componentContext)
{
$uri = $componentContext->getHttpRequest()->getUri();
$path = $uri->getPath();

if ($this->configuration['enable'] === true && $uri->getPath()[-1] !== '/') {
$info = pathinfo($uri);
if (!isset($info['extension'])) {
$uri->setPath($uri->getPath() . '/');
if ($this->configuration['enable'] === true && $path[-1] !== '/') {
if ($this->matchesBlacklist($uri) === false && isset(pathinfo($uri)['extension']) === false) {
$uri->setPath($path . '/');
$response = $componentContext->getHttpResponse();
$response->setStatus($this->configuration['statusCode']);
$response->setHeader('Location', (string) $uri);
Expand Down
2 changes: 2 additions & 0 deletions Configuration/Settings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Yeebase:
redirect:
enable: TRUE
statusCode: 303
blacklist:
'/neos/.*': TRUE

Neos:
Flow:
Expand Down

0 comments on commit c65cd96

Please sign in to comment.