Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
Changelog
=========

* **2017-01-31**: Split out enhancer code from DynamicRouter into RouteEnhancerTrait for reusability.

2.0.0-RC1
---------

Expand Down
89 changes: 3 additions & 86 deletions src/DynamicRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace Symfony\Cmf\Component\Routing;

use Symfony\Cmf\Component\Routing\Enhancer\RouteEnhancerInterface;
use Symfony\Cmf\Component\Routing\Enhancer\RouteEnhancerTrait;
use Symfony\Cmf\Component\Routing\Event\Events;
use Symfony\Cmf\Component\Routing\Event\RouterGenerateEvent;
use Symfony\Cmf\Component\Routing\Event\RouterMatchEvent;
Expand All @@ -38,6 +38,8 @@
*/
class DynamicRouter implements RouterInterface, RequestMatcherInterface, ChainedRouterInterface
{
use RouteEnhancerTrait;

/**
* @var RequestMatcherInterface|UrlMatcherInterface
*/
Expand All @@ -53,18 +55,6 @@ class DynamicRouter implements RouterInterface, RequestMatcherInterface, Chained
*/
protected $eventDispatcher;

/**
* @var RouteEnhancerInterface[][]
*/
protected $enhancers = [];

/**
* Cached sorted list of enhancers.
*
* @var RouteEnhancerInterface[]
*/
protected $sortedEnhancers = [];

/**
* The regexp pattern that needs to be matched before a dynamic lookup is
* made.
Expand Down Expand Up @@ -284,79 +274,6 @@ public function matchRequest(Request $request)
return $this->applyRouteEnhancers($defaults, $request);
}

/**
* Apply the route enhancers to the defaults, according to priorities.
*
* @param array $defaults
* @param Request $request
*
* @return array
*/
protected function applyRouteEnhancers($defaults, Request $request)
{
foreach ($this->getRouteEnhancers() as $enhancer) {
$defaults = $enhancer->enhance($defaults, $request);
}

return $defaults;
}

/**
* Add route enhancers to the router to let them generate information on
* matched routes.
*
* The order of the enhancers is determined by the priority, the higher the
* value, the earlier the enhancer is run.
*
* @param RouteEnhancerInterface $enhancer
* @param int $priority
*
* @return self
*/
public function addRouteEnhancer(RouteEnhancerInterface $enhancer, $priority = 0)
{
if (empty($this->enhancers[$priority])) {
$this->enhancers[$priority] = [];
}

$this->enhancers[$priority][] = $enhancer;
$this->sortedEnhancers = [];

return $this;
}

/**
* Sorts the enhancers and flattens them.
*
* @return RouteEnhancerInterface[] the enhancers ordered by priority
*/
public function getRouteEnhancers()
{
if (0 === count($this->sortedEnhancers)) {
$this->sortedEnhancers = $this->sortRouteEnhancers();
}

return $this->sortedEnhancers;
}

/**
* Sort enhancers by priority.
*
* The highest priority number is the highest priority (reverse sorting).
*
* @return RouteEnhancerInterface[] the sorted enhancers
*/
protected function sortRouteEnhancers()
{
if (0 === count($this->enhancers)) {
return [];
}

krsort($this->enhancers);

return call_user_func_array('array_merge', $this->enhancers);
}

/**
* Sets the request context.
*
Expand Down
109 changes: 109 additions & 0 deletions src/Enhancer/RouteEnhancerTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php

/*
* This file is part of the Symfony CMF package.
*
* (c) 2011-2017 Symfony CMF
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Cmf\Component\Routing\Enhancer;

use Symfony\Component\HttpFoundation\Request;

/**
* Functionality to collect and apply route enhancers to a request.
*
* @author Tim Plunkett
* @author Larry Garfield
* @author David Buchmann
*/
trait RouteEnhancerTrait
{
/**
* @var RouteEnhancerInterface[][]
*/
private $enhancers = [];

/**
* Cached sorted list of enhancers.
*
* @var RouteEnhancerInterface[]
*/
private $sortedEnhancers = [];

/**
* Apply the route enhancers to the defaults, according to priorities.
*
* @param array $defaults
* @param Request $request
*
* @return array
*/
protected function applyRouteEnhancers($defaults, Request $request)
{
foreach ($this->getRouteEnhancers() as $enhancer) {
$defaults = $enhancer->enhance($defaults, $request);
}

return $defaults;
}

/**
* Add route enhancers to the router to let them generate information on
* matched routes.
*
* The order of the enhancers is determined by the priority, the higher the
* value, the earlier the enhancer is run.
*
* @param RouteEnhancerInterface $enhancer
* @param int $priority
*
* @return self
*/
public function addRouteEnhancer(RouteEnhancerInterface $enhancer, $priority = 0)
{
if (empty($this->enhancers[$priority])) {
$this->enhancers[$priority] = [];
}

$this->enhancers[$priority][] = $enhancer;
$this->sortedEnhancers = [];

return $this;
}

/**
* Sorts the enhancers and flattens them.
*
* @return RouteEnhancerInterface[] the enhancers ordered by priority
*/
public function getRouteEnhancers()
{
if (0 === count($this->sortedEnhancers)) {
$this->sortedEnhancers = $this->sortRouteEnhancers();
}

return $this->sortedEnhancers;
}

/**
* Sort enhancers by priority.
*
* The highest priority number is the highest priority (reverse sorting).
*
* @return RouteEnhancerInterface[] the sorted enhancers
*/
private function sortRouteEnhancers()
{
if (0 === count($this->enhancers)) {
return [];
}

krsort($this->enhancers);

return call_user_func_array('array_merge', $this->enhancers);
}
}