Skip to content
Closed
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
68 changes: 68 additions & 0 deletions Enhancer/ContentRepositoryEnhancer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

/*
* This file is part of the Symfony CMF package.
*
* (c) 2011-2015 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\Cmf\Component\Routing\ContentRepositoryInterface;
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
use Symfony\Component\HttpFoundation\Request;

/**
* This enhancer sets the content to target field if the route has content id.
*
* Works with ContentRepositoryInterface that you can search the content.
*
* @author Samusev Andrey
*/
class ContentRepositoryEnhancer implements RouteEnhancerInterface
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add a short description what this enhancer is doing

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add rules for the styleci.io?

{
/**
* @var Repository
*/
private $contentRepository;

/**
* @var string
*/
private $target;

/**
* @var string
*/
private $source;

/**
* @param ContentRepositoryInterface $contentRepository repository to search for the content
* @param string $target the field name to set content
* @param string $source the field name of the content id
*/
public function __construct(
ContentRepositoryInterface $contentRepository,
$target = RouteObjectInterface::CONTENT_OBJECT,
$source = RouteObjectInterface::CONTENT_ID
) {
$this->contentRepository = $contentRepository;
$this->target = $target;
$this->source = $source;
}

/**
* {@inheritdoc}
*/
public function enhance(array $defaults, Request $request)
{
if (!isset($defaults[$this->target]) && isset($defaults[$this->source])) {
$defaults[$this->target] = $this->contentRepository->findById($defaults[$this->source]);
}

return $defaults;
}
}
5 changes: 5 additions & 0 deletions RouteObjectInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ interface RouteObjectInterface
*/
const CONTENT_OBJECT = '_content';

/**
* Field name for the content id of the current route, if any.
*/
const CONTENT_ID = '_content_id';

/**
* Get the content document this route entry stands for. If non-null,
* the ControllerClassMapper uses it to identify a controller and
Expand Down
80 changes: 80 additions & 0 deletions Tests/Enhancer/ContentRepositoryEnhancerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

/*
* This file is part of the Symfony CMF package.
*
* (c) 2011-2015 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\Tests\Enhancer;

use Symfony\Cmf\Component\Routing\Enhancer\ContentRepositoryEnhancer;
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
use Symfony\Cmf\Component\Routing\Test\CmfUnitTestCase;
use Symfony\Component\HttpFoundation\Request;

class ContentRepositoryEnhancerTest extends CmfUnitTestCase
{
/**
* {@inheritdoc}
*/
public function setUp()
{
$cRepository = $this->getMock('\Symfony\Cmf\Component\Routing\ContentRepositoryInterface');
$cRepository
->method('findById')
->will($this->returnValue('document'))
;
$this->mapper = new ContentRepositoryEnhancer($cRepository);

$this->request = Request::create('/test');
}

/**
* @dataProvider dataEnhancer
*/
public function testEnhancer($defaults, $expected)
{
$this->assertEquals($expected, $this->mapper->enhance($defaults, $this->request));
}

/**
* @return array
*/
public function dataEnhancer()
{
return array(
'empty' => array(array(), array()),
'with content_id' => array(
array(
RouteObjectInterface::CONTENT_ID => 'Simple:1',
),
array(
RouteObjectInterface::CONTENT_ID => 'Simple:1',
RouteObjectInterface::CONTENT_OBJECT => 'document',
),
),
'with content_id and content' => array(
array(
RouteObjectInterface::CONTENT_ID => 'Simple:1',
RouteObjectInterface::CONTENT_OBJECT => 'exist object',
),
array(
RouteObjectInterface::CONTENT_ID => 'Simple:1',
RouteObjectInterface::CONTENT_OBJECT => 'exist object',
),
),
'with content' => array(
array(
RouteObjectInterface::CONTENT_OBJECT => 'exist object',
),
array(
RouteObjectInterface::CONTENT_OBJECT => 'exist object',
),
),
);
}
}