|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Sylius package. |
| 5 | + * |
| 6 | + * (c) Sylius Sp. z o.o. |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +declare(strict_types=1); |
| 13 | + |
| 14 | +namespace Sylius\Bundle\ResourceBundle\DependencyInjection\Compiler; |
| 15 | + |
| 16 | +use Doctrine\ORM\Mapping\Entity; |
| 17 | +use Sylius\Resource\Metadata\Metadata; |
| 18 | +use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
| 19 | +use Symfony\Component\DependencyInjection\ContainerBuilder; |
| 20 | + |
| 21 | +final class RegisterAliasesForDoctrineRepositoriesPass implements CompilerPassInterface |
| 22 | +{ |
| 23 | + public function process(ContainerBuilder $container): void |
| 24 | + { |
| 25 | + if (!$container->hasParameter('sylius.resources')) { |
| 26 | + return; |
| 27 | + } |
| 28 | + |
| 29 | + /** @var array $resources */ |
| 30 | + $resources = $container->getParameter('sylius.resources'); |
| 31 | + |
| 32 | + foreach ($resources as $resourceAlias => $configuration) { |
| 33 | + $model = $configuration['classes']['model'] ?? null; |
| 34 | + |
| 35 | + if (null === $model) { |
| 36 | + continue; |
| 37 | + } |
| 38 | + |
| 39 | + $reflection = new \ReflectionClass($model); |
| 40 | + $entityAttribute = $reflection->getAttributes(Entity::class)[0] ?? null; |
| 41 | + $repositoryClass = $entityAttribute?->getArguments()['repositoryClass'] ?? null; |
| 42 | + |
| 43 | + if (null === $repositoryClass) { |
| 44 | + continue; |
| 45 | + } |
| 46 | + |
| 47 | + $metadata = Metadata::fromAliasAndConfiguration($resourceAlias, $configuration); |
| 48 | + $repositoryAlias = $metadata->getServiceId('repository'); |
| 49 | + |
| 50 | + if (!$container->hasDefinition($repositoryClass)) { |
| 51 | + continue; |
| 52 | + } |
| 53 | + |
| 54 | + $container->setAlias($repositoryAlias, $repositoryClass); |
| 55 | + } |
| 56 | + } |
| 57 | +} |
0 commit comments