From 692653249a8cbd490004c2e47704ffa35aec0537 Mon Sep 17 00:00:00 2001 From: andrey1s <“andrey@4devs.pro”> Date: Fri, 26 Feb 2016 13:41:06 +0300 Subject: [PATCH 1/2] [Enhancer] create Content Repository Enhancer --- Enhancer/ContentRepositoryEnhancer.php | 63 +++++++++++++++ RouteObjectInterface.php | 5 ++ .../ContentRepositoryEnhancerTest.php | 80 +++++++++++++++++++ 3 files changed, 148 insertions(+) create mode 100644 Enhancer/ContentRepositoryEnhancer.php create mode 100644 Tests/Enhancer/ContentRepositoryEnhancerTest.php diff --git a/Enhancer/ContentRepositoryEnhancer.php b/Enhancer/ContentRepositoryEnhancer.php new file mode 100644 index 00000000..7c6f30d6 --- /dev/null +++ b/Enhancer/ContentRepositoryEnhancer.php @@ -0,0 +1,63 @@ +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', + ), + ), + ); + } +} From e8b5b4e8b21f335b207bdedb4696e6fbec2cc69b Mon Sep 17 00:00:00 2001 From: andrey1s <“andrey@4devs.pro”> Date: Sat, 27 Feb 2016 12:30:49 +0300 Subject: [PATCH 2/2] fix cs --- Enhancer/ContentRepositoryEnhancer.php | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/Enhancer/ContentRepositoryEnhancer.php b/Enhancer/ContentRepositoryEnhancer.php index 7c6f30d6..16f7fef5 100644 --- a/Enhancer/ContentRepositoryEnhancer.php +++ b/Enhancer/ContentRepositoryEnhancer.php @@ -15,10 +15,17 @@ 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 ContentRepositoryInterface + * @var Repository */ private $contentRepository; @@ -33,24 +40,22 @@ class ContentRepositoryEnhancer implements RouteEnhancerInterface private $source; /** - * ContentRepositoryEnhancer constructor. - * - * @param ContentRepositoryInterface $contentRepository - * @param string $target - * @param string $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) - { + $source = RouteObjectInterface::CONTENT_ID + ) { $this->contentRepository = $contentRepository; $this->target = $target; $this->source = $source; } /** - *{@inheritdoc} + * {@inheritdoc} */ public function enhance(array $defaults, Request $request) {