vendor/sylius/resource-bundle/src/Bundle/DependencyInjection/Driver/AbstractDriver.php line 101

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Sylius package.
  4.  *
  5.  * (c) Paweł Jędrzejewski
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace Sylius\Bundle\ResourceBundle\DependencyInjection\Driver;
  12. use Sylius\Component\Resource\Factory\Factory;
  13. use Sylius\Component\Resource\Factory\TranslatableFactoryInterface;
  14. use Sylius\Component\Resource\Metadata\Metadata;
  15. use Sylius\Component\Resource\Metadata\MetadataInterface;
  16. use Symfony\Component\DependencyInjection\ContainerBuilder;
  17. use Symfony\Component\DependencyInjection\ContainerInterface;
  18. use Symfony\Component\DependencyInjection\Definition;
  19. use Symfony\Component\DependencyInjection\Reference;
  20. abstract class AbstractDriver implements DriverInterface
  21. {
  22.     public function load(ContainerBuilder $containerMetadataInterface $metadata): void
  23.     {
  24.         $this->setClassesParameters($container$metadata);
  25.         if ($metadata->hasClass('controller')) {
  26.             $this->addController($container$metadata);
  27.         }
  28.         $this->addManager($container$metadata);
  29.         $this->addRepository($container$metadata);
  30.         if ($metadata->hasClass('factory')) {
  31.             $this->addFactory($container$metadata);
  32.         }
  33.     }
  34.     protected function setClassesParameters(ContainerBuilder $containerMetadataInterface $metadata): void
  35.     {
  36.         if ($metadata->hasClass('model')) {
  37.             $container->setParameter(sprintf('%s.model.%s.class'$metadata->getApplicationName(), $metadata->getName()), $metadata->getClass('model'));
  38.         }
  39.         if ($metadata->hasClass('controller')) {
  40.             $container->setParameter(sprintf('%s.controller.%s.class'$metadata->getApplicationName(), $metadata->getName()), $metadata->getClass('controller'));
  41.         }
  42.         if ($metadata->hasClass('factory')) {
  43.             $container->setParameter(sprintf('%s.factory.%s.class'$metadata->getApplicationName(), $metadata->getName()), $metadata->getClass('factory'));
  44.         }
  45.         if ($metadata->hasClass('repository')) {
  46.             $container->setParameter(sprintf('%s.repository.%s.class'$metadata->getApplicationName(), $metadata->getName()), $metadata->getClass('repository'));
  47.         }
  48.     }
  49.     protected function addController(ContainerBuilder $containerMetadataInterface $metadata): void
  50.     {
  51.         $definition = new Definition($metadata->getClass('controller'));
  52.         $definition
  53.             ->setPublic(true)
  54.             ->setArguments([
  55.                 $this->getMetadataDefinition($metadata),
  56.                 new Reference('sylius.resource_controller.request_configuration_factory'),
  57.                 new Reference('sylius.resource_controller.view_handler'ContainerInterface::NULL_ON_INVALID_REFERENCE),
  58.                 new Reference($metadata->getServiceId('repository')),
  59.                 new Reference($metadata->getServiceId('factory')),
  60.                 new Reference('sylius.resource_controller.new_resource_factory'),
  61.                 new Reference($metadata->getServiceId('manager')),
  62.                 new Reference('sylius.resource_controller.single_resource_provider'),
  63.                 new Reference('sylius.resource_controller.resources_collection_provider'),
  64.                 new Reference('sylius.resource_controller.form_factory'),
  65.                 new Reference('sylius.resource_controller.redirect_handler'),
  66.                 new Reference('sylius.resource_controller.flash_helper'),
  67.                 new Reference('sylius.resource_controller.authorization_checker'),
  68.                 new Reference('sylius.resource_controller.event_dispatcher'),
  69.                 new Reference('sylius.resource_controller.state_machine'ContainerInterface::NULL_ON_INVALID_REFERENCE),
  70.                 new Reference('sylius.resource_controller.resource_update_handler'),
  71.                 new Reference('sylius.resource_controller.resource_delete_handler'),
  72.             ])
  73.             ->addMethodCall('setContainer', [new Reference('service_container')])
  74.             ->addTag('controller.service_arguments')
  75.         ;
  76.         $container->setDefinition($metadata->getServiceId('controller'), $definition);
  77.     }
  78.     protected function addFactory(ContainerBuilder $containerMetadataInterface $metadata): void
  79.     {
  80.         $factoryClass $metadata->getClass('factory');
  81.         $modelClass $metadata->getClass('model');
  82.         $definition = new Definition($factoryClass);
  83.         $definition->setPublic(true);
  84.         $definitionArgs = [$modelClass];
  85.         /** @var array $factoryInterfaces */
  86.         $factoryInterfaces class_implements($factoryClass);
  87.         if (in_array(TranslatableFactoryInterface::class, $factoryInterfacestrue)) {
  88.             $decoratedDefinition = new Definition(Factory::class);
  89.             $decoratedDefinition->setArguments($definitionArgs);
  90.             $definitionArgs = [$decoratedDefinition, new Reference('sylius.translation_locale_provider')];
  91.         }
  92.         $definition->setArguments($definitionArgs);
  93.         $container->setDefinition($metadata->getServiceId('factory'), $definition);
  94.         /** @var array $factoryParents */
  95.         $factoryParents class_parents($factoryClass);
  96.         $typehintClasses array_merge(
  97.             $factoryInterfaces,
  98.             [$factoryClass],
  99.             $factoryParents,
  100.         );
  101.         foreach ($typehintClasses as $typehintClass) {
  102.             $container->registerAliasForArgument(
  103.                 $metadata->getServiceId('factory'),
  104.                 $typehintClass,
  105.                 $metadata->getHumanizedName() . ' factory',
  106.             );
  107.         }
  108.     }
  109.     protected function getMetadataDefinition(MetadataInterface $metadata): Definition
  110.     {
  111.         $definition = new Definition(Metadata::class);
  112.         $definition
  113.             ->setFactory([new Reference('sylius.resource_registry'), 'get'])
  114.             ->setArguments([$metadata->getAlias()])
  115.         ;
  116.         return $definition;
  117.     }
  118.     abstract protected function addManager(ContainerBuilder $containerMetadataInterface $metadata): void;
  119.     abstract protected function addRepository(ContainerBuilder $containerMetadataInterface $metadata): void;
  120. }