vendor/sulu/sulu/src/Sulu/Bundle/CoreBundle/DependencyInjection/SuluCoreExtension.php line 40

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\CoreBundle\DependencyInjection;
  11. use Gedmo\Exception;
  12. use Oro\ORM\Query\AST\Functions\Cast;
  13. use Oro\ORM\Query\AST\Functions\String\GroupConcat;
  14. use Sulu\Component\Content\Types\Block\BlockVisitorInterface;
  15. use Sulu\Component\Rest\Csv\ObjectNotSupportedException;
  16. use Sulu\Component\Rest\Exception\InsufficientDescendantPermissionsException;
  17. use Sulu\Component\Rest\Exception\InvalidHashException;
  18. use Sulu\Component\Rest\Exception\MissingParameterException;
  19. use Sulu\Component\Rest\Exception\ReferencingResourcesFoundExceptionInterface;
  20. use Sulu\Component\Rest\Exception\RemoveDependantResourcesFoundExceptionInterface;
  21. use Sulu\Component\Rest\ListBuilder\Filter\InvalidFilterTypeOptionsException;
  22. use Symfony\Bundle\TwigBundle\Controller\ExceptionController;
  23. use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
  24. use Symfony\Component\Config\FileLocator;
  25. use Symfony\Component\DependencyInjection\ContainerBuilder;
  26. use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
  27. use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
  28. use Symfony\Component\HttpKernel\DependencyInjection\Extension;
  29. /**
  30.  * This is the class that loads and manages your bundle configuration.
  31.  *
  32.  * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html}
  33.  */
  34. class SuluCoreExtension extends Extension implements PrependExtensionInterface
  35. {
  36.     public function prepend(ContainerBuilder $container)
  37.     {
  38.         // process the configuration of SuluCoreExtension
  39.         $configs $container->getExtensionConfig($this->getAlias());
  40.         $parameterBag $container->getParameterBag();
  41.         $configs $parameterBag->resolveValue($configs);
  42.         $config $this->processConfiguration(new Configuration(), $configs);
  43.         if (isset($config['phpcr']) && $container->hasExtension('doctrine_phpcr')) {
  44.             $phpcrConfig $config['phpcr'];
  45.             // TODO: Workaround for issue: https://github.com/doctrine/DoctrinePHPCRBundle/issues/178
  46.             if (!isset($phpcrConfig['backend']['check_login_on_server'])) {
  47.                 $phpcrConfig['backend']['check_login_on_server'] = false;
  48.             }
  49.             $container->prependExtensionConfig(
  50.                 'doctrine_phpcr',
  51.                 [
  52.                     'session' => $phpcrConfig,
  53.                 ]
  54.             );
  55.         }
  56.         $templatesPath __DIR__ '/../Content/templates';
  57.         $container->prependExtensionConfig(
  58.             'sulu_core',
  59.             [
  60.                 'content' => [
  61.                     'structure' => [
  62.                         'paths' => [
  63.                             'sulu' => [
  64.                                 'path' => $templatesPath,
  65.                                 'type' => 'page',
  66.                             ],
  67.                         ],
  68.                     ],
  69.                 ],
  70.             ]
  71.         );
  72.         if ($container->hasExtension('massive_build')) {
  73.             $container->prependExtensionConfig('massive_build', [
  74.                 'command_class' => 'Sulu\Bundle\CoreBundle\CommandOptional\SuluBuildCommand',
  75.             ]);
  76.         }
  77.         if ($container->hasExtension('fos_rest')) {
  78.             $container->prependExtensionConfig(
  79.                 'fos_rest',
  80.                 [
  81.                     'exception' => [
  82.                         'enabled' => true,
  83.                         'map_exception_codes' => true,
  84.                         'codes' => [
  85.                             MissingParameterException::class => 400,
  86.                             InvalidHashException::class => 409,
  87.                             ObjectNotSupportedException::class => 406,
  88.                             InvalidFilterTypeOptionsException::class => 400,
  89.                             RemoveDependantResourcesFoundExceptionInterface::class => 409,
  90.                             InsufficientDescendantPermissionsException::class => 403,
  91.                             ReferencingResourcesFoundExceptionInterface::class => 409,
  92.                         ],
  93.                         'exception_listener' => false,
  94.                         'serialize_exceptions' => false,
  95.                         'flatten_exception_format' => 'legacy',
  96.                     ],
  97.                     'serializer' => [
  98.                         'serialize_null' => true,
  99.                     ],
  100.                     'body_listener' => [
  101.                         'enabled' => true,
  102.                     ],
  103.                     'routing_loader' => false,
  104.                     'view' => [
  105.                         'formats' => [
  106.                             'json' => true,
  107.                             'csv' => true,
  108.                         ],
  109.                     ],
  110.                 ]
  111.             );
  112.         }
  113.         if ($container->hasExtension('twig') && \class_exists(ExceptionController::class)) {
  114.             // Disable the deprecated exception_controller to support the newer fos_rest bundle
  115.             $container->prependExtensionConfig('twig', [
  116.                 'exception_controller' => null,
  117.             ]);
  118.         }
  119.         if ($container->hasExtension('handcraftedinthealps_rest_routing')) {
  120.             $container->prependExtensionConfig(
  121.                 'handcraftedinthealps_rest_routing',
  122.                 [
  123.                     'routing_loader' => [
  124.                         'default_format' => 'json',
  125.                         'formats' => [
  126.                             'json' => true,
  127.                             'csv' => true,
  128.                         ],
  129.                     ],
  130.                 ]
  131.             );
  132.         }
  133.         if ($container->hasExtension('jms_serializer')) {
  134.             $container->prependExtensionConfig(
  135.                 'jms_serializer',
  136.                 [
  137.                     'property_naming' => [
  138.                         'id' => 'jms_serializer.identical_property_naming_strategy',
  139.                     ],
  140.                 ]
  141.             );
  142.         }
  143.         if ($container->hasExtension('doctrine')) {
  144.             // depending on the gedmo version it is in gedmo lib or src folder
  145.             // we use the autoloader to detect the correct directory
  146.             // see also: https://github.com/sulu/sulu/pull/5753
  147.             $reflection = new \ReflectionClass(Exception::class);
  148.             $gedmoDirectory \dirname($reflection->getFileName());
  149.             $container->prependExtensionConfig(
  150.                 'doctrine',
  151.                 [
  152.                     'orm' => [
  153.                         'mappings' => [
  154.                             'gedmo_tree' => [
  155.                                 'type' => 'xml',
  156.                                 'prefix' => 'Gedmo\\Tree\\Entity',
  157.                                 'dir' => $gedmoDirectory '/Tree/Entity',
  158.                                 'alias' => 'GedmoTree',
  159.                                 'is_bundle' => false,
  160.                             ],
  161.                         ],
  162.                         'dql' => [
  163.                             'string_functions' => [
  164.                                 'group_concat' => GroupConcat::class,
  165.                                 'CAST' => Cast::class,
  166.                             ],
  167.                         ],
  168.                     ],
  169.                 ]
  170.             );
  171.         }
  172.         if ($container->hasExtension('stof_doctrine_extensions')) {
  173.             $container->prependExtensionConfig('stof_doctrine_extensions', ['orm' => ['default' => ['tree' => true]]]);
  174.         }
  175.         if ($container->hasExtension('jms_serializer')) {
  176.             $container->prependExtensionConfig(
  177.                 'jms_serializer',
  178.                 [
  179.                     'metadata' => [
  180.                         'debug' => '%kernel.debug%',
  181.                     ],
  182.                     'handlers' => [
  183.                         'datetime' => [
  184.                             'default_format' => 'Y-m-d\\TH:i:s',
  185.                         ],
  186.                     ],
  187.                 ]
  188.             );
  189.         }
  190.         if ($container->hasExtension('massive_build')) {
  191.             $container->prependExtensionConfig(
  192.                 'massive_build',
  193.                 [
  194.                     'targets' => [
  195.                         'prod' => [
  196.                             'dependencies' => [
  197.                                 'database' => [],
  198.                                 'phpcr' => [],
  199.                                 'fixtures' => [],
  200.                                 'phpcr_migrations' => [],
  201.                                 'system_collections' => [],
  202.                                 'security' => [],
  203.                             ],
  204.                         ],
  205.                         'dev' => [
  206.                             'dependencies' => [
  207.                                 'database' => [],
  208.                                 'fixtures' => [],
  209.                                 'phpcr' => [],
  210.                                 'user' => [],
  211.                                 'phpcr_migrations' => [],
  212.                                 'system_collections' => [],
  213.                                 'security' => [],
  214.                             ],
  215.                         ],
  216.                         'maintain' => [
  217.                             'dependencies' => [
  218.                                 'node_order' => [],
  219.                                 'search_index' => [],
  220.                                 'phpcr_migrations' => [],
  221.                             ],
  222.                         ],
  223.                     ],
  224.                 ]
  225.             );
  226.         }
  227.         if ($container->hasExtension('framework')) {
  228.             $container->prependExtensionConfig(
  229.                 'framework',
  230.                 [
  231.                     'cache' => [
  232.                         'directory' => '%sulu.common_cache_dir%/pools',
  233.                     ],
  234.                 ]
  235.             );
  236.         }
  237.     }
  238.     public function load(array $configsContainerBuilder $container)
  239.     {
  240.         $configuration = new Configuration();
  241.         $config $this->processConfiguration($configuration$configs);
  242.         $loader = new XmlFileLoader($container, new FileLocator(__DIR__ '/../Resources/config'));
  243.         foreach ($config['locales'] as $locale => $localeName) {
  244.             if (\strtolower($locale) !== $locale) {
  245.                 throw new InvalidConfigurationException('Invalid locale in configuration: ' $locale);
  246.             }
  247.         }
  248.         foreach ($config['translations'] as $translation) {
  249.             if (\strtolower($translation) !== $translation) {
  250.                 throw new InvalidConfigurationException('Invalid translation in configuration: ' $translation);
  251.             }
  252.         }
  253.         if (\strtolower($config['fallback_locale']) !== $config['fallback_locale']) {
  254.             throw new InvalidConfigurationException(
  255.                 'Invalid fallback_locale in configuration: ' $config['fallback_locale']
  256.             );
  257.         }
  258.         $container->setParameter('sulu_core.locales'\array_unique(\array_keys($config['locales'])));
  259.         $container->setParameter('sulu_core.translated_locales'$config['locales']);
  260.         $container->setParameter('sulu_core.translations'\array_unique($config['translations']));
  261.         $container->setParameter('sulu_core.fallback_locale'$config['fallback_locale']);
  262.         $container->setParameter('sulu.cache_dir'$config['cache_dir']);
  263.         $proxyCacheDirectory $container->getParameterBag()->resolveValue(
  264.             $container->getParameter('sulu.cache_dir') . '/proxies'
  265.         );
  266.         if (!\is_dir($proxyCacheDirectory)) {
  267.             \mkdir($proxyCacheDirectory0777true);
  268.         }
  269.         $container->setParameter('sulu_core.proxy_cache_dir'$proxyCacheDirectory);
  270.         // Content
  271.         if (isset($config['content'])) {
  272.             $this->initContent($config['content'], $container$loader);
  273.         }
  274.         // Webspace
  275.         if (isset($config['webspace'])) {
  276.             $this->initWebspace($config['webspace'], $container$loader);
  277.         }
  278.         // Cache
  279.         if (isset($config['cache'])) {
  280.             $this->initCache($config['cache'], $container$loader);
  281.         }
  282.         // Default Fields
  283.         if (isset($config['fields_defaults'])) {
  284.             $this->initFields($config['fields_defaults'], $container);
  285.         }
  286.         $this->initListBuilder($container$loader);
  287.         $loader->load('expression_language.xml');
  288.         $loader->load('phpcr.xml');
  289.         $loader->load('rest.xml');
  290.         $loader->load('build.xml');
  291.         $loader->load('localization.xml');
  292.         $loader->load('serializer.xml');
  293.         $loader->load('request_analyzer.xml');
  294.         $loader->load('doctrine.xml');
  295.         $container->registerForAutoconfiguration(BlockVisitorInterface::class)
  296.             ->addTag('sulu_content.block_visitor');
  297.     }
  298.     private function initWebspace(array $webspaceConfigContainerBuilder $containerXmlFileLoader $loader)
  299.     {
  300.         $container->setParameter('sulu_core.webspace.config_dir'$webspaceConfig['config_dir']);
  301.         $loader->load('webspace.xml');
  302.     }
  303.     private function initFields(array $fieldsConfigContainerBuilder $container)
  304.     {
  305.         $container->setParameter('sulu.fields_defaults.translations'$fieldsConfig['translations']);
  306.         $container->setParameter('sulu.fields_defaults.widths'$fieldsConfig['widths']);
  307.     }
  308.     private function initContent(array $contentConfigContainerBuilder $containerXmlFileLoader $loader)
  309.     {
  310.         // Default Language
  311.         $container->setParameter('sulu.content.language.namespace'$contentConfig['language']['namespace']);
  312.         $container->setParameter('sulu.content.language.default'$contentConfig['language']['default']);
  313.         // Node names
  314.         $container->setParameter('sulu.content.node_names.base'$contentConfig['node_names']['base']);
  315.         $container->setParameter('sulu.content.node_names.content'$contentConfig['node_names']['content']);
  316.         $container->setParameter('sulu.content.node_names.route'$contentConfig['node_names']['route']);
  317.         $container->setParameter('sulu.content.node_names.snippet'$contentConfig['node_names']['snippet']);
  318.         // Default template
  319.         $container->setParameter(
  320.             'sulu.content.structure.default_types',
  321.             $contentConfig['structure']['default_type']
  322.         );
  323.         foreach ($contentConfig['structure']['default_type'] as $type => $default) {
  324.             $container->setParameter(
  325.                 'sulu.content.structure.default_type.' $type,
  326.                 $default
  327.             );
  328.         }
  329.         $container->setParameter(
  330.             'sulu.content.structure.required_properties',
  331.             $contentConfig['structure']['required_properties']
  332.         );
  333.         $container->setParameter(
  334.             'sulu.content.structure.required_tags',
  335.             $contentConfig['structure']['required_tags']
  336.         );
  337.         $container->setParameter(
  338.             'sulu.content.internal_prefix',
  339.             $contentConfig['internal_prefix']
  340.         );
  341.         $container->setParameter(
  342.             'sulu.content.structure.type_map',
  343.             $contentConfig['structure']['type_map']
  344.         );
  345.         // Template
  346.         $paths = [];
  347.         foreach ($contentConfig['structure']['paths'] as $pathConfig) {
  348.             $pathType $pathConfig['type'];
  349.             if (!isset($paths[$pathType])) {
  350.                 $paths[$pathType] = [];
  351.             }
  352.             $paths[$pathType][] = $pathConfig;
  353.         }
  354.         $container->setParameter('sulu.content.structure.paths'$paths);
  355.         $loader->load('content.xml');
  356.     }
  357.     private function initCache(array $cacheContainerBuilder $containerXmlFileLoader $loader)
  358.     {
  359.         $container->setParameter('sulu_core.cache.memoize.default_lifetime'$cache['memoize']['default_lifetime']);
  360.         $loader->load('cache.xml');
  361.     }
  362.     /**
  363.      * Initializes list builder.
  364.      */
  365.     private function initListBuilder(ContainerBuilder $containerXmlFileLoader $loader)
  366.     {
  367.         $bundles $container->getParameter('kernel.bundles');
  368.         if (\array_key_exists('SuluAdminBundle'$bundles)) {
  369.             $loader->load('list_builder.xml');
  370.         }
  371.     }
  372. }