Skip to content

Commit

Permalink
refactor: improving mapping overrides
Browse files Browse the repository at this point in the history
  • Loading branch information
siganushka committed Sep 28, 2024
1 parent 65401eb commit 9ef787b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 22 deletions.
10 changes: 7 additions & 3 deletions src/Command/RegionUpdateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
}

Expand Down Expand Up @@ -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());
Expand Down
20 changes: 13 additions & 7 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
23 changes: 11 additions & 12 deletions src/DependencyInjection/SiganushkaRegionExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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],
]);
}
}

0 comments on commit 9ef787b

Please sign in to comment.