|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the BenGorFile package. |
| 5 | + * |
| 6 | + * (c) Beñat Espiña <benatespina@gmail.com> |
| 7 | + * (c) Gorka Laucirica <gorka.lauzirika@gmail.com> |
| 8 | + * |
| 9 | + * For the full copyright and license information, please view the LICENSE |
| 10 | + * file that was distributed with this source code. |
| 11 | + */ |
| 12 | + |
| 13 | +namespace BenGorFile\FileBundle\DependencyInjection\Compiler; |
| 14 | + |
| 15 | +use BenGorFile\File\Infrastructure\Persistence\Sql\SqlFileRepository; |
| 16 | +use BenGorFile\File\Infrastructure\Persistence\Sql\SqlFileSpecificationFactory; |
| 17 | +use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
| 18 | +use Symfony\Component\DependencyInjection\ContainerBuilder; |
| 19 | +use Symfony\Component\DependencyInjection\Definition; |
| 20 | +use Symfony\Component\DependencyInjection\Reference; |
| 21 | + |
| 22 | +/** |
| 23 | + * Register Sql services compiler pass. |
| 24 | + * |
| 25 | + * Service declaration via PHP allows more |
| 26 | + * flexibility with customization extend files. |
| 27 | + * |
| 28 | + * @author Mikel Etxebarria <mikeletxe4594@gmail.com> |
| 29 | + */ |
| 30 | +class SqlServicesPass implements CompilerPassInterface |
| 31 | +{ |
| 32 | + /** |
| 33 | + * {@inheritdoc} |
| 34 | + */ |
| 35 | + public function process(ContainerBuilder $container) |
| 36 | + { |
| 37 | + $config = $container->getParameter('bengor_file.config'); |
| 38 | + foreach ($config['file_class'] as $key => $file) { |
| 39 | + if ('sql' !== $file['persistence']) { |
| 40 | + continue; |
| 41 | + } |
| 42 | + |
| 43 | + if (!$container->hasDefinition('bengor.file.infrastructure.persistence.pdo')) { |
| 44 | + $container->setDefinition( |
| 45 | + 'bengor.file.infrastructure.persistence.pdo', |
| 46 | + (new Definition( |
| 47 | + \PDO::class, [ |
| 48 | + "mysql:host=%database_host%;dbname=%database_name%", |
| 49 | + "%database_user%", |
| 50 | + "%database_password%", |
| 51 | + [], |
| 52 | + ] |
| 53 | + ))->setPublic(false) |
| 54 | + ); |
| 55 | + } |
| 56 | + |
| 57 | + $container->setDefinition( |
| 58 | + 'bengor.file.infrastructure.persistence.' . $key . '_repository', |
| 59 | + (new Definition( |
| 60 | + SqlFileRepository::class, [ |
| 61 | + new Reference('bengor.file.infrastructure.persistence.pdo'), |
| 62 | + ] |
| 63 | + ))->setPublic(false) |
| 64 | + ); |
| 65 | + |
| 66 | + $container->setAlias( |
| 67 | + 'bengor_file.' . $key . '.repository', |
| 68 | + 'bengor.file.infrastructure.persistence.' . $key . '_repository' |
| 69 | + ); |
| 70 | + |
| 71 | + $container->setDefinition( |
| 72 | + 'bengor.file.infrastructure.persistence.' . $key . '_specification_factory', |
| 73 | + (new Definition( |
| 74 | + SqlFileSpecificationFactory::class |
| 75 | + ))->setPublic(false) |
| 76 | + ); |
| 77 | + |
| 78 | + $container->setAlias( |
| 79 | + 'bengor_file.' . $key . '.specification_factory', |
| 80 | + 'bengor.file.infrastructure.persistence.' . $key . '_specification_factory' |
| 81 | + ); |
| 82 | + } |
| 83 | + } |
| 84 | +} |
| 85 | + |
0 commit comments