From a796d8a9e861a4853265be8a47e04508721d1da0 Mon Sep 17 00:00:00 2001 From: Alexander Schranz Date: Fri, 12 Mar 2021 11:54:28 +0100 Subject: [PATCH] Improve code readability --- DependencyInjection/SuluArticleExtension.php | 408 +++++++++--------- SuluArticleBundle.php | 42 +- Tests/Application/Kernel.php | 4 +- ...al.yml => config_experimental_storage.yml} | 0 ...fig_phpcr.yml => config_phpcr_storage.yml} | 0 5 files changed, 243 insertions(+), 211 deletions(-) rename Tests/Application/config/{config_experimental.yml => config_experimental_storage.yml} (100%) rename Tests/Application/config/{config_phpcr.yml => config_phpcr_storage.yml} (100%) diff --git a/DependencyInjection/SuluArticleExtension.php b/DependencyInjection/SuluArticleExtension.php index 4f7136fe..6d0b9156 100644 --- a/DependencyInjection/SuluArticleExtension.php +++ b/DependencyInjection/SuluArticleExtension.php @@ -28,7 +28,9 @@ use Symfony\Component\HttpKernel\DependencyInjection\Extension; /** - * This is the class that loads and manages article bundle configuration. + * @internal + * + * This is the class that loads and manages article bundle configuration */ class SuluArticleExtension extends Extension implements PrependExtensionInterface { @@ -45,22 +47,16 @@ public function prepend(ContainerBuilder $container) $config = $this->processConfiguration(new Configuration(), $configs); $storage = $config['article']['storage']; - $isPHPCRStorage = Configuration::ARTICLE_STORAGE_PHPCR === $storage; - $isExperimentalStorage = Configuration::ARTICLE_STORAGE_EXPERIMENTAL === $storage; - if ($isExperimentalStorage && $container->hasExtension('doctrine')) { + if ($container->hasExtension('jms_serializer')) { $container->prependExtensionConfig( - 'doctrine', + 'jms_serializer', [ - '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, + 'metadata' => [ + 'directories' => [ + 'sulu_article' => [ + 'path' => __DIR__ . '/../Resources/config/serializer', + 'namespace_prefix' => 'Sulu\Bundle\ArticleBundle', ], ], ], @@ -68,20 +64,65 @@ public function prepend(ContainerBuilder $container) ); } - if ($isExperimentalStorage && $container->hasExtension('sulu_core')) { + if ($container->hasExtension('sulu_admin')) { $container->prependExtensionConfig( - 'sulu_core', + 'sulu_admin', [ - 'content' => [ - 'structure' => [ - 'paths' => [ - ArticleInterface::TEMPLATE_TYPE => [ - 'path' => '%kernel.project_dir%/config/templates/articles', - 'type' => 'article', + 'lists' => [ + 'directories' => [ + __DIR__ . '/../Resources/config/lists', + ], + ], + 'forms' => [ + 'directories' => [ + __DIR__ . '/../Resources/config/forms', + ], + ], + 'resources' => [ + 'articles' => [ + 'routes' => [ + 'list' => 'sulu_article.get_articles', + 'detail' => 'sulu_article.get_article', + ], + ], + 'article_versions' => [ + 'routes' => [ + 'list' => 'sulu_article.get_article_versions', + 'detail' => 'sulu_article.post_article_version_trigger', + ], + ], + ], + 'field_type_options' => [ + 'selection' => [ + 'article_selection' => [ + 'default_type' => 'list_overlay', + 'resource_key' => 'articles', + 'types' => [ + 'list_overlay' => [ + 'adapter' => 'table', + 'list_key' => 'articles', + 'display_properties' => ['title', 'routePath'], + 'icon' => 'su-newspaper', + 'label' => 'sulu_article.selection_label', + 'overlay_title' => 'sulu_article.selection_overlay_title', + ], ], ], - 'default_type' => [ - ArticleInterface::TEMPLATE_TYPE => 'default', + ], + 'single_selection' => [ + 'single_article_selection' => [ + 'default_type' => 'list_overlay', + 'resource_key' => 'articles', + 'types' => [ + 'list_overlay' => [ + 'adapter' => 'table', + 'list_key' => 'articles', + 'display_properties' => ['title'], + 'empty_text' => 'sulu_article.no_article_selected', + 'icon' => 'su-newspaper', + 'overlay_title' => 'sulu_article.single_selection_overlay_title', + ], + ], ], ], ], @@ -89,7 +130,19 @@ public function prepend(ContainerBuilder $container) ); } - if ($isPHPCRStorage && $container->hasExtension('sulu_core')) { + if (Configuration::ARTICLE_STORAGE_PHPCR === $storage) { + $this->prependPHPCRStorage($container); + } elseif (Configuration::ARTICLE_STORAGE_EXPERIMENTAL === $storage) { + $this->prependExperimentalStorage($container); + } + } + + /** + * Can be removed when phpcr storage is removed. + */ + private function prependPHPCRStorage(ContainerBuilder $container): void + { + if ($container->hasExtension('sulu_core')) { // can be removed when phpcr storage is removed $container->prependExtensionConfig( 'sulu_core', @@ -116,7 +169,7 @@ public function prepend(ContainerBuilder $container) ); } - if ($isPHPCRStorage && $container->hasExtension('sulu_route')) { + if ($container->hasExtension('sulu_route')) { // can be removed when phpcr storage is removed $container->prependExtensionConfig( 'sulu_route', @@ -133,40 +186,7 @@ public function prepend(ContainerBuilder $container) ); } - if ($isExperimentalStorage && $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', - [ - 'metadata' => [ - 'directories' => [ - 'sulu_article' => [ - 'path' => __DIR__ . '/../Resources/config/serializer', - 'namespace_prefix' => 'Sulu\Bundle\ArticleBundle', - ], - ], - ], - ] - ); - } - - if ($isPHPCRStorage && $container->hasExtension('sulu_search')) { + if ($container->hasExtension('sulu_search')) { // can be removed when phpcr storage is removed $container->prependExtensionConfig( 'sulu_page', @@ -181,26 +201,7 @@ public function prepend(ContainerBuilder $container) ); } - if ($isExperimentalStorage && $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')) { + if ($container->hasExtension('sulu_document_manager')) { // can be removed when phpcr storage is removed $container->prependExtensionConfig( 'sulu_document_manager', @@ -224,7 +225,7 @@ public function prepend(ContainerBuilder $container) ); } - if ($isPHPCRStorage && $container->hasExtension('fos_js_routing')) { + if ($container->hasExtension('fos_js_routing')) { // can be removed when phpcr storage is removed $container->prependExtensionConfig( 'fos_js_routing', @@ -236,7 +237,7 @@ public function prepend(ContainerBuilder $container) ); } - if ($isPHPCRStorage && $container->hasExtension('fos_rest')) { + if ($container->hasExtension('fos_rest')) { // can be removed when phpcr storage is removed $container->prependExtensionConfig( 'fos_rest', @@ -251,7 +252,7 @@ public function prepend(ContainerBuilder $container) ); } - if ($isPHPCRStorage && $container->hasExtension('massive_build')) { + if ($container->hasExtension('massive_build')) { // can be removed when phpcr storage is removed $container->prependExtensionConfig( 'massive_build', @@ -277,73 +278,7 @@ public function prepend(ContainerBuilder $container) ); } - if ($container->hasExtension('sulu_admin')) { - $container->prependExtensionConfig( - 'sulu_admin', - [ - 'lists' => [ - 'directories' => [ - __DIR__ . '/../Resources/config/lists', - ], - ], - 'forms' => [ - 'directories' => [ - __DIR__ . '/../Resources/config/forms', - ], - ], - 'resources' => [ - 'articles' => [ - 'routes' => [ - 'list' => 'sulu_article.get_articles', - 'detail' => 'sulu_article.get_article', - ], - ], - 'article_versions' => [ - 'routes' => [ - 'list' => 'sulu_article.get_article_versions', - 'detail' => 'sulu_article.post_article_version_trigger', - ], - ], - ], - 'field_type_options' => [ - 'selection' => [ - 'article_selection' => [ - 'default_type' => 'list_overlay', - 'resource_key' => 'articles', - 'types' => [ - 'list_overlay' => [ - 'adapter' => 'table', - 'list_key' => 'articles', - 'display_properties' => ['title', 'routePath'], - 'icon' => 'su-newspaper', - 'label' => 'sulu_article.selection_label', - 'overlay_title' => 'sulu_article.selection_overlay_title', - ], - ], - ], - ], - 'single_selection' => [ - 'single_article_selection' => [ - 'default_type' => 'list_overlay', - 'resource_key' => 'articles', - 'types' => [ - 'list_overlay' => [ - 'adapter' => 'table', - 'list_key' => 'articles', - 'display_properties' => ['title'], - 'empty_text' => 'sulu_article.no_article_selected', - 'icon' => 'su-newspaper', - 'overlay_title' => 'sulu_article.single_selection_overlay_title', - ], - ], - ], - ], - ], - ] - ); - } - - if ($isPHPCRStorage && $container->hasExtension('ongr_elasticsearch')) { + if ($container->hasExtension('ongr_elasticsearch')) { // can be removed when phpcr storage is removed $configs = $container->getExtensionConfig($this->getAlias()); $config = $this->processConfiguration(new Configuration(), $configs); @@ -380,6 +315,86 @@ public function prepend(ContainerBuilder $container) } } + private function prependExperimentalStorage(ContainerBuilder $container): void + { + if ($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 ($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 ($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('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', + ], + ], + ] + ); + } + } + } + } + /** * {@inheritdoc} */ @@ -388,64 +403,63 @@ 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; - $isExperimentalStorage = Configuration::ARTICLE_STORAGE_EXPERIMENTAL === $storage; - - if ($isPHPCRStorage) { - // can be removed when phpcr storage is removed - $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.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']); - $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')); - if ($isPHPCRStorage) { - // can be removed when phpcr storage is removed - $loader->load('services.xml'); - } - - if ($isExperimentalStorage) { - $this->configurePersistence($config['article']['objects'], $container); - } + $storage = $config['article']['storage']; + $container->setParameter('sulu_article.article_storage', $storage); - $bundles = $container->getParameter('kernel.bundles'); - if (array_key_exists('SuluAutomationBundle', $bundles)) { - $loader->load('automation.xml'); + if (Configuration::ARTICLE_STORAGE_PHPCR === $storage) { + $this->loadPHPCRStorage($config, $container, $loader); + } elseif (Configuration::ARTICLE_STORAGE_EXPERIMENTAL === $storage) { + $this->loadExperimentalStorage($config, $container, $loader); } + } - if ($isPHPCRStorage) { - // can be removed when phpcr storage is removed - $this->appendDefaultAuthor($config, $container); - $this->appendArticlePageConfig($container); - - $articleDocument = ArticleDocument::class; - $articlePageDocument = ArticlePageDocument::class; - - foreach ($container->getParameter('sulu_document_manager.mapping') as $mapping) { - if ('article' == $mapping['alias']) { - $articleDocument = $mapping['class']; - } + private function loadExperimentalStorage(array $config, ContainerBuilder $container, Loader\XmlFileLoader $loader): void + { + $this->configurePersistence($config['article']['objects'], $container); + } - if ('article_page' == $mapping['alias']) { - $articlePageDocument = $mapping['class']; - } + /** + * Can be removed when phpcr storage is removed. + */ + private function loadPHPCRStorage(array $config, ContainerBuilder $container, Loader\XmlFileLoader $loader): void + { + $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.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']); + $container->setParameter('sulu_article.documents', $config['documents']); + $container->setParameter('sulu_article.view_document.article.class', $config['documents']['article']['view']); + + $loader->load('services.xml'); + + $this->appendDefaultAuthor($config, $container); + $this->appendArticlePageConfig($container); + + $articleDocument = ArticleDocument::class; + $articlePageDocument = ArticlePageDocument::class; + + foreach ($container->getParameter('sulu_document_manager.mapping') as $mapping) { + if ('article' == $mapping['alias']) { + $articleDocument = $mapping['class']; } - $container->setParameter('sulu_article.article_document.class', $articleDocument); - $container->setParameter('sulu_article.article_page_document.class', $articlePageDocument); + 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); } /** * Append configuration for article "set_default_author". + * + * Can be removed when phpcr storage is removed. */ private function appendDefaultAuthor(array $config, ContainerBuilder $container): void { @@ -462,6 +476,8 @@ private function appendDefaultAuthor(array $config, ContainerBuilder $container) /** * Append configuration for article-page (cloned from article). + * + * Can be removed when phpcr storage is removed. */ private function appendArticlePageConfig(ContainerBuilder $container): void { @@ -478,6 +494,8 @@ private function appendArticlePageConfig(ContainerBuilder $container): void /** * Clone given path configuration and use given type. + * + * Can be removed when phpcr storage is removed. */ private function cloneArticleConfig(array $config, string $type): array { diff --git a/SuluArticleBundle.php b/SuluArticleBundle.php index 240d7a42..2b028faa 100644 --- a/SuluArticleBundle.php +++ b/SuluArticleBundle.php @@ -41,27 +41,41 @@ public function build(ContainerBuilder $container) public function process(ContainerBuilder $container): void { - $interfaces = []; - - if (Configuration::ARTICLE_STORAGE_EXPERIMENTAL === $container->getParameter('sulu_article.article_storage')) { - $interfaces = \array_merge($interfaces, [ - ArticleInterface::class => 'sulu.model.article.class', - ]); - } + $storage = $container->getParameter('sulu_article.article_storage'); $compilerPasses = []; - if (0 < \count($interfaces)) { - $compilerPasses[] = new ResolveTargetEntitiesPass($interfaces); - } - if (Configuration::ARTICLE_STORAGE_PHPCR === $container->getParameter('sulu_article.article_storage')) { - // can be removed when phpcr storage is removed - $compilerPasses[] = new RouteEnhancerCompilerPass(); - $compilerPasses[] = new ConverterCompilerPass(); + if (Configuration::ARTICLE_STORAGE_PHPCR === $storage) { + $compilerPasses = $this->getExperimentalStorageCompilerPasses($container); + } elseif (Configuration::ARTICLE_STORAGE_EXPERIMENTAL === $storage) { + $compilerPasses = $this->getPHPCRStorageCompilerPasses($container); } foreach ($compilerPasses as $compilerPass) { $compilerPass->process($container); } } + + /** + * @return CompilerPassInterface[] + */ + private function getExperimentalStorageCompilerPasses(ContainerBuilder $container): array + { + return [ + new ResolveTargetEntitiesPass([ + ArticleInterface::class => 'sulu.model.article.class', + ]), + ]; + } + + /** + * @return CompilerPassInterface[] + */ + private function getPHPCRStorageCompilerPasses(ContainerBuilder $container): array + { + return [ + new RouteEnhancerCompilerPass(), + new ConverterCompilerPass(), + ]; + } } diff --git a/Tests/Application/Kernel.php b/Tests/Application/Kernel.php index a0b482c5..4e1d3efc 100644 --- a/Tests/Application/Kernel.php +++ b/Tests/Application/Kernel.php @@ -29,7 +29,7 @@ class Kernel extends SuluTestKernel implements CompilerPassInterface /** * @var string|null */ - private $config = 'phpcr'; + private $config = 'phpcr_storage'; public function __construct(string $environment, bool $debug, string $suluContext = SuluKernel::CONTEXT_ADMIN) { @@ -48,7 +48,7 @@ public function registerBundles() $bundles = parent::registerBundles(); $bundles[] = new SuluArticleBundle(); - if ('phpcr' === $this->config) { + if ('phpcr_storage' === $this->config) { $bundles[] = new ONGRElasticsearchBundle(); } diff --git a/Tests/Application/config/config_experimental.yml b/Tests/Application/config/config_experimental_storage.yml similarity index 100% rename from Tests/Application/config/config_experimental.yml rename to Tests/Application/config/config_experimental_storage.yml diff --git a/Tests/Application/config/config_phpcr.yml b/Tests/Application/config/config_phpcr_storage.yml similarity index 100% rename from Tests/Application/config/config_phpcr.yml rename to Tests/Application/config/config_phpcr_storage.yml