|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Zenstruck\Foundry\Bundle\DependencyInjection; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Bundle\MakerBundle\Maker\AbstractMaker; |
|
6
|
|
|
use Symfony\Component\Config\FileLocator; |
|
7
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
8
|
|
|
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; |
|
9
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\ConfigurableExtension; |
|
10
|
|
|
use Zenstruck\Foundry\Bundle\Command\StubMakeFactory; |
|
11
|
|
|
use Zenstruck\Foundry\Bundle\Command\StubMakeStory; |
|
12
|
|
|
use Zenstruck\Foundry\Configuration; |
|
|
|
|
|
|
13
|
|
|
use Zenstruck\Foundry\ModelFactory; |
|
14
|
|
|
use Zenstruck\Foundry\Story; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @author Kevin Bond <[email protected]> |
|
18
|
64 |
|
*/ |
|
19
|
|
|
final class ZenstruckFoundryExtension extends ConfigurableExtension |
|
20
|
64 |
|
{ |
|
21
|
|
|
protected function loadInternal(array $mergedConfig, ContainerBuilder $container): void |
|
22
|
64 |
|
{ |
|
23
|
|
|
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
|
24
|
64 |
|
|
|
25
|
64 |
|
$loader->load('services.xml'); |
|
26
|
|
|
|
|
27
|
|
|
$container->registerForAutoconfiguration(Story::class) |
|
28
|
64 |
|
->addTag('foundry.story') |
|
29
|
64 |
|
; |
|
30
|
|
|
|
|
31
|
|
|
$container->registerForAutoconfiguration(ModelFactory::class) |
|
32
|
64 |
|
->addTag('foundry.factory') |
|
33
|
64 |
|
; |
|
34
|
|
|
|
|
35
|
64 |
|
$this->configureFaker($mergedConfig['faker'], $container); |
|
36
|
10 |
|
$this->configureDefaultInstantiator($mergedConfig['instantiator'], $container); |
|
37
|
10 |
|
|
|
38
|
|
|
if (true === $mergedConfig['auto_refresh_proxies']) { |
|
39
|
|
|
$container->getDefinition(Configuration::class)->addMethodCall('enableDefaultProxyAutoRefresh'); |
|
40
|
64 |
|
} elseif (false === $mergedConfig['auto_refresh_proxies']) { |
|
41
|
|
|
$container->getDefinition(Configuration::class)->addMethodCall('disableDefaultProxyAutoRefresh'); |
|
42
|
64 |
|
} |
|
43
|
|
|
|
|
44
|
64 |
|
if (!\class_exists(AbstractMaker::class)) { |
|
45
|
10 |
|
$container->register(StubMakeFactory::class)->addTag('console.command'); |
|
46
|
|
|
$container->register(StubMakeStory::class)->addTag('console.command'); |
|
47
|
10 |
|
} |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
54 |
|
private function configureFaker(array $config, ContainerBuilder $container): void |
|
51
|
10 |
|
{ |
|
52
|
|
|
if ($config['service']) { |
|
53
|
54 |
|
$container->setAlias('zenstruck_foundry.faker', $config['service']); |
|
54
|
|
|
|
|
55
|
64 |
|
return; |
|
56
|
|
|
} |
|
57
|
64 |
|
|
|
58
|
10 |
|
if ($config['locale']) { |
|
59
|
|
|
$container->getDefinition('zenstruck_foundry.faker')->addArgument($config['locale']); |
|
60
|
10 |
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
54 |
|
private function configureDefaultInstantiator(array $config, ContainerBuilder $container): void |
|
64
|
|
|
{ |
|
65
|
54 |
|
if ($config['service']) { |
|
66
|
10 |
|
$container->setAlias('zenstruck_foundry.default_instantiator', $config['service']); |
|
67
|
|
|
|
|
68
|
|
|
return; |
|
69
|
54 |
|
} |
|
70
|
10 |
|
|
|
71
|
|
|
$definition = $container->getDefinition('zenstruck_foundry.default_instantiator'); |
|
72
|
|
|
|
|
73
|
54 |
|
if ($config['without_constructor']) { |
|
74
|
10 |
|
$definition->addMethodCall('withoutConstructor'); |
|
75
|
|
|
} |
|
76
|
54 |
|
|
|
77
|
|
|
if ($config['allow_extra_attributes']) { |
|
78
|
|
|
$definition->addMethodCall('allowExtraAttributes'); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
if ($config['always_force_properties']) { |
|
82
|
|
|
$definition->addMethodCall('alwaysForceProperties'); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: