1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Hautelook\AliceBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) Baldur Rensch <[email protected]> |
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
|
|
|
namespace Hautelook\AliceBundle\DependencyInjection; |
13
|
|
|
|
14
|
|
|
use Faker\Provider\Base; |
15
|
|
|
use Hautelook\AliceBundle\HautelookAliceBundle; |
16
|
|
|
use LogicException; |
17
|
|
|
use Symfony\Component\Config\FileLocator; |
18
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
19
|
|
|
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; |
20
|
|
|
use Symfony\Component\Finder\Finder; |
21
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @private |
25
|
|
|
* |
26
|
|
|
* @author Baldur Rensch <[email protected]> |
27
|
|
|
* @author Théo FIDRY <[email protected]> |
28
|
|
|
*/ |
29
|
|
|
final class HautelookAliceExtension extends Extension |
30
|
|
|
{ |
31
|
|
|
const SERVICES_DIR = __DIR__.'/../../resources/config'; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @inheritdoc |
35
|
|
|
*/ |
36
|
|
|
public function load(array $configs, ContainerBuilder $container) |
37
|
|
|
{ |
38
|
|
|
$bundles = array_flip($container->getParameter('kernel.bundles')); |
39
|
|
|
|
40
|
27 |
View Code Duplication |
if (false === array_key_exists('Fidry\AliceDataFixtures\Bridge\Symfony\FidryAliceDataFixturesBundle', $bundles)) { |
|
|
|
|
41
|
|
|
throw new LogicException( |
42
|
27 |
|
sprintf( |
43
|
27 |
|
'Cannot register "%s" without "Fidry\AliceDataFixtures\Bridge\Symfony\FidryAliceDataFixturesBundle".', |
44
|
27 |
|
HautelookAliceBundle::class |
45
|
27 |
|
) |
46
|
|
|
); |
47
|
|
|
} |
48
|
|
View Code Duplication |
if (false === array_key_exists('Doctrine\Bundle\DoctrineBundle\DoctrineBundle', $bundles)) { |
|
|
|
|
49
|
|
|
throw new LogicException( |
50
|
|
|
sprintf( |
51
|
|
|
'Cannot register "%s" without "Doctrine\Bundle\DoctrineBundle\DoctrineBundle".', |
52
|
27 |
|
HautelookAliceBundle::class |
53
|
|
|
) |
54
|
27 |
|
); |
55
|
27 |
|
} |
56
|
|
|
|
57
|
24 |
|
$this->loadConfig($configs, $container); |
58
|
24 |
|
$this->loadServices($container); |
59
|
24 |
|
|
60
|
|
|
// TODO: remove it in the future as we bump the minimal requirement of nelmio/alice |
61
|
24 |
|
// Register autoconfiguration rules for Symfony DI 3.3+ |
62
|
24 |
|
if (method_exists($container, 'registerForAutoconfiguration')) { |
63
|
|
|
if ( 0 === count($container->findTaggedServiceIds('nelmio_alice.faker.provider')) ) { |
64
|
|
|
$container->registerForAutoconfiguration(Base::class) |
65
|
|
|
->addTag('nelmio_alice.faker.provider'); |
66
|
24 |
|
} |
67
|
24 |
|
} |
68
|
16 |
|
} |
69
|
16 |
|
|
70
|
8 |
|
/** |
71
|
8 |
|
* Loads alice configuration and add the configuration values to the application parameters. |
72
|
|
|
* |
73
|
|
|
* @param array $configs |
74
|
24 |
|
* @param ContainerBuilder $container |
75
|
24 |
|
* |
76
|
24 |
|
* @throws \InvalidArgumentException |
77
|
24 |
|
*/ |
78
|
15 |
|
private function loadConfig(array $configs, ContainerBuilder $container) |
79
|
|
|
{ |
80
|
15 |
|
$configuration = new Configuration(); |
81
|
15 |
|
$processedConfiguration = $this->processConfiguration($configuration, $configs); |
82
|
15 |
|
|
83
|
15 |
|
foreach ($processedConfiguration as $key => $value) { |
84
|
15 |
|
$container->setParameter( |
85
|
|
|
$this->getAlias().'.'.$key, |
86
|
15 |
|
$value |
87
|
24 |
|
); |
88
|
|
|
} |
89
|
24 |
|
} |
90
|
24 |
|
|
91
|
24 |
|
/** |
92
|
|
|
* Loads all the services declarations. |
93
|
15 |
|
* |
94
|
|
|
* @param ContainerBuilder $container |
95
|
|
|
*/ |
96
|
|
|
private function loadServices(ContainerBuilder $container) |
97
|
15 |
|
{ |
98
|
10 |
|
$loader = new XmlFileLoader($container, new FileLocator(self::SERVICES_DIR)); |
99
|
10 |
|
$finder = new Finder(); |
100
|
5 |
|
|
101
|
5 |
|
$finder->files()->in(self::SERVICES_DIR); |
102
|
|
|
|
103
|
15 |
|
foreach ($finder as $file) { |
104
|
|
|
$loader->load( |
105
|
|
|
$file->getRelativePathname() |
106
|
|
|
); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.