Skip to content

Commit

Permalink
Add basic configuration for content bundle storage
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-schranz committed Mar 11, 2021
1 parent 37e9da7 commit c137268
Show file tree
Hide file tree
Showing 13 changed files with 345 additions and 70 deletions.
28 changes: 28 additions & 0 deletions Article/Domain/Model/Article.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php


namespace Sulu\Bundle\ArticleBundle\Article\Domain\Model;

use Ramsey\Uuid\Uuid;
use Sulu\Component\Persistence\Model\AuditableTrait;

class Article implements ArticleInterface
{
use AuditableTrait;

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

public function __construct(
?string $id = null
) {
$this->id = $id ?: Uuid::uuid4()->toString();
}

public function getId(): string
{
return $this->id;
}
}
13 changes: 13 additions & 0 deletions Article/Domain/Model/ArticleInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Sulu\Bundle\ArticleBundle\Article\Domain\Model;

use Sulu\Component\Persistence\Model\AuditableInterface;

interface ArticleInterface extends AuditableInterface
{
public const TEMPLATE_TYPE = 'article';
public const RESOURCE_KEY = 'article';

public function getId(): string;
}
26 changes: 25 additions & 1 deletion DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Sulu\Bundle\ArticleBundle\DependencyInjection;

use Sulu\Bundle\ArticleBundle\Article\Domain\Model\Article;
use Sulu\Bundle\ArticleBundle\Document\ArticlePageViewObject;
use Sulu\Bundle\ArticleBundle\Document\ArticleViewDocument;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
Expand All @@ -21,6 +22,9 @@
*/
class Configuration implements ConfigurationInterface
{
public const ARTICLE_STORAGE_PHPCR = 'phpcr';
public const ARTICLE_STORAGE_CONTENT_BUNDLE = 'content_bundle';

/**
* {@inheritdoc}
*/
Expand All @@ -31,7 +35,27 @@ public function getConfigTreeBuilder()

$rootNode
->children()
->scalarNode('index_name')->isRequired()->end()
->arrayNode('article')
->addDefaultsIfNotSet()
->children()
->enumNode('storage')
->values([self::ARTICLE_STORAGE_PHPCR, self::ARTICLE_STORAGE_CONTENT_BUNDLE])
->defaultValue(self::ARTICLE_STORAGE_PHPCR)
->end()
->arrayNode('objects')
->addDefaultsIfNotSet()
->children()
->arrayNode('article')
->addDefaultsIfNotSet()
->children()
->scalarNode('model')->defaultValue(Article::class)->end()
->end()
->end()
->end()
->end()
->end()
->end()
->scalarNode('index_name')->end()
->arrayNode('hosts')
->prototype('scalar')->end()
->end()
Expand Down
167 changes: 143 additions & 24 deletions DependencyInjection/SuluArticleExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Sulu\Bundle\ArticleBundle\DependencyInjection;

use Sulu\Bundle\ArticleBundle\Article\Domain\Model\ArticleInterface;
use Sulu\Bundle\ArticleBundle\Document\ArticleDocument;
use Sulu\Bundle\ArticleBundle\Document\ArticlePageDocument;
use Sulu\Bundle\ArticleBundle\Document\Form\ArticleDocumentType;
Expand All @@ -19,6 +20,7 @@
use Sulu\Bundle\ArticleBundle\Document\Structure\ArticlePageBridge;
use Sulu\Bundle\ArticleBundle\Exception\ArticlePageNotFoundException;
use Sulu\Bundle\ArticleBundle\Exception\ParameterNotAllowedException;
use Sulu\Bundle\PersistenceBundle\DependencyInjection\PersistenceExtensionTrait;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
Expand All @@ -30,12 +32,65 @@
*/
class SuluArticleExtension extends Extension implements PrependExtensionInterface
{
use PersistenceExtensionTrait;

/**
* {@inheritdoc}
*/
public function prepend(ContainerBuilder $container)
{
if ($container->hasExtension('sulu_core')) {
$configs = $container->getExtensionConfig($this->getAlias());
$resolvingBag = $container->getParameterBag();
$configs = $resolvingBag->resolveValue($configs);
$config = $this->processConfiguration(new Configuration(), $configs);

$storage = $config['article']['storage'];
$isPHPCRStorage = Configuration::ARTICLE_STORAGE_PHPCR === $storage;
$isContentBundleStorage = Configuration::ARTICLE_STORAGE_CONTENT_BUNDLE === $storage;

if ($isContentBundleStorage && $container->hasExtension('doctrine')) {
$container->prependExtensionConfig(
'doctrine',
[
'orm' => [
'mappings' => [
'SuluBundleArticle' => [
'type' => 'xml',
'prefix' => 'Sulu\Bundle\ArticleBundle\Article\Domain\Model',
'dir' => \dirname(__DIR__) . '/Resources/config/doctrine/Article',
'alias' => 'SuluArticleBundle',
'is_bundle' => false,
'mapping' => true,
],
],
],
]
);
}

if ($isContentBundleStorage && $container->hasExtension('sulu_core')) {
$container->prependExtensionConfig(
'sulu_core',
[
'content' => [
'structure' => [
'paths' => [
ArticleInterface::TEMPLATE_TYPE => [
'path' => '%kernel.project_dir%/config/templates/articles',
'type' => 'article',
],
],
'default_type' => [
ArticleInterface::TEMPLATE_TYPE => 'default',
],
],
],
]
);
}

if ($isPHPCRStorage && $container->hasExtension('sulu_core')) {
// can be removed when phpcr storage is removed
$container->prependExtensionConfig(
'sulu_core',
[
Expand All @@ -61,7 +116,8 @@ public function prepend(ContainerBuilder $container)
);
}

if ($container->hasExtension('sulu_route')) {
if ($isPHPCRStorage && $container->hasExtension('sulu_route')) {
// can be removed when phpcr storage is removed
$container->prependExtensionConfig(
'sulu_route',
[
Expand All @@ -77,6 +133,23 @@ public function prepend(ContainerBuilder $container)
);
}

if ($isContentBundleStorage && $container->hasExtension('sulu_route')) {
$container->prependExtensionConfig(
'sulu_route',
[
'mappings' => [
ArticleInterface::class => [
'generator' => 'schema',
'options' => [
'route_schema' => '/{object["title"]}',
],
'resource_key' => ArticleInterface::RESOURCE_KEY,
],
],
]
);
}

if ($container->hasExtension('jms_serializer')) {
$container->prependExtensionConfig(
'jms_serializer',
Expand All @@ -93,7 +166,8 @@ public function prepend(ContainerBuilder $container)
);
}

if ($container->hasExtension('sulu_search')) {
if ($isPHPCRStorage && $container->hasExtension('sulu_search')) {
// can be removed when phpcr storage is removed
$container->prependExtensionConfig(
'sulu_page',
[
Expand All @@ -107,7 +181,27 @@ public function prepend(ContainerBuilder $container)
);
}

if ($container->hasExtension('sulu_document_manager')) {
if ($isContentBundleStorage && $container->hasExtension('sulu_search')) {
$suluSearchConfigs = $container->getExtensionConfig('sulu_search');

foreach ($suluSearchConfigs as $suluSearchConfig) {
if (isset($suluSearchConfig['website']['indexes'])) {
$container->prependExtensionConfig(
'sulu_search',
[
'website' => [
'indexes' => [
ArticleInterface::RESOURCE_KEY => ArticleInterface::RESOURCE_KEY . '_published',
],
],
]
);
}
}
}

if ($isPHPCRStorage && $container->hasExtension('sulu_document_manager')) {
// can be removed when phpcr storage is removed
$container->prependExtensionConfig(
'sulu_document_manager',
[
Expand All @@ -130,7 +224,8 @@ public function prepend(ContainerBuilder $container)
);
}

if ($container->hasExtension('fos_js_routing')) {
if ($isPHPCRStorage && $container->hasExtension('fos_js_routing')) {
// can be removed when phpcr storage is removed
$container->prependExtensionConfig(
'fos_js_routing',
[
Expand All @@ -141,7 +236,8 @@ public function prepend(ContainerBuilder $container)
);
}

if ($container->hasExtension('fos_rest')) {
if ($isPHPCRStorage && $container->hasExtension('fos_rest')) {
// can be removed when phpcr storage is removed
$container->prependExtensionConfig(
'fos_rest',
[
Expand All @@ -155,7 +251,8 @@ public function prepend(ContainerBuilder $container)
);
}

if ($container->hasExtension('massive_build')) {
if ($isPHPCRStorage && $container->hasExtension('massive_build')) {
// can be removed when phpcr storage is removed
$container->prependExtensionConfig(
'massive_build',
[
Expand Down Expand Up @@ -246,7 +343,8 @@ public function prepend(ContainerBuilder $container)
);
}

if ($container->hasExtension('ongr_elasticsearch')) {
if ($isPHPCRStorage && $container->hasExtension('ongr_elasticsearch')) {
// can be removed when phpcr storage is removed
$configs = $container->getExtensionConfig($this->getAlias());
$config = $this->processConfiguration(new Configuration(), $configs);

Expand Down Expand Up @@ -289,41 +387,62 @@ public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);

$storage = $config['article']['storage'];
$container->setParameter('sulu_article.article_storage', $storage);
$isPHPCRStorage = Configuration::ARTICLE_STORAGE_PHPCR === $storage;
$isContentBundleStorage = Configuration::ARTICLE_STORAGE_CONTENT_BUNDLE === $storage;

$container->setParameter('sulu_article.default_main_webspace', $config['default_main_webspace']);
$container->setParameter('sulu_article.default_additional_webspaces', $config['default_additional_webspaces']);
$container->setParameter('sulu_article.types', $config['types']);
$container->setParameter('sulu_article.documents', $config['documents']);
$container->setParameter('sulu_article.view_document.article.class', $config['documents']['article']['view']);
$container->setParameter('sulu_article.display_tab_all', $config['display_tab_all']);
$container->setParameter('sulu_article.smart_content.default_limit', $config['smart_content']['default_limit']);
$container->setParameter('sulu_article.search_fields', $config['search_fields']);

if ($isPHPCRStorage) {
// can be removed when phpcr storage is removed
$container->setParameter('sulu_article.documents', $config['documents']);
$container->setParameter('sulu_article.view_document.article.class', $config['documents']['article']['view']);
}

$loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
$loader->load('services.xml');

if ($isPHPCRStorage) {
// can be removed when phpcr storage is removed
$loader->load('services.xml');
}

if ($isContentBundleStorage) {
$this->configurePersistence($config['article']['objects'], $container);
}

$bundles = $container->getParameter('kernel.bundles');
if (array_key_exists('SuluAutomationBundle', $bundles)) {
$loader->load('automation.xml');
}

$this->appendDefaultAuthor($config, $container);
$this->appendArticlePageConfig($container);
if ($isPHPCRStorage) {
// can be removed when phpcr storage is removed
$this->appendDefaultAuthor($config, $container);
$this->appendArticlePageConfig($container);

$articleDocument = ArticleDocument::class;
$articlePageDocument = ArticlePageDocument::class;
$articleDocument = ArticleDocument::class;
$articlePageDocument = ArticlePageDocument::class;

foreach ($container->getParameter('sulu_document_manager.mapping') as $mapping) {
if ('article' == $mapping['alias']) {
$articleDocument = $mapping['class'];
}
foreach ($container->getParameter('sulu_document_manager.mapping') as $mapping) {
if ('article' == $mapping['alias']) {
$articleDocument = $mapping['class'];
}

if ('article_page' == $mapping['alias']) {
$articlePageDocument = $mapping['class'];
if ('article_page' == $mapping['alias']) {
$articlePageDocument = $mapping['class'];
}
}
}

$container->setParameter('sulu_article.article_document.class', $articleDocument);
$container->setParameter('sulu_article.article_page_document.class', $articlePageDocument);
$container->setParameter('sulu_article.article_document.class', $articleDocument);
$container->setParameter('sulu_article.article_page_document.class', $articlePageDocument);
}
}

/**
Expand Down
14 changes: 14 additions & 0 deletions Resources/config/doctrine/Article/Article.orm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
<mapped-superclass
name="Sulu\Bundle\ArticleBundle\Article\Domain\Model\Article"
table="ar_articles"
repository-class="Sulu\Component\Persistence\Repository\ORM\EntityRepository"
>
<id name="id" type="string" column="id">
<generator strategy="NONE"/>
</id>
</mapped-superclass>
</doctrine-mapping>
6 changes: 6 additions & 0 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<!--
NOTE: this are the service definitions when using the "phpcr" storage
The service definitions when using the "content_bundle" storage can
be found in the other xml files in this directory.
-->

<parameters>
<parameter key="sulu_article.export.article.formats" type="collection">
<parameter key="1.2.xliff">@SuluArticle/Export/Article/1.2.xliff.twig</parameter>
Expand Down
Loading

0 comments on commit c137268

Please sign in to comment.