vendor/zenstruck/foundry/src/Bundle/DependencyInjection/ZenstruckFoundryExtension.php line 126

  1. <?php
  2. /*
  3.  * This file is part of the zenstruck/foundry package.
  4.  *
  5.  * (c) Kevin Bond <kevinbond@gmail.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Zenstruck\Foundry\Bundle\DependencyInjection;
  11. use Doctrine\Bundle\DoctrineBundle\DoctrineBundle;
  12. use Doctrine\Bundle\MongoDBBundle\DoctrineMongoDBBundle;
  13. use Symfony\Bundle\MakerBundle\Maker\AbstractMaker;
  14. use Symfony\Bundle\MakerBundle\MakerBundle;
  15. use Symfony\Component\Config\FileLocator;
  16. use Symfony\Component\DependencyInjection\ContainerBuilder;
  17. use Symfony\Component\DependencyInjection\Loader\FileLoader;
  18. use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
  19. use Symfony\Component\HttpKernel\DependencyInjection\ConfigurableExtension;
  20. use Zenstruck\Foundry\Bundle\Command\StubMakeFactory;
  21. use Zenstruck\Foundry\Bundle\Command\StubMakeStory;
  22. use Zenstruck\Foundry\ModelFactory;
  23. use Zenstruck\Foundry\Story;
  24. use Zenstruck\Foundry\Test\ORMDatabaseResetter;
  25. /**
  26.  * @author Kevin Bond <kevinbond@gmail.com>
  27.  */
  28. final class ZenstruckFoundryExtension extends ConfigurableExtension
  29. {
  30.     protected function loadInternal(array $mergedConfigContainerBuilder $container): void
  31.     {
  32.         $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
  33.         $loader->load('services.xml');
  34.         $container->registerForAutoconfiguration(Story::class)
  35.             ->addTag('foundry.story')
  36.         ;
  37.         $container->registerForAutoconfiguration(ModelFactory::class)
  38.             ->addTag('foundry.factory')
  39.         ;
  40.         $this->configureFaker($mergedConfig['faker'], $container);
  41.         $this->configureDefaultInstantiator($mergedConfig['instantiator'], $container);
  42.         $this->configureDatabaseResetter($mergedConfig['database_resetter'], $container);
  43.         $this->configureMakeFactory($mergedConfig['make_factory'], $container$loader);
  44.         if (true === $mergedConfig['auto_refresh_proxies']) {
  45.             $container->getDefinition('.zenstruck_foundry.configuration')->addMethodCall('enableDefaultProxyAutoRefresh');
  46.         } elseif (false === $mergedConfig['auto_refresh_proxies']) {
  47.             $container->getDefinition('.zenstruck_foundry.configuration')->addMethodCall('disableDefaultProxyAutoRefresh');
  48.         }
  49.     }
  50.     private function configureFaker(array $configContainerBuilder $container): void
  51.     {
  52.         if ($config['service']) {
  53.             $container->setAlias('.zenstruck_foundry.faker'$config['service']);
  54.             return;
  55.         }
  56.         $definition $container->getDefinition('.zenstruck_foundry.faker');
  57.         if ($config['locale']) {
  58.             $definition->addArgument($config['locale']);
  59.         }
  60.         if ($config['seed']) {
  61.             $definition->addMethodCall('seed', [$config['seed']]);
  62.         }
  63.     }
  64.     private function configureDefaultInstantiator(array $configContainerBuilder $container): void
  65.     {
  66.         if ($config['service']) {
  67.             $container->setAlias('.zenstruck_foundry.default_instantiator'$config['service']);
  68.             return;
  69.         }
  70.         $definition $container->getDefinition('.zenstruck_foundry.default_instantiator');
  71.         if ($config['without_constructor']) {
  72.             $definition->addMethodCall('withoutConstructor');
  73.         }
  74.         if ($config['allow_extra_attributes']) {
  75.             $definition->addMethodCall('allowExtraAttributes');
  76.         }
  77.         if ($config['always_force_properties']) {
  78.             $definition->addMethodCall('alwaysForceProperties');
  79.         }
  80.     }
  81.     private function configureDatabaseResetter(array $configContainerBuilder $container): void
  82.     {
  83.         $configurationDefinition $container->getDefinition('.zenstruck_foundry.configuration');
  84.         if (false === $config['enabled']) {
  85.             $configurationDefinition->addMethodCall('disableDatabaseReset');
  86.         }
  87.         if (isset($config['orm']) && !self::isBundleLoaded($containerDoctrineBundle::class)) {
  88.             throw new \InvalidArgumentException('doctrine/doctrine-bundle should be enabled to use config under "database_resetter.orm".');
  89.         }
  90.         if (isset($config['odm']) && !self::isBundleLoaded($containerDoctrineMongoDBBundle::class)) {
  91.             throw new \InvalidArgumentException('doctrine/mongodb-odm-bundle should be enabled to use config under "database_resetter.odm".');
  92.         }
  93.         $configurationDefinition->setArgument('$ormConnectionsToReset'$config['orm']['connections'] ?? []);
  94.         $configurationDefinition->setArgument('$ormObjectManagersToReset'$config['orm']['object_managers'] ?? []);
  95.         $configurationDefinition->setArgument('$ormResetMode'$config['orm']['reset_mode'] ?? ORMDatabaseResetter::RESET_MODE_SCHEMA);
  96.         $configurationDefinition->setArgument('$odmObjectManagersToReset'$config['odm']['object_managers'] ?? []);
  97.     }
  98.     private function configureMakeFactory(array $makerConfigContainerBuilder $containerFileLoader $loader): void
  99.     {
  100.         if (\class_exists(AbstractMaker::class) && self::isBundleLoaded($containerMakerBundle::class)) {
  101.             $loader->load('maker.xml');
  102.             $makeFactoryDefinition $container->getDefinition('.zenstruck_foundry.maker.factory');
  103.             $makeFactoryDefinition->setArgument('$defaultNamespace'$makerConfig['default_namespace']);
  104.         } else {
  105.             $container->register('.zenstruck_foundry.maker.factory_stub'StubMakeFactory::class)->addTag('console.command');
  106.             $container->register('.zenstruck_foundry.maker.story_stub'StubMakeStory::class)->addTag('console.command');
  107.         }
  108.     }
  109.     private static function isBundleLoaded(ContainerBuilder $containerstring $bundleName): bool
  110.     {
  111.         return \in_array(
  112.             $bundleName,
  113.             \is_array($bundles $container->getParameter('kernel.bundles')) ? $bundles : [],
  114.             true
  115.         );
  116.     }
  117. }