vendor/sulu/article-bundle/DependencyInjection/SuluArticleExtension.php line 248

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of Sulu.
  4.  *
  5.  * (c) Sulu GmbH
  6.  *
  7.  * This source file is subject to the MIT license that is bundled
  8.  * with this source code in the file LICENSE.
  9.  */
  10. namespace Sulu\Bundle\ArticleBundle\DependencyInjection;
  11. use Sulu\Bundle\ArticleBundle\Document\ArticleDocument;
  12. use Sulu\Bundle\ArticleBundle\Document\ArticlePageDocument;
  13. use Sulu\Bundle\ArticleBundle\Document\Form\ArticleDocumentType;
  14. use Sulu\Bundle\ArticleBundle\Document\Form\ArticlePageDocumentType;
  15. use Sulu\Bundle\ArticleBundle\Document\Structure\ArticleBridge;
  16. use Sulu\Bundle\ArticleBundle\Document\Structure\ArticlePageBridge;
  17. use Sulu\Bundle\ArticleBundle\Exception\ArticlePageNotFoundException;
  18. use Sulu\Bundle\ArticleBundle\Exception\ParameterNotAllowedException;
  19. use Symfony\Component\Config\FileLocator;
  20. use Symfony\Component\DependencyInjection\ContainerBuilder;
  21. use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
  22. use Symfony\Component\DependencyInjection\Loader;
  23. use Symfony\Component\HttpKernel\DependencyInjection\Extension;
  24. /**
  25.  * This is the class that loads and manages article bundle configuration.
  26.  */
  27. class SuluArticleExtension extends Extension implements PrependExtensionInterface
  28. {
  29.     public function prepend(ContainerBuilder $container)
  30.     {
  31.         if ($container->hasExtension('sulu_core')) {
  32.             $container->prependExtensionConfig(
  33.                 'sulu_core',
  34.                 [
  35.                     'content' => [
  36.                         'structure' => [
  37.                             'paths' => [
  38.                                 'article' => [
  39.                                     'path' => '%kernel.project_dir%/config/templates/articles',
  40.                                     'type' => 'article',
  41.                                 ],
  42.                                 'article_page' => [
  43.                                     'path' => '%kernel.project_dir%/config/templates/articles',
  44.                                     'type' => 'article_page',
  45.                                 ],
  46.                             ],
  47.                             'type_map' => [
  48.                                 'article' => ArticleBridge::class,
  49.                                 'article_page' => ArticlePageBridge::class,
  50.                             ],
  51.                         ],
  52.                     ],
  53.                 ]
  54.             );
  55.         }
  56.         if ($container->hasExtension('sulu_route')) {
  57.             $container->prependExtensionConfig(
  58.                 'sulu_route',
  59.                 [
  60.                     'mappings' => [
  61.                         'Sulu\Bundle\ArticleBundle\Document\ArticleDocument' => [
  62.                             'resource_key' => 'articles',
  63.                         ],
  64.                         'Sulu\Bundle\ArticleBundle\Document\ArticlePageDocument' => [
  65.                             'resource_key' => 'article_pages',
  66.                         ],
  67.                     ],
  68.                 ]
  69.             );
  70.         }
  71.         if ($container->hasExtension('jms_serializer')) {
  72.             $container->prependExtensionConfig(
  73.                 'jms_serializer',
  74.                 [
  75.                     'metadata' => [
  76.                         'directories' => [
  77.                             'sulu_article' => [
  78.                                 'path' => __DIR__ '/../Resources/config/serializer',
  79.                                 'namespace_prefix' => 'Sulu\Bundle\ArticleBundle',
  80.                             ],
  81.                         ],
  82.                     ],
  83.                 ]
  84.             );
  85.         }
  86.         if ($container->hasExtension('sulu_search')) {
  87.             $container->prependExtensionConfig(
  88.                 'sulu_page',
  89.                 [
  90.                     'search' => [
  91.                         'mapping' => [
  92.                             ArticleDocument::class => ['index' => 'article''decorate_index' => true],
  93.                             ArticlePageDocument::class => ['index' => 'article_page'],
  94.                         ],
  95.                     ],
  96.                 ]
  97.             );
  98.         }
  99.         if ($container->hasExtension('sulu_document_manager')) {
  100.             $container->prependExtensionConfig(
  101.                 'sulu_document_manager',
  102.                 [
  103.                     'mapping' => [
  104.                         'article' => [
  105.                             'class' => ArticleDocument::class,
  106.                             'phpcr_type' => 'sulu:article',
  107.                             'form_type' => ArticleDocumentType::class,
  108.                         ],
  109.                         'article_page' => [
  110.                             'class' => ArticlePageDocument::class,
  111.                             'phpcr_type' => 'sulu:articlepage',
  112.                             'form_type' => ArticlePageDocumentType::class,
  113.                         ],
  114.                     ],
  115.                     'path_segments' => [
  116.                         'articles' => 'articles',
  117.                     ],
  118.                 ]
  119.             );
  120.         }
  121.         if ($container->hasExtension('fos_js_routing')) {
  122.             $container->prependExtensionConfig(
  123.                 'fos_js_routing',
  124.                 [
  125.                     'routes_to_expose' => [
  126.                         'sulu_article.post_article_version_trigger',
  127.                     ],
  128.                 ]
  129.             );
  130.         }
  131.         if ($container->hasExtension('fos_rest')) {
  132.             $container->prependExtensionConfig(
  133.                 'fos_rest',
  134.                 [
  135.                     'exception' => [
  136.                         'codes' => [
  137.                             ParameterNotAllowedException::class => 400,
  138.                             ArticlePageNotFoundException::class => 404,
  139.                         ],
  140.                     ],
  141.                 ]
  142.             );
  143.         }
  144.         if ($container->hasExtension('massive_build')) {
  145.             $container->prependExtensionConfig(
  146.                 'massive_build',
  147.                 [
  148.                     'targets' => [
  149.                         'prod' => [
  150.                             'dependencies' => [
  151.                                 'article_index' => [],
  152.                             ],
  153.                         ],
  154.                         'dev' => [
  155.                             'dependencies' => [
  156.                                 'article_index' => [],
  157.                             ],
  158.                         ],
  159.                         'maintain' => [
  160.                             'dependencies' => [
  161.                                 'article_index' => [],
  162.                             ],
  163.                         ],
  164.                     ],
  165.                 ]
  166.             );
  167.         }
  168.         if ($container->hasExtension('sulu_admin')) {
  169.             $container->prependExtensionConfig(
  170.                 'sulu_admin',
  171.                 [
  172.                     'lists' => [
  173.                         'directories' => [
  174.                             __DIR__ '/../Resources/config/lists',
  175.                         ],
  176.                     ],
  177.                     'forms' => [
  178.                         'directories' => [
  179.                             __DIR__ '/../Resources/config/forms',
  180.                         ],
  181.                     ],
  182.                     'resources' => [
  183.                         'articles' => [
  184.                             'routes' => [
  185.                                 'list' => 'sulu_article.get_articles',
  186.                                 'detail' => 'sulu_article.get_article',
  187.                             ],
  188.                         ],
  189.                         'article_versions' => [
  190.                             'routes' => [
  191.                                 'list' => 'sulu_article.get_article_versions',
  192.                                 'detail' => 'sulu_article.post_article_version_trigger',
  193.                             ],
  194.                         ],
  195.                     ],
  196.                     'field_type_options' => [
  197.                         'selection' => [
  198.                             'article_selection' => [
  199.                                 'default_type' => 'list_overlay',
  200.                                 'resource_key' => 'articles',
  201.                                 'types' => [
  202.                                     'list_overlay' => [
  203.                                         'adapter' => 'table',
  204.                                         'list_key' => 'articles',
  205.                                         'display_properties' => ['title''routePath'],
  206.                                         'icon' => 'su-newspaper',
  207.                                         'label' => 'sulu_article.selection_label',
  208.                                         'overlay_title' => 'sulu_article.selection_overlay_title',
  209.                                     ],
  210.                                 ],
  211.                             ],
  212.                         ],
  213.                         'single_selection' => [
  214.                             'single_article_selection' => [
  215.                                 'default_type' => 'list_overlay',
  216.                                 'resource_key' => 'articles',
  217.                                 'types' => [
  218.                                     'list_overlay' => [
  219.                                         'adapter' => 'table',
  220.                                         'list_key' => 'articles',
  221.                                         'display_properties' => ['title'],
  222.                                         'empty_text' => 'sulu_article.no_article_selected',
  223.                                         'icon' => 'su-newspaper',
  224.                                         'overlay_title' => 'sulu_article.single_selection_overlay_title',
  225.                                     ],
  226.                                 ],
  227.                             ],
  228.                         ],
  229.                     ],
  230.                 ]
  231.             );
  232.         }
  233.         if ($container->hasExtension('ongr_elasticsearch')) {
  234.             $configs $container->getExtensionConfig($this->getAlias());
  235.             $config $this->processConfiguration(new Configuration(), $configs);
  236.             $indexName $config['index_name'];
  237.             $hosts $config['hosts'];
  238.             $ongrElasticSearchConfig = [
  239.                 'managers' => [
  240.                     'default' => [
  241.                         'index' => [
  242.                             'index_name' => $indexName,
  243.                         ],
  244.                         'mappings' => ['SuluArticleBundle'],
  245.                     ],
  246.                     'live' => [
  247.                         'index' => [
  248.                             'index_name' => $indexName '_live',
  249.                         ],
  250.                         'mappings' => ['SuluArticleBundle'],
  251.                     ],
  252.                 ],
  253.             ];
  254.             if (\count($hosts) > 0) {
  255.                 $ongrElasticSearchConfig['managers']['default']['index']['hosts'] = $hosts;
  256.                 $ongrElasticSearchConfig['managers']['live']['index']['hosts'] = $hosts;
  257.             }
  258.             $container->prependExtensionConfig(
  259.                 'ongr_elasticsearch',
  260.                 $ongrElasticSearchConfig
  261.             );
  262.         }
  263.     }
  264.     public function load(array $configsContainerBuilder $container)
  265.     {
  266.         $configuration = new Configuration();
  267.         $config $this->processConfiguration($configuration$configs);
  268.         $container->setParameter('sulu_article.default_main_webspace'$config['default_main_webspace']);
  269.         $container->setParameter('sulu_article.default_additional_webspaces'$config['default_additional_webspaces']);
  270.         $container->setParameter('sulu_article.types'$config['types']);
  271.         $container->setParameter('sulu_article.documents'$config['documents']);
  272.         $container->setParameter('sulu_article.view_document.article.class'$config['documents']['article']['view']);
  273.         $container->setParameter('sulu_article.display_tab_all'$config['display_tab_all']);
  274.         $container->setParameter('sulu_article.smart_content.default_limit'$config['smart_content']['default_limit']);
  275.         $container->setParameter('sulu_article.search_fields'$config['search_fields']);
  276.         $loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__ '/../Resources/config'));
  277.         $loader->load('services.xml');
  278.         $bundles $container->getParameter('kernel.bundles');
  279.         if (\array_key_exists('SuluAutomationBundle'$bundles)) {
  280.             $loader->load('automation.xml');
  281.         }
  282.         if (\array_key_exists('SuluTrashBundle'$bundles)) {
  283.             $loader->load('services_trash.xml');
  284.         }
  285.         if (\array_key_exists('SuluHeadlessBundle'$bundles)) {
  286.             $loader->load('services_headless.xml');
  287.         }
  288.         $this->appendDefaultAuthor($config$container);
  289.         $this->appendArticlePageConfig($container);
  290.         $articleDocument ArticleDocument::class;
  291.         $articlePageDocument ArticlePageDocument::class;
  292.         foreach ($container->getParameter('sulu_document_manager.mapping') as $mapping) {
  293.             if ('article' == $mapping['alias']) {
  294.                 $articleDocument $mapping['class'];
  295.             }
  296.             if ('article_page' == $mapping['alias']) {
  297.                 $articlePageDocument $mapping['class'];
  298.             }
  299.         }
  300.         $container->setParameter('sulu_article.article_document.class'$articleDocument);
  301.         $container->setParameter('sulu_article.article_page_document.class'$articlePageDocument);
  302.     }
  303.     /**
  304.      * Append configuration for article "set_default_author".
  305.      */
  306.     private function appendDefaultAuthor(array $configContainerBuilder $container): void
  307.     {
  308.         $mapping $container->getParameter('sulu_document_manager.mapping');
  309.         foreach ($mapping as $key => $item) {
  310.             if ('article' === $item['alias']) {
  311.                 $mapping[$key]['set_default_author'] = $config['default_author'];
  312.             }
  313.         }
  314.         $container->setParameter('sulu_document_manager.mapping'$mapping);
  315.         $container->setParameter('sulu_article.default_author'$config['default_author']);
  316.     }
  317.     /**
  318.      * Append configuration for article-page (cloned from article).
  319.      */
  320.     private function appendArticlePageConfig(ContainerBuilder $container): void
  321.     {
  322.         $paths $container->getParameter('sulu.content.structure.paths');
  323.         $paths['article_page'] = $this->cloneArticleConfig($paths['article'], 'article_page');
  324.         $container->setParameter('sulu.content.structure.paths'$paths);
  325.         $defaultTypes $container->getParameter('sulu.content.structure.default_types');
  326.         if (isset($defaultTypes['article'])) {
  327.             $defaultTypes['article_page'] = $defaultTypes['article'];
  328.             $container->setParameter('sulu.content.structure.default_types'$defaultTypes);
  329.         }
  330.     }
  331.     /**
  332.      * Clone given path configuration and use given type.
  333.      */
  334.     private function cloneArticleConfig(array $configstring $type): array
  335.     {
  336.         $result = [];
  337.         foreach ($config as $item) {
  338.             $result[] = ['path' => $item['path'], 'type' => $type];
  339.         }
  340.         return $result;
  341.     }
  342. }