diff --git a/src/Command/RegionUpdateCommand.php b/src/Command/RegionUpdateCommand.php index 505936b..878602a 100644 --- a/src/Command/RegionUpdateCommand.php +++ b/src/Command/RegionUpdateCommand.php @@ -6,6 +6,7 @@ use Doctrine\ORM\EntityManagerInterface; use Siganushka\RegionBundle\Entity\Region; +use Siganushka\RegionBundle\Repository\RegionRepository; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; @@ -17,8 +18,11 @@ */ class RegionUpdateCommand extends Command { - public function __construct(private readonly HttpClientInterface $httpClient, private readonly EntityManagerInterface $entityManager) - { + public function __construct( + private readonly HttpClientInterface $httpClient, + private readonly EntityManagerInterface $entityManager, + private readonly RegionRepository $regionRepository, + ) { parent::__construct(); } @@ -53,7 +57,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int protected function import(OutputInterface $output, array $data, Region $parent = null): void { foreach ($data as $value) { - $region = new Region($value['code'], $value['name']); + $region = $this->regionRepository->createNew($value['code'], $value['name']); $region->setParent($parent); $messages = \sprintf('[%s] %s', $region->getCode(), $region->getName()); diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index ffc26a7..af76d0f 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -5,28 +5,34 @@ namespace Siganushka\RegionBundle\DependencyInjection; use Siganushka\RegionBundle\Entity\Region; +use Siganushka\RegionBundle\Repository\RegionRepository; use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; use Symfony\Component\Config\Definition\Builder\TreeBuilder; use Symfony\Component\Config\Definition\ConfigurationInterface; class Configuration implements ConfigurationInterface { + public static array $resourceMapping = [ + 'region_class' => [Region::class, RegionRepository::class], + ]; + public function getConfigTreeBuilder(): TreeBuilder { $treeBuilder = new TreeBuilder('siganushka_region'); /** @var ArrayNodeDefinition */ $rootNode = $treeBuilder->getRootNode(); - $rootNode - ->children() - ->scalarNode('region_class') - ->defaultValue(Region::class) + foreach (static::$resourceMapping as $configName => [$entityClass]) { + $rootNode->children() + ->scalarNode($configName) + ->defaultValue($entityClass) ->validate() - ->ifTrue(static fn (mixed $v): bool => !is_a($v, Region::class, true)) - ->thenInvalid('The value must be instanceof '.Region::class.', %s given.') + ->ifTrue(static fn (mixed $v): bool => !is_a($v, $entityClass, true)) + ->thenInvalid('The value must be instanceof '.$entityClass.', %s given.') ->end() ->end() - ; + ; + } return $treeBuilder; } diff --git a/src/DependencyInjection/SiganushkaRegionExtension.php b/src/DependencyInjection/SiganushkaRegionExtension.php index d55fa5a..3a30979 100644 --- a/src/DependencyInjection/SiganushkaRegionExtension.php +++ b/src/DependencyInjection/SiganushkaRegionExtension.php @@ -4,14 +4,13 @@ namespace Siganushka\RegionBundle\DependencyInjection; -use Siganushka\RegionBundle\Entity\Region; -use Siganushka\RegionBundle\Repository\RegionRepository; use Symfony\Component\Config\FileLocator; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Extension\Extension; +use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface; use Symfony\Component\DependencyInjection\Loader\PhpFileLoader; -class SiganushkaRegionExtension extends Extension +class SiganushkaRegionExtension extends Extension implements PrependExtensionInterface { public function load(array $configs, ContainerBuilder $container): void { @@ -21,28 +20,28 @@ public function load(array $configs, ContainerBuilder $container): void $configuration = new Configuration(); $config = $this->processConfiguration($configuration, $configs); - $regionRepositoryDef = $container->findDefinition(RegionRepository::class); - $regionRepositoryDef->setArgument('$entityClass', $config['region_class']); + foreach (Configuration::$resourceMapping as $configName => [, $repositoryClass]) { + $repositoryDef = $container->findDefinition($repositoryClass); + $repositoryDef->setArgument('$entityClass', $config[$configName]); + } } public function prepend(ContainerBuilder $container): void { - if (!$container->hasExtension('siganushka_region')) { - return; - } - $configs = $container->getExtensionConfig($this->getAlias()); $configuration = new Configuration(); $config = $this->processConfiguration($configuration, $configs); $overrideMappings = []; - if (Region::class !== $config['region_class']) { - $overrideMappings[] = Region::class; + foreach (Configuration::$resourceMapping as $configName => [$entityClass]) { + if ($config[$configName] !== $entityClass) { + $overrideMappings[$entityClass] = $config[$configName]; + } } $container->prependExtensionConfig('siganushka_generic', [ - 'doctrine' => ['entity_to_superclass' => $overrideMappings], + 'doctrine' => ['mapping_override' => $overrideMappings], ]); } }