Skip to content

Commit ea583da

Browse files
authored
Merge pull request #191 from symfony-cmf/separate-enhancer-trait
extract route enhancer into trait
2 parents 63f523a + 0cb2f53 commit ea583da

File tree

3 files changed

+114
-86
lines changed

3 files changed

+114
-86
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
Changelog
22
=========
33

4+
* **2017-01-31**: Split out enhancer code from DynamicRouter into RouteEnhancerTrait for reusability.
5+
46
2.0.0-RC1
57
---------
68

src/DynamicRouter.php

Lines changed: 3 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace Symfony\Cmf\Component\Routing;
1313

14-
use Symfony\Cmf\Component\Routing\Enhancer\RouteEnhancerInterface;
14+
use Symfony\Cmf\Component\Routing\Enhancer\RouteEnhancerTrait;
1515
use Symfony\Cmf\Component\Routing\Event\Events;
1616
use Symfony\Cmf\Component\Routing\Event\RouterGenerateEvent;
1717
use Symfony\Cmf\Component\Routing\Event\RouterMatchEvent;
@@ -38,6 +38,8 @@
3838
*/
3939
class DynamicRouter implements RouterInterface, RequestMatcherInterface, ChainedRouterInterface
4040
{
41+
use RouteEnhancerTrait;
42+
4143
/**
4244
* @var RequestMatcherInterface|UrlMatcherInterface
4345
*/
@@ -53,18 +55,6 @@ class DynamicRouter implements RouterInterface, RequestMatcherInterface, Chained
5355
*/
5456
protected $eventDispatcher;
5557

56-
/**
57-
* @var RouteEnhancerInterface[][]
58-
*/
59-
protected $enhancers = [];
60-
61-
/**
62-
* Cached sorted list of enhancers.
63-
*
64-
* @var RouteEnhancerInterface[]
65-
*/
66-
protected $sortedEnhancers = [];
67-
6858
/**
6959
* The regexp pattern that needs to be matched before a dynamic lookup is
7060
* made.
@@ -284,79 +274,6 @@ public function matchRequest(Request $request)
284274
return $this->applyRouteEnhancers($defaults, $request);
285275
}
286276

287-
/**
288-
* Apply the route enhancers to the defaults, according to priorities.
289-
*
290-
* @param array $defaults
291-
* @param Request $request
292-
*
293-
* @return array
294-
*/
295-
protected function applyRouteEnhancers($defaults, Request $request)
296-
{
297-
foreach ($this->getRouteEnhancers() as $enhancer) {
298-
$defaults = $enhancer->enhance($defaults, $request);
299-
}
300-
301-
return $defaults;
302-
}
303-
304-
/**
305-
* Add route enhancers to the router to let them generate information on
306-
* matched routes.
307-
*
308-
* The order of the enhancers is determined by the priority, the higher the
309-
* value, the earlier the enhancer is run.
310-
*
311-
* @param RouteEnhancerInterface $enhancer
312-
* @param int $priority
313-
*
314-
* @return self
315-
*/
316-
public function addRouteEnhancer(RouteEnhancerInterface $enhancer, $priority = 0)
317-
{
318-
if (empty($this->enhancers[$priority])) {
319-
$this->enhancers[$priority] = [];
320-
}
321-
322-
$this->enhancers[$priority][] = $enhancer;
323-
$this->sortedEnhancers = [];
324-
325-
return $this;
326-
}
327-
328-
/**
329-
* Sorts the enhancers and flattens them.
330-
*
331-
* @return RouteEnhancerInterface[] the enhancers ordered by priority
332-
*/
333-
public function getRouteEnhancers()
334-
{
335-
if (0 === count($this->sortedEnhancers)) {
336-
$this->sortedEnhancers = $this->sortRouteEnhancers();
337-
}
338-
339-
return $this->sortedEnhancers;
340-
}
341-
342-
/**
343-
* Sort enhancers by priority.
344-
*
345-
* The highest priority number is the highest priority (reverse sorting).
346-
*
347-
* @return RouteEnhancerInterface[] the sorted enhancers
348-
*/
349-
protected function sortRouteEnhancers()
350-
{
351-
if (0 === count($this->enhancers)) {
352-
return [];
353-
}
354-
355-
krsort($this->enhancers);
356-
357-
return call_user_func_array('array_merge', $this->enhancers);
358-
}
359-
360277
/**
361278
* Sets the request context.
362279
*
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony CMF package.
5+
*
6+
* (c) 2011-2017 Symfony CMF
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Cmf\Component\Routing\Enhancer;
13+
14+
use Symfony\Component\HttpFoundation\Request;
15+
16+
/**
17+
* Functionality to collect and apply route enhancers to a request.
18+
*
19+
* @author Tim Plunkett
20+
* @author Larry Garfield
21+
* @author David Buchmann
22+
*/
23+
trait RouteEnhancerTrait
24+
{
25+
/**
26+
* @var RouteEnhancerInterface[][]
27+
*/
28+
private $enhancers = [];
29+
30+
/**
31+
* Cached sorted list of enhancers.
32+
*
33+
* @var RouteEnhancerInterface[]
34+
*/
35+
private $sortedEnhancers = [];
36+
37+
/**
38+
* Apply the route enhancers to the defaults, according to priorities.
39+
*
40+
* @param array $defaults
41+
* @param Request $request
42+
*
43+
* @return array
44+
*/
45+
protected function applyRouteEnhancers($defaults, Request $request)
46+
{
47+
foreach ($this->getRouteEnhancers() as $enhancer) {
48+
$defaults = $enhancer->enhance($defaults, $request);
49+
}
50+
51+
return $defaults;
52+
}
53+
54+
/**
55+
* Add route enhancers to the router to let them generate information on
56+
* matched routes.
57+
*
58+
* The order of the enhancers is determined by the priority, the higher the
59+
* value, the earlier the enhancer is run.
60+
*
61+
* @param RouteEnhancerInterface $enhancer
62+
* @param int $priority
63+
*
64+
* @return self
65+
*/
66+
public function addRouteEnhancer(RouteEnhancerInterface $enhancer, $priority = 0)
67+
{
68+
if (empty($this->enhancers[$priority])) {
69+
$this->enhancers[$priority] = [];
70+
}
71+
72+
$this->enhancers[$priority][] = $enhancer;
73+
$this->sortedEnhancers = [];
74+
75+
return $this;
76+
}
77+
78+
/**
79+
* Sorts the enhancers and flattens them.
80+
*
81+
* @return RouteEnhancerInterface[] the enhancers ordered by priority
82+
*/
83+
public function getRouteEnhancers()
84+
{
85+
if (0 === count($this->sortedEnhancers)) {
86+
$this->sortedEnhancers = $this->sortRouteEnhancers();
87+
}
88+
89+
return $this->sortedEnhancers;
90+
}
91+
92+
/**
93+
* Sort enhancers by priority.
94+
*
95+
* The highest priority number is the highest priority (reverse sorting).
96+
*
97+
* @return RouteEnhancerInterface[] the sorted enhancers
98+
*/
99+
private function sortRouteEnhancers()
100+
{
101+
if (0 === count($this->enhancers)) {
102+
return [];
103+
}
104+
105+
krsort($this->enhancers);
106+
107+
return call_user_func_array('array_merge', $this->enhancers);
108+
}
109+
}

0 commit comments

Comments
 (0)