Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
wachterjohannes committed Mar 28, 2017
2 parents 81dc65d + 56c18d9 commit 671b011
Show file tree
Hide file tree
Showing 19 changed files with 579 additions and 333 deletions.
35 changes: 32 additions & 3 deletions Controller/WebsiteArticleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@

use JMS\Serializer\SerializationContext;
use Sulu\Bundle\ArticleBundle\Document\ArticleDocument;
use Sulu\Component\HttpCache\HttpCache;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

/**
Expand All @@ -22,12 +24,15 @@
class WebsiteArticleController extends Controller
{
/**
* @param string $view
* Article index action.
*
* @param Request $request
* @param ArticleDocument $object
* @param string $view
*
* @return Response
*/
public function indexAction($view, ArticleDocument $object)
public function indexAction(Request $request, ArticleDocument $object, $view)
{
$content = $this->get('jms_serializer')->serialize(
$object,
Expand All @@ -40,7 +45,31 @@ public function indexAction($view, ArticleDocument $object)

return $this->render(
$view . '.html.twig',
$content
$this->get('sulu_website.resolver.template_attribute')->resolve($content),
$this->createResponse($request)
);
}

/**
* Create response.
*
* @param Request $request
*
* @return Response
*/
private function createResponse(Request $request)
{
$response = new Response();
$cacheLifetime = $request->attributes->get('_cacheLifetime');

if ($cacheLifetime) {
$response->setPublic();
$response->headers->set(
HttpCache::HEADER_REVERSE_PROXY_TTL,
$cacheLifetime
);
}

return $response;
}
}
16 changes: 5 additions & 11 deletions Document/ArticleDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
namespace Sulu\Bundle\ArticleBundle\Document;

use Sulu\Bundle\ArticleBundle\Document\Behavior\DateShardingBehavior;
use Sulu\Bundle\RouteBundle\Model\RoutableInterface;
use Sulu\Bundle\ArticleBundle\Document\Behavior\RoutableBehavior;
use Sulu\Bundle\RouteBundle\Model\RouteInterface;
use Sulu\Component\Content\Document\Behavior\AuthorBehavior;
use Sulu\Component\Content\Document\Behavior\ExtensionBehavior;
Expand Down Expand Up @@ -44,7 +44,7 @@ class ArticleDocument implements
LocalizedStructureBehavior,
LocalizedAuditableBehavior,
DateShardingBehavior,
RoutableInterface,
RoutableBehavior,
ExtensionBehavior,
WorkflowStageBehavior,
VersionBehavior,
Expand Down Expand Up @@ -178,9 +178,7 @@ public function getUuid()
}

/**
* Set uuid.
*
* @param string $uuid
* {@inheritdoc}
*/
public function setUuid($uuid)
{
Expand Down Expand Up @@ -257,19 +255,15 @@ public function setRoute(RouteInterface $route)
}

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

/**
* Set route-path.
*
* @param string $routePath
* {@inheritdoc}
*/
public function setRoutePath($routePath)
{
Expand Down
43 changes: 43 additions & 0 deletions Document/Behavior/RoutableBehavior.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?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\Bundle\RouteBundle\Model\RoutableInterface;
use Sulu\Component\DocumentManager\Behavior\Mapping\LocaleBehavior;
use Sulu\Component\DocumentManager\Behavior\Mapping\UuidBehavior;

/**
* This behavior has to be attached to documents which should have a sulu-route.
*/
interface RoutableBehavior extends RoutableInterface, UuidBehavior, LocaleBehavior
{
/**
* Returns route-path.
*
* @return string
*/
public function getRoutePath();

/**
* Set route-path.
*
* @param string $routePath
*/
public function setRoutePath($routePath);

/**
* Set uuid.
*
* @param string $uuid
*/
public function setUuid($uuid);
}
129 changes: 3 additions & 126 deletions Document/Subscriber/ArticleSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,11 @@

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\DocumentManagerInterface;
use Sulu\Component\DocumentManager\Event\AbstractMappingEvent;
use Sulu\Component\DocumentManager\Event\ConfigureOptionsEvent;
use Sulu\Component\DocumentManager\Event\FlushEvent;
use Sulu\Component\DocumentManager\Event\MetadataLoadEvent;
use Sulu\Component\DocumentManager\Event\RemoveEvent;
use Sulu\Component\DocumentManager\Event\UnpublishEvent;
use Sulu\Component\DocumentManager\Events;
Expand All @@ -41,21 +36,6 @@ class ArticleSubscriber implements EventSubscriberInterface
*/
private $liveIndexer;

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

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

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

/**
* @var DocumentManagerInterface
*/
Expand All @@ -74,24 +54,15 @@ class ArticleSubscriber implements EventSubscriberInterface
/**
* @param IndexerInterface $indexer
* @param IndexerInterface $liveIndexer
* @param RouteManagerInterface $routeManager
* @param RouteRepositoryInterface $routeRepository
* @param EntityManagerInterface $entityManager
* @param DocumentManagerInterface $documentManager
*/
public function __construct(
IndexerInterface $indexer,
IndexerInterface $liveIndexer,
RouteManagerInterface $routeManager,
RouteRepositoryInterface $routeRepository,
EntityManagerInterface $entityManager,
DocumentManagerInterface $documentManager
) {
$this->indexer = $indexer;
$this->liveIndexer = $liveIndexer;
$this->routeManager = $routeManager;
$this->routeRepository = $routeRepository;
$this->entityManager = $entityManager;
$this->documentManager = $documentManager;
}

Expand All @@ -101,78 +72,15 @@ public function __construct(
public static function getSubscribedEvents()
{
return [
Events::HYDRATE => [['handleHydrate', -500]],
Events::PERSIST => [['handleRouteUpdate', 1], ['handleRoute', 0], ['handleScheduleIndex', -500]],
Events::PERSIST => [['handleScheduleIndex', -500]],
Events::REMOVE => [['handleRemove', -500], ['handleRemoveLive', -500]],
Events::METADATA_LOAD => 'handleMetadataLoad',
Events::PUBLISH => [['handleScheduleIndexLive', 0], ['handleScheduleIndex', 0]],
Events::UNPUBLISH => 'handleUnpublish',
Events::CONFIGURE_OPTIONS => 'configureOptions',
Events::REMOVE_DRAFT => ['handleScheduleIndex', -1024],
Events::FLUSH => [['handleFlush', -2048], ['handleFlushLive', -2048]],
];
}

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

$route = $this->routeRepository->findByPath($document->getRoutePath(), $document->getOriginalLocale());
if (!$route) {
return;
}

$document->setRoute($route);
}

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

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

$route = $this->routeManager->create($document, $event->getOption('route_path'));
$this->entityManager->persist($route);
$this->entityManager->flush();
}

/**
* Update route for article-document if route-path was changed.
*
* @param AbstractMappingEvent $event
*/
public function handleRouteUpdate(AbstractMappingEvent $event)
{
$document = $event->getDocument();
if (!$document instanceof ArticleDocument
|| null === $document->getRoute()
|| null === ($routePath = $event->getOption('route_path'))
) {
return;
}

$route = $this->routeManager->update($document, $routePath);
$document->setRoutePath($route->getPath());
$this->entityManager->persist($route);
$this->entityManager->flush();
}

/**
* Schedule article document for index.
*
Expand Down Expand Up @@ -222,7 +130,8 @@ public function handleFlush(FlushEvent $event)

foreach ($this->documents as $document) {
$this->indexer->index(
$this->documentManager->find($document['uuid'],
$this->documentManager->find(
$document['uuid'],
$document['locale']
)
);
Expand Down Expand Up @@ -319,36 +228,4 @@ public function handleRemoveLive($event)
$this->liveIndexer->remove($document);
$this->liveIndexer->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',
]
);
}

/**
* Add route-path to options.
*
* @param ConfigureOptionsEvent $event
*/
public function configureOptions(ConfigureOptionsEvent $event)
{
$options = $event->getOptions();
$options->setDefaults(['route_path' => null]);
}
}
Loading

0 comments on commit 671b011

Please sign in to comment.