vendor/sylius/resource-bundle/src/Bundle/AbstractResourceBundle.php line 103

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;
  12. use Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\DoctrineOrmMappingsPass;
  13. use Doctrine\Bundle\MongoDBBundle\DependencyInjection\Compiler\DoctrineMongoDBMappingsPass;
  14. use Doctrine\Bundle\PHPCRBundle\DependencyInjection\Compiler\DoctrinePhpcrMappingsPass;
  15. use Sylius\Bundle\ResourceBundle\DependencyInjection\Driver\Exception\UnknownDriverException;
  16. use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
  17. use Symfony\Component\DependencyInjection\Container;
  18. use Symfony\Component\DependencyInjection\ContainerBuilder;
  19. use Symfony\Component\HttpKernel\Bundle\Bundle;
  20. abstract class AbstractResourceBundle extends Bundle implements ResourceBundleInterface
  21. {
  22.     /**
  23.      * Configure format of mapping files.
  24.      */
  25.     protected string $mappingFormat ResourceBundleInterface::MAPPING_XML;
  26.     public function build(ContainerBuilder $container): void
  27.     {
  28.         if (null !== $this->getModelNamespace()) {
  29.             foreach ($this->getSupportedDrivers() as $driver) {
  30.                 [$compilerPassClassName$compilerPassMethod] = $this->getMappingCompilerPassInfo($driver);
  31.                 if (class_exists($compilerPassClassName)) {
  32.                     if (!method_exists($compilerPassClassName$compilerPassMethod)) {
  33.                         throw new InvalidConfigurationException(
  34.                             "The 'mappingFormat' value is invalid, must be 'xml', 'yaml' or 'annotation'.",
  35.                         );
  36.                     }
  37.                     switch ($this->mappingFormat) {
  38.                         case ResourceBundleInterface::MAPPING_XML:
  39.                         case ResourceBundleInterface::MAPPING_YAML:
  40.                             $container->addCompilerPass($compilerPassClassName::$compilerPassMethod(
  41.                                 [$this->getConfigFilesPath() => $this->getModelNamespace()],
  42.                                 [$this->getObjectManagerParameter()],
  43.                                 sprintf('%s.driver.%s'$this->getBundlePrefix(), $driver),
  44.                             ));
  45.                             break;
  46.                         case ResourceBundleInterface::MAPPING_ANNOTATION:
  47.                             $container->addCompilerPass($compilerPassClassName::$compilerPassMethod(
  48.                                 [$this->getModelNamespace()],
  49.                                 [$this->getConfigFilesPath()],
  50.                                 [sprintf('%s.object_manager'$this->getBundlePrefix())],
  51.                                 sprintf('%s.driver.%s'$this->getBundlePrefix(), $driver),
  52.                             ));
  53.                             break;
  54.                     }
  55.                 }
  56.             }
  57.         }
  58.     }
  59.     /**
  60.      * Return the prefix of the bundle.
  61.      */
  62.     protected function getBundlePrefix(): string
  63.     {
  64.         return Container::underscore(substr((string) strrchr(static::class, '\\'), 1, -6));
  65.     }
  66.     /**
  67.      * Return the directory where are stored the doctrine mapping.
  68.      */
  69.     protected function getDoctrineMappingDirectory(): string
  70.     {
  71.         return 'model';
  72.     }
  73.     /**
  74.      * Return the entity namespace.
  75.      *
  76.      * @return string
  77.      */
  78.     protected function getModelNamespace(): ?string
  79.     {
  80.         return (new \ReflectionClass($this))->getNamespaceName() . '\\Model';
  81.     }
  82.     /**
  83.      * Return mapping compiler pass class depending on driver.
  84.      *
  85.      *
  86.      *
  87.      * @throws UnknownDriverException
  88.      */
  89.     protected function getMappingCompilerPassInfo(string $driverType): array
  90.     {
  91.         switch ($driverType) {
  92.             case SyliusResourceBundle::DRIVER_DOCTRINE_MONGODB_ODM:
  93.                 trigger_deprecation(
  94.                     'sylius/resource-bundle',
  95.                     '1.3',
  96.                     'The "%s" driver is deprecated. Doctrine MongoDB and PHPCR will no longer be supported in 2.0.',
  97.                     SyliusResourceBundle::DRIVER_DOCTRINE_MONGODB_ODM,
  98.                 );
  99.                 $mappingsPassClassname DoctrineMongoDBMappingsPass::class;
  100.                 break;
  101.             case SyliusResourceBundle::DRIVER_DOCTRINE_ORM:
  102.                 $mappingsPassClassname DoctrineOrmMappingsPass::class;
  103.                 break;
  104.             case SyliusResourceBundle::DRIVER_DOCTRINE_PHPCR_ODM:
  105.                 trigger_deprecation(
  106.                     'sylius/resource-bundle',
  107.                     '1.3',
  108.                     'The "%s" driver is deprecated. Doctrine MongoDB and PHPCR will no longer be supported in 2.0.',
  109.                     SyliusResourceBundle::DRIVER_DOCTRINE_PHPCR_ODM,
  110.                 );
  111.                 $mappingsPassClassname DoctrinePhpcrMappingsPass::class;
  112.                 break;
  113.             default:
  114.                 throw new UnknownDriverException($driverType);
  115.         }
  116.         $compilerPassMethod sprintf('create%sMappingDriver'ucfirst($this->mappingFormat));
  117.         return [$mappingsPassClassname$compilerPassMethod];
  118.     }
  119.     /**
  120.      * Return the absolute path where are stored the doctrine mapping.
  121.      */
  122.     protected function getConfigFilesPath(): string
  123.     {
  124.         return sprintf(
  125.             '%s/Resources/config/doctrine/%s',
  126.             $this->getPath(),
  127.             strtolower($this->getDoctrineMappingDirectory()),
  128.         );
  129.     }
  130.     protected function getObjectManagerParameter(): string
  131.     {
  132.         return sprintf('%s.object_manager'$this->getBundlePrefix());
  133.     }
  134. }