vendor/sulu/sulu/src/Sulu/Bundle/AdminBundle/DependencyInjection/SuluAdminExtension.php line 217

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\AdminBundle\DependencyInjection;
  11. use Sulu\Bundle\AdminBundle\Admin\Admin;
  12. use Sulu\Bundle\AdminBundle\DependencyInjection\Compiler\AddAdminPass;
  13. use Sulu\Bundle\AdminBundle\Exception\MetadataNotFoundException;
  14. use Sulu\Bundle\AdminBundle\Exception\MetadataProviderNotFoundException;
  15. use Sulu\Bundle\AdminBundle\Metadata\FormMetadata\FormMetadataVisitorInterface;
  16. use Sulu\Bundle\AdminBundle\Metadata\FormMetadata\TypedFormMetadataVisitorInterface;
  17. use Sulu\Bundle\AdminBundle\Metadata\ListMetadata\ListMetadataLoaderInterface;
  18. use Sulu\Bundle\AdminBundle\Metadata\ListMetadata\ListMetadataVisitorInterface;
  19. use Sulu\Bundle\CoreBundle\DependencyInjection\Compiler\RemoveForeignContextServicesPass;
  20. use Sulu\Component\HttpKernel\SuluKernel;
  21. use Sulu\Component\Rest\Exception\MissingArgumentException;
  22. use Symfony\Component\Config\FileLocator;
  23. use Symfony\Component\DependencyInjection\ContainerBuilder;
  24. use Symfony\Component\DependencyInjection\Definition;
  25. use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
  26. use Symfony\Component\DependencyInjection\Loader;
  27. use Symfony\Component\HttpKernel\DependencyInjection\Extension;
  28. class SuluAdminExtension extends Extension implements PrependExtensionInterface
  29. {
  30.     public function prepend(ContainerBuilder $container)
  31.     {
  32.         if ($container->hasExtension('framework')) {
  33.             $publicDir 'public';
  34.             $composerFile $container->getParameter('kernel.project_dir') . '/composer.json';
  35.             if (\file_exists($composerFile)) {
  36.                 $composerConfig = \json_decode(\file_get_contents($composerFile), true);
  37.                 $publicDir $composerConfig['extra']['public-dir'] ?? $publicDir;
  38.             }
  39.             $container->prependExtensionConfig(
  40.                 'framework',
  41.                 [
  42.                     'assets' => [
  43.                         'packages' => [
  44.                             'sulu_admin' => [
  45.                                 'json_manifest_path' => '%kernel.project_dir%/' $publicDir '/build/admin/manifest.json',
  46.                             ],
  47.                         ],
  48.                     ],
  49.                     'cache' => [
  50.                         'pools' => [
  51.                             'sulu_admin.collaboration_cache' => [
  52.                                 'adapter' => 'cache.app',
  53.                             ],
  54.                         ],
  55.                     ],
  56.                     'translator' => [
  57.                         'enabled' => true,
  58.                     ],
  59.                 ]
  60.             );
  61.         }
  62.         if ($container->hasExtension('jms_serializer')) {
  63.             $container->prependExtensionConfig(
  64.                 'jms_serializer',
  65.                 [
  66.                     'metadata' => [
  67.                         'directories' => [
  68.                             [
  69.                                 'name' => 'sulu_admin',
  70.                                 'path' => __DIR__ '/../Resources/config/serializer',
  71.                                 'namespace_prefix' => 'Sulu\Component\SmartContent\Configuration',
  72.                             ],
  73.                         ],
  74.                     ],
  75.                 ]
  76.             );
  77.         }
  78.         if ($container->hasExtension('fos_rest')) {
  79.             $container->prependExtensionConfig(
  80.                 'fos_rest',
  81.                 [
  82.                     'exception' => [
  83.                         'codes' => [
  84.                             MetadataNotFoundException::class => 404,
  85.                             MetadataProviderNotFoundException::class => 404,
  86.                             MissingArgumentException::class => 400,
  87.                         ],
  88.                     ],
  89.                 ]
  90.             );
  91.         }
  92.         if ($container->hasExtension('fos_js_routing')) {
  93.             $container->prependExtensionConfig(
  94.                 'fos_js_routing',
  95.                 [
  96.                     'routes_to_expose' => [
  97.                         '(.+\.)?c?get_.*',
  98.                         'sulu_admin.metadata',
  99.                         'sulu_admin.put_collaborations',
  100.                     ],
  101.                 ]
  102.             );
  103.         }
  104.         $container->prependExtensionConfig(
  105.             'sulu_admin',
  106.             [
  107.                 'resources' => [
  108.                     'collaborations' => [
  109.                         'routes' => [
  110.                             'detail' => 'sulu_admin.put_collaborations',
  111.                         ],
  112.                     ],
  113.                     'localizations' => [
  114.                         'routes' => [
  115.                             'list' => 'sulu_core.get_localizations',
  116.                         ],
  117.                     ],
  118.                     'teasers' => [
  119.                         'routes' => [
  120.                             'list' => 'sulu_page.get_teasers',
  121.                         ],
  122.                     ],
  123.                 ],
  124.             ]
  125.         );
  126.     }
  127.     public function load(array $configsContainerBuilder $container): void
  128.     {
  129.         $configuration $this->getConfiguration($configs$container);
  130.         $config $this->processConfiguration($configuration$configs);
  131.         $container->setParameter($this->getAlias() . '.name'$config['name']);
  132.         $container->setParameter($this->getAlias() . '.email'$config['email']);
  133.         $container->setParameter($this->getAlias() . '.user_data_service'$config['user_data_service']);
  134.         $container->setParameter($this->getAlias() . '.resources'$config['resources']);
  135.         $container->setParameter($this->getAlias() . '.collaboration_enabled'$config['collaboration']['enabled']);
  136.         $container->setParameter($this->getAlias() . '.collaboration_interval'$config['collaboration']['interval']);
  137.         $container->setParameter($this->getAlias() . '.collaboration_threshold'$config['collaboration']['threshold']);
  138.         $container->setParameter($this->getAlias() . '.forms.directories'$config['forms']['directories'] ?? []);
  139.         $container->setParameter($this->getAlias() . '.lists.directories'$config['lists']['directories'] ?? []);
  140.         $loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__ '/../Resources/config'));
  141.         $loader->load('services.xml');
  142.         $container->registerForAutoconfiguration(Admin::class)
  143.             ->addTag(AddAdminPass::ADMIN_TAG)
  144.             ->addTag(RemoveForeignContextServicesPass::SULU_CONTEXT_TAG, ['context' => SuluKernel::CONTEXT_ADMIN]);
  145.         $container->registerForAutoconfiguration(ListMetadataLoaderInterface::class)
  146.             ->addTag('sulu_admin.list_metadata_loader');
  147.         $container->registerForAutoconfiguration(ListMetadataVisitorInterface::class)
  148.             ->addTag('sulu_admin.list_metadata_visitor');
  149.         $container->registerForAutoconfiguration(FormMetadataVisitorInterface::class)
  150.             ->addTag('sulu_admin.form_metadata_visitor');
  151.         $container->registerForAutoconfiguration(TypedFormMetadataVisitorInterface::class)
  152.             ->addTag('sulu_admin.typed_form_metadata_visitor');
  153.         $this->loadFieldTypeOptions(
  154.             $config['field_type_options'],
  155.             $container->getDefinition('sulu_admin.field_type_option_registry')
  156.         );
  157.         $this->registerPropertyMetadataMappers(
  158.             $config['field_type_options'],
  159.             $container
  160.         );
  161.     }
  162.     public function loadFieldTypeOptions(
  163.         array $fieldTypeOptionsConfig,
  164.         Definition $fieldTypeOptionRegistry
  165.     ) {
  166.         foreach ($fieldTypeOptionsConfig as $baseFieldType => $baseFieldTypeConfig) {
  167.             foreach ($baseFieldTypeConfig as $fieldTypeName => $fieldTypeConfig) {
  168.                 $fieldTypeOptionRegistry->addMethodCall('add', [$fieldTypeName$baseFieldType$fieldTypeConfig]);
  169.             }
  170.         }
  171.     }
  172.     private function registerPropertyMetadataMappers(array $fieldTypeOptionsConfigContainerBuilder $container)
  173.     {
  174.         foreach ($fieldTypeOptionsConfig as $baseFieldType => $baseFieldTypeConfig) {
  175.             if (!\in_array($baseFieldType, ['selection''single_selection'], true)) {
  176.                 continue;
  177.             }
  178.             $definition $container->getDefinition('sulu_admin.property_metadata_mapper.' $baseFieldType);
  179.             foreach ($baseFieldTypeConfig as $fieldTypeName => $fieldTypeConfig) {
  180.                 $definition->addTag('sulu_admin.property_metadata_mapper', [
  181.                     'type' => $fieldTypeName,
  182.                 ]);
  183.             }
  184.         }
  185.     }
  186.     /**
  187.      * @param mixed[] $config
  188.      */
  189.     public function getConfiguration(array $configContainerBuilder $container): Configuration
  190.     {
  191.         return new Configuration($container->getParameter('kernel.debug'));
  192.     }
  193. }