-
Notifications
You must be signed in to change notification settings - Fork 80
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
Added route-behavior to article-page #131
Merged
alexander-schranz
merged 1 commit into
sulu:feature/multi-page
from
wachterjohannes:feature/multi-page-route
Apr 5, 2017
Merged
Changes from all commits
Commits
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 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
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,37 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of Sulu. | ||
* | ||
* (c) MASSIVE ART WebServices GmbH | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace Sulu\Bundle\ArticleBundle\Document\Behavior; | ||
|
||
use Sulu\Component\DocumentManager\Behavior\Mapping\ParentBehavior; | ||
use Sulu\Component\DocumentManager\Behavior\Mapping\UuidBehavior; | ||
|
||
/** | ||
* This behavior has to be attached to documents which should have a page-number property. | ||
*/ | ||
interface PageBehavior extends ParentBehavior, UuidBehavior | ||
{ | ||
/** | ||
* Returns page-number. | ||
* | ||
* @return int | ||
*/ | ||
public function getPageNumber(); | ||
|
||
/** | ||
* Set page-number. | ||
* | ||
* @param int $pageNumber | ||
* | ||
* @return self | ||
*/ | ||
public function setPageNumber($pageNumber); | ||
} |
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,135 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of Sulu. | ||
* | ||
* (c) MASSIVE ART WebServices GmbH | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace Sulu\Bundle\ArticleBundle\Document\Subscriber; | ||
|
||
use Sulu\Bundle\ArticleBundle\Document\Behavior\PageBehavior; | ||
use Sulu\Component\DocumentManager\DocumentInspector; | ||
use Sulu\Component\DocumentManager\Event\MetadataLoadEvent; | ||
use Sulu\Component\DocumentManager\Event\PersistEvent; | ||
use Sulu\Component\DocumentManager\Event\RemoveEvent; | ||
use Sulu\Component\DocumentManager\Events; | ||
use Sulu\Component\DocumentManager\PropertyEncoder; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
|
||
/** | ||
* Handles document-manager events to set and update page-numbers. | ||
*/ | ||
class PageSubscriber implements EventSubscriberInterface | ||
{ | ||
const FIELD = 'pageNumber'; | ||
|
||
/** | ||
* @var DocumentInspector | ||
*/ | ||
private $documentInspector; | ||
|
||
/** | ||
* @var PropertyEncoder | ||
*/ | ||
private $propertyEncoder; | ||
|
||
/** | ||
* @param DocumentInspector $documentInspector | ||
* @param PropertyEncoder $propertyEncoder | ||
*/ | ||
public function __construct(DocumentInspector $documentInspector, PropertyEncoder $propertyEncoder) | ||
{ | ||
$this->documentInspector = $documentInspector; | ||
$this->propertyEncoder = $propertyEncoder; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function getSubscribedEvents() | ||
{ | ||
return [ | ||
Events::PERSIST => [['handlePersist', -1024]], | ||
Events::REMOVE => [['handleRemove', 5]], | ||
Events::METADATA_LOAD => 'handleMetadataLoad', | ||
]; | ||
} | ||
|
||
/** | ||
* Add page-number to metadata. | ||
* | ||
* @param MetadataLoadEvent $event | ||
*/ | ||
public function handleMetadataLoad(MetadataLoadEvent $event) | ||
{ | ||
$metadata = $event->getMetadata(); | ||
|
||
if (false === $metadata->getReflectionClass()->isSubclassOf(PageBehavior::class)) { | ||
return; | ||
} | ||
|
||
$metadata->addFieldMapping( | ||
'pageNumber', | ||
[ | ||
'encoding' => 'system', | ||
'property' => self::FIELD, | ||
] | ||
); | ||
} | ||
|
||
/** | ||
* Set the page-number to new pages. | ||
* | ||
* @param PersistEvent $event | ||
*/ | ||
public function handlePersist(PersistEvent $event) | ||
{ | ||
$document = $event->getDocument(); | ||
if (!$document instanceof PageBehavior || $document->getPageNumber()) { | ||
return; | ||
} | ||
|
||
$parentDocument = $document->getParent(); | ||
|
||
$page = 1; | ||
foreach ($parentDocument->getChildren() as $child) { | ||
if (!$child instanceof PageBehavior) { | ||
continue; | ||
} | ||
|
||
++$page; | ||
} | ||
|
||
$childNode = $this->documentInspector->getNode($document); | ||
$childNode->setProperty($this->propertyEncoder->systemName(static::FIELD), $page); | ||
} | ||
|
||
/** | ||
* Adjust the page-numbers of siblings when removing a page. | ||
* | ||
* @param RemoveEvent $event | ||
*/ | ||
public function handleRemove(RemoveEvent $event) | ||
{ | ||
$document = $event->getDocument(); | ||
if (!$document instanceof PageBehavior) { | ||
return; | ||
} | ||
|
||
$parentDocument = $document->getParent(); | ||
|
||
$page = 1; | ||
foreach ($parentDocument->getChildren() as $child) { | ||
if (!$child instanceof PageBehavior || $child->getUuid() === $document->getUuid()) { | ||
continue; | ||
} | ||
|
||
$childNode = $this->documentInspector->getNode($child); | ||
$childNode->setProperty($this->propertyEncoder->systemName(static::FIELD), $page++); | ||
} | ||
} | ||
} |
Oops, something went wrong.
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.
would call the parameter just
schema
as we are in the route definitionThere 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.
there it is also
route_schema
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.
ok