diff --git a/CHANGELOG.md b/CHANGELOG.md index 6821059b..bd4face3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,8 @@ Changelog ========= + * **2017-01-31**: Split out enhancer code from DynamicRouter into RouteEnhancerTrait for reusability. + 2.0.0-RC1 --------- diff --git a/src/DynamicRouter.php b/src/DynamicRouter.php index 89a4862f..4b1dae0a 100644 --- a/src/DynamicRouter.php +++ b/src/DynamicRouter.php @@ -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; @@ -38,6 +38,8 @@ */ class DynamicRouter implements RouterInterface, RequestMatcherInterface, ChainedRouterInterface { + use RouteEnhancerTrait; + /** * @var RequestMatcherInterface|UrlMatcherInterface */ @@ -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. @@ -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. * diff --git a/src/Enhancer/RouteEnhancerTrait.php b/src/Enhancer/RouteEnhancerTrait.php new file mode 100644 index 00000000..249c9560 --- /dev/null +++ b/src/Enhancer/RouteEnhancerTrait.php @@ -0,0 +1,109 @@ +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); + } +}