Skip to content

Commit d24d49a

Browse files
committed
Merge pull request #174 from symfony-cmf/conditional-enhancer
conditional enhancer
2 parents 9d7eb43 + 339b39c commit d24d49a

File tree

3 files changed

+154
-0
lines changed

3 files changed

+154
-0
lines changed

CHANGELOG.md

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

4+
* **2016-05-11**: Added a ConditionalEnhancer that accepts pairs of request matcher
5+
and enhancer to be called if the request matcher matches the request.
6+
47
1.4.0
58
-----
69

Enhancer/ConditionalEnhancer.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony CMF package.
5+
*
6+
* (c) 2011-2015 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+
* This enhancer uses a HttpFoundation request matcher to decide which enhancer
18+
* to use.
19+
*
20+
* @author David Buchmann
21+
*/
22+
class ConditionalEnhancer implements RouteEnhancerInterface
23+
{
24+
/**
25+
* The enhancer of the first entry where the matcher matches the request is
26+
* used.
27+
*
28+
* The matcher has to implement Symfony\Component\HttpFoundation\RequestMatcherInterface.
29+
*
30+
* The enhancer has to implement RouteEnhancerInterface.
31+
*
32+
* @var array Ordered list of 'matcher', 'enhancer' pairs.
33+
*/
34+
private $enhancerMap;
35+
36+
/**
37+
* @param array $enhancerMap Ordered list of 'matcher', 'enhancer' pairs.
38+
*/
39+
public function __construct(array $enhancerMap)
40+
{
41+
$this->enhancerMap = $enhancerMap;
42+
}
43+
44+
/**
45+
* If the target field is not set but the source field is, map the field.
46+
*
47+
* {@inheritdoc}
48+
*/
49+
public function enhance(array $defaults, Request $request)
50+
{
51+
foreach ($this->enhancerMap as $pair) {
52+
if ($pair['matcher']->matches($request)) {
53+
return $pair['enhancer']->enhance($defaults, $request);
54+
}
55+
}
56+
57+
return $defaults;
58+
}
59+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony CMF package.
5+
*
6+
* (c) 2011-2015 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\Tests\Mapper;
13+
14+
use Symfony\Cmf\Component\Routing\Enhancer\ConditionalEnhancer;
15+
use Symfony\Component\HttpFoundation\Request;
16+
use Symfony\Cmf\Component\Routing\Test\CmfUnitTestCase;
17+
18+
class ConditionalEnhancerTest extends CmfUnitTestCase
19+
{
20+
/**
21+
* @var Request
22+
*/
23+
private $request;
24+
25+
public function setUp()
26+
{
27+
$this->request = Request::create('/test');
28+
}
29+
30+
public function testSecondMatch()
31+
{
32+
$defaults = array('foo' => 'bar');
33+
$expected = array('matcher' => 'found');
34+
35+
$enhancer1 = $this->buildMock('\Symfony\Cmf\Component\Routing\Enhancer\RouteEnhancerInterface');
36+
$enhancer1->expects($this->never())
37+
->method('enhance');
38+
$matcher1 = $this->buildMock('\Symfony\Component\HttpFoundation\RequestMatcherInterface');
39+
$matcher1->expects($this->once())
40+
->method('matches')
41+
->with($this->request)
42+
->will($this->returnValue(false));
43+
44+
$enhancer2 = $this->buildMock('\Symfony\Cmf\Component\Routing\Enhancer\RouteEnhancerInterface');
45+
$enhancer2->expects($this->once())
46+
->method('enhance')
47+
->with($defaults, $this->request)
48+
->will($this->returnValue($expected));
49+
$matcher2 = $this->buildMock('\Symfony\Component\HttpFoundation\RequestMatcherInterface');
50+
$matcher2->expects($this->once())
51+
->method('matches')
52+
->with($this->request)
53+
->will($this->returnValue(true));
54+
55+
$enhancer = new ConditionalEnhancer(array(
56+
array(
57+
'matcher' => $matcher1,
58+
'enhancer' => $enhancer1,
59+
),
60+
array(
61+
'matcher' => $matcher2,
62+
'enhancer' => $enhancer2,
63+
),
64+
));
65+
66+
$this->assertEquals($expected, $enhancer->enhance($defaults, $this->request));
67+
}
68+
69+
public function testNoMatch()
70+
{
71+
$defaults = array('foo' => 'bar');
72+
$expected = array('matcher' => 'found');
73+
74+
$enhancer1 = $this->buildMock('\Symfony\Cmf\Component\Routing\Enhancer\RouteEnhancerInterface');
75+
$enhancer1->expects($this->never())
76+
->method('enhance');
77+
$matcher1 = $this->buildMock('\Symfony\Component\HttpFoundation\RequestMatcherInterface');
78+
$matcher1->expects($this->once())
79+
->method('matches')
80+
->with($this->request)
81+
->will($this->returnValue(false));
82+
83+
$enhancer = new ConditionalEnhancer(array(
84+
array(
85+
'matcher' => $matcher1,
86+
'enhancer' => $enhancer1,
87+
),
88+
));
89+
90+
$this->assertEquals($defaults, $enhancer->enhance($defaults, $this->request));
91+
}
92+
}

0 commit comments

Comments
 (0)