Skip to content

Commit

Permalink
added route generation
Browse files Browse the repository at this point in the history
  • Loading branch information
wachterjohannes committed Jul 11, 2016
1 parent e606d18 commit f8f0fca
Show file tree
Hide file tree
Showing 7 changed files with 260 additions and 7 deletions.
1 change: 1 addition & 0 deletions Controller/ArticleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use JMS\Serializer\SerializationContext;
use ONGR\ElasticsearchBundle\Service\Manager;
use ONGR\ElasticsearchDSL\Query\FuzzyQuery;
use ONGR\ElasticsearchDSL\Query\MatchAllQuery;
use ONGR\ElasticsearchDSL\Query\TermQuery;
use ONGR\ElasticsearchDSL\Sort\FieldSort;
use Sulu\Bundle\ArticleBundle\Document\ArticleOngrDocument;
Expand Down
76 changes: 75 additions & 1 deletion Document/ArticleDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
namespace Sulu\Bundle\ArticleBundle\Document;

use Sulu\Bundle\ArticleBundle\Document\Behavior\DateShardingBehavior;
use Sulu\Bundle\RouteBundle\Model\RoutableInterface;
use Sulu\Bundle\RouteBundle\Model\RouteInterface;
use Sulu\Component\Content\Document\Behavior\LocalizedAuditableBehavior;
use Sulu\Component\Content\Document\Behavior\LocalizedStructureBehavior;
use Sulu\Component\Content\Document\Behavior\StructureBehavior;
Expand All @@ -35,7 +37,8 @@ class ArticleDocument implements
StructureBehavior,
LocalizedStructureBehavior,
LocalizedAuditableBehavior,
DateShardingBehavior
DateShardingBehavior,
RoutableInterface
{
/**
* @var string
Expand All @@ -62,6 +65,16 @@ class ArticleDocument implements
*/
private $title;

/**
* @var RouteInterface
*/
private $route;

/**
* @var string
*/
private $routePath;

/**
* @var string
*/
Expand Down Expand Up @@ -115,6 +128,16 @@ public function getUuid()
return $this->uuid;
}

/**
* Set uuid.
*
* @param string $uuid
*/
public function setUuid($uuid)
{
$this->uuid = $uuid;
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -163,6 +186,47 @@ public function setTitle($title)
$this->title = $title;
}

/**
* Returns route.
*
* @return RouteInterface
*/
public function getRoute()
{
return $this->route;
}

/**
* Set route.
*
* @param RouteInterface $route
*/
public function setRoute(RouteInterface $route)
{
$this->route = $route;
$this->routePath = $route->getPath();
}

/**
* Returns route-path.
*
* @return string
*/
public function getRoutePath()
{
return $this->routePath;
}

/**
* Set route-path.
*
* @param string $routePath
*/
public function setRoutePath($routePath)
{
$this->routePath = $routePath;
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -250,4 +314,14 @@ public function getChanged()
{
return $this->changed;
}

/**
* Returns identifier.
*
* @return mixed
*/
public function getId()
{
return $this->getUuid();
}
}
100 changes: 94 additions & 6 deletions Document/Subscriber/ArticleSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,20 @@

namespace Sulu\Bundle\ArticleBundle\Document\Subscriber;

use Doctrine\ORM\EntityManagerInterface;
use Sulu\Bundle\ArticleBundle\Document\ArticleDocument;
use Sulu\Bundle\ArticleBundle\Document\Index\IndexerInterface;
use Sulu\Bundle\RouteBundle\Entity\RouteRepositoryInterface;
use Sulu\Bundle\RouteBundle\Manager\RouteManagerInterface;
use Sulu\Component\DocumentManager\Event\HydrateEvent;
use Sulu\Component\DocumentManager\Event\MetadataLoadEvent;
use Sulu\Component\DocumentManager\Event\PersistEvent;
use Sulu\Component\DocumentManager\Event\RemoveEvent;
use Sulu\Component\DocumentManager\Events;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
* Indexes article on persist and removes it from index on delete.
* Indexes article and generate route on persist and removes it from index and routing on delete.
*/
class ArticleSubscriber implements EventSubscriberInterface
{
Expand All @@ -28,12 +33,37 @@ class ArticleSubscriber implements EventSubscriberInterface
*/
private $indexer;

/**
* @var RouteManagerInterface
*/
private $routeManager;

/**
* @var RouteRepositoryInterface
*/
private $routeRepository;

/**
* @var EntityManagerInterface
*/
private $entityManager;

/**
* @param IndexerInterface $indexer
* @param RouteManagerInterface $routeManager
* @param RouteRepositoryInterface $routeRepository
* @param EntityManagerInterface $entityManager
*/
public function __construct(IndexerInterface $indexer)
{
public function __construct(
IndexerInterface $indexer,
RouteManagerInterface $routeManager,
RouteRepositoryInterface $routeRepository,
EntityManagerInterface $entityManager
) {
$this->indexer = $indexer;
$this->routeManager = $routeManager;
$this->routeRepository = $routeRepository;
$this->entityManager = $entityManager;
}

/**
Expand All @@ -42,17 +72,54 @@ public function __construct(IndexerInterface $indexer)
public static function getSubscribedEvents()
{
return [
Events::PERSIST => [['handlePersist', -500]],
Events::HYDRATE => [['handleHydrate', -500]],
Events::PERSIST => [['handleIndex', -500]],
Events::PERSIST => ['handleRoute'],
Events::REMOVE => [['handleRemove', -500]],
Events::METADATA_LOAD => 'handleMetadataLoad',
];
}

/**
* Indexes article-document.
* Load route for article-document.
*
* @param HydrateEvent $event
*/
public function handleHydrate(HydrateEvent $event)
{
$document = $event->getDocument();
if (!$document instanceof ArticleDocument || null === $document->getRoutePath()) {
return;
}

$document->setRoute($this->routeRepository->findByPath($document->getRoutePath(), $event->getLocale()));
}

/**
* Generate route for article-document.
*
* @param PersistEvent $event
*/
public function handlePersist(PersistEvent $event)
public function handleRoute(PersistEvent $event)
{
$document = $event->getDocument();
if (!$document instanceof ArticleDocument || null !== $document->getRoutePath()) {
return;
}

$document->setUuid($event->getNode()->getIdentifier());

$route = $this->routeManager->create($document);
$this->entityManager->persist($route);
$this->entityManager->flush();
}

/**
* Indexes for article-document.
*
* @param PersistEvent $event
*/
public function handleIndex(PersistEvent $event)
{
$document = $event->getDocument();
if (!$document instanceof ArticleDocument) {
Expand All @@ -78,4 +145,25 @@ public function handleRemove(RemoveEvent $event)
$this->indexer->remove($document);
$this->indexer->flush();
}

/**
* Add route to metadata.
*
* @param MetadataLoadEvent $event
*/
public function handleMetadataLoad(MetadataLoadEvent $event)
{
if ($event->getMetadata()->getClass() !== ArticleDocument::class) {
return;
}

$metadata = $event->getMetadata();
$metadata->addFieldMapping(
'routePath',
[
'encoding' => 'system_localized',
'property' => 'routePath',
]
);
}
}
1 change: 1 addition & 0 deletions Resources/config/serializer/Document.ArticleDocument.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<property name="uuid" serialized-name="id" type="string" groups="defaultPage,smallPage,preview"/>
<property name="nodeName" type="string"/>
<property name="path" type="string"/>
<property name="routePath" serialized-name="route" type="string" groups="defaultPage,smallPage,preview"/>

<property name="locale" type="string" groups="preview"/>
<property name="originalLocale" type="string" groups="preview"/>
Expand Down
10 changes: 10 additions & 0 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@
<service id="sulu_article.subscriber.article"
class="Sulu\Bundle\ArticleBundle\Document\Subscriber\ArticleSubscriber">
<argument type="service" id="sulu_article.elastic_search.article_indexer"/>
<argument type="service" id="sulu_route.manager.route_manager"/>
<argument type="service" id="sulu.repository.route"/>
<argument type="service" id="doctrine.orm.entity_manager"/>

<tag name="sulu_document_manager.event_subscriber"/>
</service>
Expand All @@ -60,5 +63,12 @@
<tag name="jms_serializer.event_subscriber" />
<tag name="sulu.context" context="admin"/>
</service>
<service id="sulu_article.routing.default_provider"
class="Sulu\Bundle\ArticleBundle\Routing\ArticleRouteDefaultProvider">
<argument type="service" id="sulu_document_manager.document_manager"/>
<argument type="service" id="sulu_content.structure.factory"/>

<tag name="sulu_route.defaults_provider"/>
</service>
</services>
</container>
74 changes: 74 additions & 0 deletions Routing/ArticleRouteDefaultProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?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\Routing;

use Sulu\Bundle\ArticleBundle\Document\ArticleDocument;
use Sulu\Bundle\RouteBundle\Routing\Defaults\RouteDefaultsProviderInterface;
use Sulu\Component\Content\Metadata\Factory\StructureMetadataFactory;
use Sulu\Component\DocumentManager\DocumentManagerInterface;

/**
* Provides route-defaults for articles.
*/
class ArticleRouteDefaultProvider implements RouteDefaultsProviderInterface
{
/**
* @var DocumentManagerInterface
*/
private $documentManager;

/**
* @var StructureMetadataFactory
*/
private $structureMetadataFactory;

/**
* @param DocumentManagerInterface $documentManager
* @param StructureMetadataFactory $structureMetadataFactory
*/
public function __construct(
DocumentManagerInterface $documentManager,
StructureMetadataFactory $structureMetadataFactory
) {
$this->documentManager = $documentManager;
$this->structureMetadataFactory = $structureMetadataFactory;
}

/**
* {@inheritdoc}
*
* @param ArticleDocument $object
*/
public function getByEntity($entityClass, $id, $locale, $object = null)
{
if (!$object) {
$object = $this->documentManager->find($id, $locale);
}

$metadata = $this->structureMetadataFactory->getStructureMetadata('article', $object->getStructureType());

return [
'object' => $object,
'view' => $metadata->view,
'_cacheLifetime' => $metadata->cacheLifetime,
'_controller' => $metadata->controller,
];
}

/**
* {@inheritdoc}
*/
public function supports($entityClass)
{
return $entityClass === ArticleDocument::class;
}
}
5 changes: 5 additions & 0 deletions Tests/app/config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,8 @@ ongr_elasticsearch:
connection: default
mappings:
- SuluArticleBundle
# Sulu Routing
sulu_route:
mappings:
Sulu\Bundle\ArticleBundle\Document\ArticleDocument:
route_schema: /articles/{object.getTitle()}

0 comments on commit f8f0fca

Please sign in to comment.