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

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