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

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