diff --git a/Enhancer/ContentRepositoryEnhancer.php b/Enhancer/ContentRepositoryEnhancer.php new file mode 100644 index 00000000..16f7fef5 --- /dev/null +++ b/Enhancer/ContentRepositoryEnhancer.php @@ -0,0 +1,68 @@ +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; + } +} diff --git a/RouteObjectInterface.php b/RouteObjectInterface.php index db25cd20..ba26c169 100644 --- a/RouteObjectInterface.php +++ b/RouteObjectInterface.php @@ -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 diff --git a/Tests/Enhancer/ContentRepositoryEnhancerTest.php b/Tests/Enhancer/ContentRepositoryEnhancerTest.php new file mode 100644 index 00000000..a7872fe8 --- /dev/null +++ b/Tests/Enhancer/ContentRepositoryEnhancerTest.php @@ -0,0 +1,80 @@ +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', + ), + ), + ); + } +}