-
Notifications
You must be signed in to change notification settings - Fork 66
[Enhancer] create Content Repository Enhancer #168
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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', | ||
| ), | ||
| ), | ||
| ); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?