-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Route Enhancer] create Content Repository Enhancer
- Loading branch information
Showing
3 changed files
with
153 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
/** | ||
* @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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
), | ||
), | ||
); | ||
} | ||
} |