Skip to content

Commit

Permalink
Added content/view to article-view-document (#255)
Browse files Browse the repository at this point in the history
* added event-aware converter which replaces ongr converter

* added content-data to index and extracted content-proxy-factory

* fixed code-style

* added documentation

* added unreleased comment to upgrade
  • Loading branch information
wachterjohannes authored and trickreich committed Oct 11, 2017
1 parent b74491e commit 9e8dd24
Show file tree
Hide file tree
Showing 20 changed files with 706 additions and 111 deletions.
39 changes: 39 additions & 0 deletions DependencyInjection/ConverterCompilerPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?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\DependencyInjection;

use Sulu\Bundle\ArticleBundle\Elasticsearch\EventAwareConverter;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

/**
* Replaces class and add argument for replaced service "es.result_converter".
*/
class ConverterCompilerPass implements CompilerPassInterface
{
const SERVICE_ID = 'es.result_converter';

/**
* {@inheritdoc}
*/
public function process(ContainerBuilder $container)
{
if (!$container->hasDefinition(self::SERVICE_ID)) {
return;
}

$definition = $container->getDefinition(self::SERVICE_ID);
$definition->setClass(EventAwareConverter::class);
$definition->addArgument(new Reference('event_dispatcher'));
}
}
17 changes: 17 additions & 0 deletions Document/ArticlePageViewObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,21 @@ class ArticlePageViewObject
* @Property(type="integer", options={"index"="not_analyzed"})
*/
public $pageNumber;

/**
* @var string
*
* @Property(type="string", options={"index":"not_analyzed"})
*/
public $contentData;

/**
* @var \ArrayObject
*/
public $content;

/**
* @var \ArrayObject
*/
public $view;
}
71 changes: 71 additions & 0 deletions Document/ArticleViewDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,23 @@ class ArticleViewDocument implements ArticleViewDocumentInterface
*/
protected $pages = [];

/**
* @var string
*
* @Property(type="string", options={"index":"not_analyzed"})
*/
protected $contentData;

/**
* @var \ArrayObject
*/
protected $content;

/**
* @var \ArrayObject
*/
protected $view;

/**
* @param string $uuid
*/
Expand Down Expand Up @@ -733,4 +750,58 @@ public function setPages(Collection $pages)

return $this;
}

/**
* {@inheritdoc}
*/
public function getContentData()
{
return $this->contentData;
}

/**
* {@inheritdoc}
*/
public function setContentData($contentData)
{
$this->contentData = $contentData;

return $this;
}

/**
* {@inheritdoc}
*/
public function getContent()
{
return $this->content;
}

/**
* {@inheritdoc}
*/
public function setContent(\ArrayObject $content)
{
$this->content = $content;

return $this;
}

/**
* {@inheritdoc}
*/
public function getView()
{
return $this->view;
}

/**
* {@inheritdoc}
*/
public function setView(\ArrayObject $view)
{
$this->view = $view;

return $this;
}
}
48 changes: 48 additions & 0 deletions Document/ArticleViewDocumentInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -433,4 +433,52 @@ public function getPages();
* @return $this
*/
public function setPages(Collection $pages);

/**
* Returns contentData.
*
* @return string
*/
public function getContentData();

/**
* Set contentData.
*
* @param string $contentData
*
* @return $this
*/
public function setContentData($contentData);

/**
* Returns content.
*
* @return \ArrayObject
*/
public function getContent();

/**
* Set content.
*
* @param \ArrayObject $content
*
* @return \ArrayObject
*/
public function setContent(\ArrayObject $content);

/**
* Returns view.
*
* @return \ArrayObject
*/
public function getView();

/**
* Set view.
*
* @param \ArrayObject $view
*
* @return $this
*/
public function setView(\ArrayObject $view);
}
7 changes: 7 additions & 0 deletions Document/Index/ArticleIndexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
use ONGR\ElasticsearchDSL\Query\TermLevel\TermQuery;
use Sulu\Bundle\ArticleBundle\Content\PageTreeRouteContentType;
use Sulu\Bundle\ArticleBundle\Document\ArticleDocument;
use Sulu\Bundle\ArticleBundle\Document\ArticlePageDocument;
use Sulu\Bundle\ArticleBundle\Document\ArticlePageViewObject;
use Sulu\Bundle\ArticleBundle\Document\ArticleViewDocumentInterface;
use Sulu\Bundle\ArticleBundle\Document\Index\Factory\ExcerptFactory;
use Sulu\Bundle\ArticleBundle\Document\Index\Factory\SeoFactory;
Expand Down Expand Up @@ -230,6 +232,8 @@ protected function createOrUpdateArticle(
}
}

$article->setContentData(json_encode($document->getStructure()->toArray()));

$this->mapPages($document, $article);

return $article;
Expand Down Expand Up @@ -278,12 +282,15 @@ protected function findOrCreateViewDocument(ArticleDocument $document, $locale,
private function mapPages(ArticleDocument $document, ArticleViewDocumentInterface $article)
{
$pages = [];
/** @var ArticlePageDocument $child */
foreach ($document->getChildren() as $child) {
/** @var ArticlePageViewObject $page */
$pages[] = $page = $this->documentFactory->create('article_page');
$page->uuid = $child->getUuid();
$page->pageNumber = $child->getPageNumber();
$page->title = $child->getPageTitle();
$page->routePath = $child->getRoutePath();
$page->contentData = json_encode($child->getStructure()->toArray());
}

$article->setPages(new Collection($pages));
Expand Down
109 changes: 9 additions & 100 deletions Document/Serializer/ArticleWebsiteSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,11 @@
use JMS\Serializer\EventDispatcher\Events;
use JMS\Serializer\EventDispatcher\EventSubscriberInterface;
use JMS\Serializer\EventDispatcher\ObjectEvent;
use ProxyManager\Factory\LazyLoadingValueHolderFactory;
use ProxyManager\Proxy\LazyLoadingInterface;
use ProxyManager\Proxy\VirtualProxyInterface;
use Sulu\Bundle\ArticleBundle\Document\ArticleDocument;
use Sulu\Bundle\ArticleBundle\Document\ArticleInterface;
use Sulu\Bundle\ArticleBundle\Document\ArticlePageDocument;
use Sulu\Component\Content\Compat\StructureInterface;
use Sulu\Bundle\ArticleBundle\Document\Structure\ContentProxyFactory;
use Sulu\Component\Content\Compat\StructureManagerInterface;
use Sulu\Component\Content\ContentTypeManagerInterface;
use Sulu\Component\Content\Extension\ExtensionManagerInterface;
use Sulu\Component\Util\SortUtils;

Expand All @@ -36,37 +32,24 @@ class ArticleWebsiteSubscriber implements EventSubscriberInterface
*/
private $structureManager;

/**
* @var ContentTypeManagerInterface
*/
private $contentTypeManager;

/**
* @var LazyLoadingValueHolderFactory
*/
private $proxyFactory;

/**
* @var ExtensionManagerInterface
*/
private $extensionManager;

/**
* @param StructureManagerInterface $structureManager
* @param ContentTypeManagerInterface $contentTypeManager
* @param LazyLoadingValueHolderFactory $proxyFactory
* @param ExtensionManagerInterface $extensionManager
* @var ContentProxyFactory
*/
private $contentProxyFactory;

public function __construct(
StructureManagerInterface $structureManager,
ContentTypeManagerInterface $contentTypeManager,
LazyLoadingValueHolderFactory $proxyFactory,
ExtensionManagerInterface $extensionManager
ExtensionManagerInterface $extensionManager,
ContentProxyFactory $contentProxyFactory
) {
$this->structureManager = $structureManager;
$this->contentTypeManager = $contentTypeManager;
$this->proxyFactory = $proxyFactory;
$this->extensionManager = $extensionManager;
$this->contentProxyFactory = $contentProxyFactory;
}

/**
Expand Down Expand Up @@ -222,82 +205,8 @@ private function resolve(ArticleInterface $article)
$data = $article->getStructure()->toArray();

return [
'content' => $this->createContentProxy($structure, $data),
'view' => $this->createViewProxy($structure, $data),
'content' => $this->contentProxyFactory->createContentProxy($structure, $data),
'view' => $this->contentProxyFactory->createViewProxy($structure, $data),
];
}

/**
* Create content-proxy for given structure.
*
* @param StructureInterface $structure
* @param array $data
*
* @return VirtualProxyInterface
*/
private function createContentProxy($structure, $data)
{
return $this->proxyFactory->createProxy(
\ArrayObject::class,
function (
&$wrappedObject,
LazyLoadingInterface $proxy,
$method,
array $parameters,
&$initializer
) use ($structure, $data) {
$content = [];
foreach ($structure->getProperties(true) as $child) {
if (array_key_exists($child->getName(), $data)) {
$child->setValue($data[$child->getName()]);
}

$contentType = $this->contentTypeManager->get($child->getContentTypeName());
$content[$child->getName()] = $contentType->getContentData($child);
}

$initializer = null;
$wrappedObject = new \ArrayObject($content);

return true;
}
);
}

/**
* Create view-proxy for given structure.
*
* @param StructureInterface $structure
* @param array $data
*
* @return VirtualProxyInterface
*/
private function createViewProxy($structure, array $data)
{
return $this->proxyFactory->createProxy(
\ArrayObject::class,
function (
&$wrappedObject,
LazyLoadingInterface $proxy,
$method,
array $parameters,
&$initializer
) use ($structure, $data) {
$view = [];
foreach ($structure->getProperties(true) as $child) {
if (array_key_exists($child->getName(), $data)) {
$child->setValue($data[$child->getName()]);
}

$contentType = $this->contentTypeManager->get($child->getContentTypeName());
$view[$child->getName()] = $contentType->getViewData($child);
}

$initializer = null;
$wrappedObject = new \ArrayObject($view);

return true;
}
);
}
}
Loading

0 comments on commit 9e8dd24

Please sign in to comment.