Skip to content

Commit fe6fb84

Browse files
andrey1sdbu
authored andcommitted
[Route Enhancer] create Content Repository Enhancer
1 parent da333f0 commit fe6fb84

File tree

3 files changed

+153
-0
lines changed

3 files changed

+153
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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\Cmf\Component\Routing\ContentRepositoryInterface;
15+
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
16+
use Symfony\Component\HttpFoundation\Request;
17+
18+
/**
19+
* This enhancer sets the content to target field if the route has content id.
20+
*
21+
* Works with ContentRepositoryInterface that you can search the content.
22+
*
23+
* @author Samusev Andrey
24+
*/
25+
class ContentRepositoryEnhancer implements RouteEnhancerInterface
26+
{
27+
/**
28+
* @var Repository
29+
*/
30+
private $contentRepository;
31+
32+
/**
33+
* @var string
34+
*/
35+
private $target;
36+
37+
/**
38+
* @var string
39+
*/
40+
private $source;
41+
42+
/**
43+
* @param ContentRepositoryInterface $contentRepository repository to search for the content
44+
* @param string $target the field name to set content
45+
* @param string $source the field name of the content id
46+
*/
47+
public function __construct(
48+
ContentRepositoryInterface $contentRepository,
49+
$target = RouteObjectInterface::CONTENT_OBJECT,
50+
$source = RouteObjectInterface::CONTENT_ID
51+
) {
52+
$this->contentRepository = $contentRepository;
53+
$this->target = $target;
54+
$this->source = $source;
55+
}
56+
57+
/**
58+
* {@inheritdoc}
59+
*/
60+
public function enhance(array $defaults, Request $request)
61+
{
62+
if (!isset($defaults[$this->target]) && isset($defaults[$this->source])) {
63+
$defaults[$this->target] = $this->contentRepository->findById($defaults[$this->source]);
64+
}
65+
66+
return $defaults;
67+
}
68+
}

RouteObjectInterface.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ interface RouteObjectInterface
5151
*/
5252
const CONTENT_OBJECT = '_content';
5353

54+
/**
55+
* Field name for the content id of the current route, if any.
56+
*/
57+
const CONTENT_ID = '_content_id';
58+
5459
/**
5560
* Get the content document this route entry stands for. If non-null,
5661
* the ControllerClassMapper uses it to identify a controller and
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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\Enhancer;
13+
14+
use Symfony\Cmf\Component\Routing\Enhancer\ContentRepositoryEnhancer;
15+
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
16+
use Symfony\Cmf\Component\Routing\Test\CmfUnitTestCase;
17+
use Symfony\Component\HttpFoundation\Request;
18+
19+
class ContentRepositoryEnhancerTest extends CmfUnitTestCase
20+
{
21+
/**
22+
* {@inheritdoc}
23+
*/
24+
public function setUp()
25+
{
26+
$cRepository = $this->getMock('\Symfony\Cmf\Component\Routing\ContentRepositoryInterface');
27+
$cRepository
28+
->method('findById')
29+
->will($this->returnValue('document'))
30+
;
31+
$this->mapper = new ContentRepositoryEnhancer($cRepository);
32+
33+
$this->request = Request::create('/test');
34+
}
35+
36+
/**
37+
* @dataProvider dataEnhancer
38+
*/
39+
public function testEnhancer($defaults, $expected)
40+
{
41+
$this->assertEquals($expected, $this->mapper->enhance($defaults, $this->request));
42+
}
43+
44+
/**
45+
* @return array
46+
*/
47+
public function dataEnhancer()
48+
{
49+
return array(
50+
'empty' => array(array(), array()),
51+
'with content_id' => array(
52+
array(
53+
RouteObjectInterface::CONTENT_ID => 'Simple:1',
54+
),
55+
array(
56+
RouteObjectInterface::CONTENT_ID => 'Simple:1',
57+
RouteObjectInterface::CONTENT_OBJECT => 'document',
58+
),
59+
),
60+
'with content_id and content' => array(
61+
array(
62+
RouteObjectInterface::CONTENT_ID => 'Simple:1',
63+
RouteObjectInterface::CONTENT_OBJECT => 'exist object',
64+
),
65+
array(
66+
RouteObjectInterface::CONTENT_ID => 'Simple:1',
67+
RouteObjectInterface::CONTENT_OBJECT => 'exist object',
68+
),
69+
),
70+
'with content' => array(
71+
array(
72+
RouteObjectInterface::CONTENT_OBJECT => 'exist object',
73+
),
74+
array(
75+
RouteObjectInterface::CONTENT_OBJECT => 'exist object',
76+
),
77+
),
78+
);
79+
}
80+
}

0 commit comments

Comments
 (0)