5
5
namespace Tests \Acme \SyliusExamplePlugin \Application ;
6
6
7
7
use PSS \SymfonyMockerContainer \DependencyInjection \MockerContainer ;
8
+ use Sylius \Bundle \CoreBundle \Application \Kernel as SyliusKernel ;
8
9
use Symfony \Bundle \FrameworkBundle \Kernel \MicroKernelTrait ;
9
- use Symfony \Component \Config \Loader \DelegatingLoader ;
10
10
use Symfony \Component \Config \Loader \LoaderInterface ;
11
- use Symfony \Component \Config \Loader \LoaderResolver ;
12
11
use Symfony \Component \Config \Resource \FileResource ;
13
12
use Symfony \Component \DependencyInjection \ContainerBuilder ;
14
- use Symfony \Component \DependencyInjection \ContainerInterface ;
15
- use Symfony \Component \DependencyInjection \Loader \ClosureLoader ;
16
- use Symfony \Component \DependencyInjection \Loader \DirectoryLoader ;
17
- use Symfony \Component \DependencyInjection \Loader \GlobFileLoader ;
18
- use Symfony \Component \DependencyInjection \Loader \IniFileLoader ;
19
- use Symfony \Component \DependencyInjection \Loader \PhpFileLoader ;
20
- use Symfony \Component \DependencyInjection \Loader \XmlFileLoader ;
21
- use Symfony \Component \DependencyInjection \Loader \YamlFileLoader ;
22
- use Symfony \Component \HttpKernel \Config \FileLocator ;
13
+ use Symfony \Component \HttpKernel \Bundle \BundleInterface ;
23
14
use Symfony \Component \HttpKernel \Kernel as BaseKernel ;
24
15
use Symfony \Component \Routing \RouteCollectionBuilder ;
25
- use Webmozart \Assert \Assert ;
26
16
27
17
final class Kernel extends BaseKernel
28
18
{
@@ -42,33 +32,37 @@ public function getLogDir(): string
42
32
43
33
public function registerBundles (): iterable
44
34
{
45
- $ contents = require $ this ->getProjectDir () . ' /config/bundles.php ' ;
46
- foreach ( $ contents as $ class => $ envs ) {
47
- if (isset ( $ envs [ ' all ' ]) || isset ( $ envs [ $ this -> environment ] )) {
48
- yield new $ class () ;
35
+ foreach ( $ this ->getConfigurationDirectories () as $ confDir ) {
36
+ $ bundlesFile = $ confDir . ' /bundles.php ' ;
37
+ if (false === is_file ( $ bundlesFile )) {
38
+ continue ;
49
39
}
40
+ yield from $ this ->registerBundlesFromFile ($ bundlesFile );
50
41
}
51
42
}
52
43
53
44
protected function configureContainer (ContainerBuilder $ container , LoaderInterface $ loader ): void
54
45
{
55
- $ container ->addResource (new FileResource ($ this ->getProjectDir () . '/config/bundles.php ' ));
46
+ foreach ($ this ->getConfigurationDirectories () as $ confDir ) {
47
+ $ bundlesFile = $ confDir . '/bundles.php ' ;
48
+ if (false === is_file ($ bundlesFile )) {
49
+ continue ;
50
+ }
51
+ $ container ->addResource (new FileResource ($ bundlesFile ));
52
+ }
53
+
56
54
$ container ->setParameter ('container.dumper.inline_class_loader ' , true );
57
- $ confDir = $ this ->getProjectDir () . '/config ' ;
58
55
59
- $ loader ->load ($ confDir . '/{packages}/* ' . self ::CONFIG_EXTS , 'glob ' );
60
- $ loader ->load ($ confDir . '/{packages}/ ' . $ this ->environment . '/**/* ' . self ::CONFIG_EXTS , 'glob ' );
61
- $ loader ->load ($ confDir . '/{services} ' . self ::CONFIG_EXTS , 'glob ' );
62
- $ loader ->load ($ confDir . '/{services}_ ' . $ this ->environment . self ::CONFIG_EXTS , 'glob ' );
56
+ foreach ($ this ->getConfigurationDirectories () as $ confDir ) {
57
+ $ this ->loadContainerConfiguration ($ loader , $ confDir );
58
+ }
63
59
}
64
60
65
61
protected function configureRoutes (RouteCollectionBuilder $ routes ): void
66
62
{
67
- $ confDir = $ this ->getProjectDir () . '/config ' ;
68
-
69
- $ routes ->import ($ confDir . '/{routes}/* ' . self ::CONFIG_EXTS , '/ ' , 'glob ' );
70
- $ routes ->import ($ confDir . '/{routes}/ ' . $ this ->environment . '/**/* ' . self ::CONFIG_EXTS , '/ ' , 'glob ' );
71
- $ routes ->import ($ confDir . '/{routes} ' . self ::CONFIG_EXTS , '/ ' , 'glob ' );
63
+ foreach ($ this ->getConfigurationDirectories () as $ confDir ) {
64
+ $ this ->loadRoutesConfiguration ($ routes , $ confDir );
65
+ }
72
66
}
73
67
74
68
protected function getContainerBaseClass (): string
@@ -80,27 +74,52 @@ protected function getContainerBaseClass(): string
80
74
return parent ::getContainerBaseClass ();
81
75
}
82
76
83
- protected function getContainerLoader ( ContainerInterface $ container ): LoaderInterface
77
+ private function isTestEnvironment ( ): bool
84
78
{
85
- /** @var ContainerBuilder $container */
86
- Assert::isInstanceOf ($ container , ContainerBuilder::class);
87
-
88
- $ locator = new FileLocator ($ this , $ this ->getRootDir () . '/Resources ' );
89
- $ resolver = new LoaderResolver ([
90
- new XmlFileLoader ($ container , $ locator ),
91
- new YamlFileLoader ($ container , $ locator ),
92
- new IniFileLoader ($ container , $ locator ),
93
- new PhpFileLoader ($ container , $ locator ),
94
- new GlobFileLoader ($ container , $ locator ),
95
- new DirectoryLoader ($ container , $ locator ),
96
- new ClosureLoader ($ container ),
97
- ]);
98
-
99
- return new DelegatingLoader ($ resolver );
79
+ return 0 === strpos ($ this ->getEnvironment (), 'test ' );
100
80
}
101
81
102
- private function isTestEnvironment ( ): bool
82
+ private function loadContainerConfiguration ( LoaderInterface $ loader , string $ confDir ): void
103
83
{
104
- return 0 === strpos ($ this ->getEnvironment (), 'test ' );
84
+ $ loader ->load ($ confDir . '/{packages}/* ' . self ::CONFIG_EXTS , 'glob ' );
85
+ $ loader ->load ($ confDir . '/{packages}/ ' . $ this ->environment . '/**/* ' . self ::CONFIG_EXTS , 'glob ' );
86
+ $ loader ->load ($ confDir . '/{services} ' . self ::CONFIG_EXTS , 'glob ' );
87
+ $ loader ->load ($ confDir . '/{services}_ ' . $ this ->environment . self ::CONFIG_EXTS , 'glob ' );
88
+ }
89
+
90
+ private function loadRoutesConfiguration (RouteCollectionBuilder $ routes , string $ confDir ): void
91
+ {
92
+ $ routes ->import ($ confDir . '/{routes}/* ' . self ::CONFIG_EXTS , '/ ' , 'glob ' );
93
+ $ routes ->import ($ confDir . '/{routes}/ ' . $ this ->environment . '/**/* ' . self ::CONFIG_EXTS , '/ ' , 'glob ' );
94
+ $ routes ->import ($ confDir . '/{routes} ' . self ::CONFIG_EXTS , '/ ' , 'glob ' );
95
+ }
96
+
97
+ /**
98
+ * @return BundleInterface[]
99
+ */
100
+ private function registerBundlesFromFile (string $ bundlesFile ): iterable
101
+ {
102
+ $ contents = require $ bundlesFile ;
103
+ foreach ($ contents as $ class => $ envs ) {
104
+ if (isset ($ envs ['all ' ]) || isset ($ envs [$ this ->environment ])) {
105
+ yield new $ class ();
106
+ }
107
+ }
108
+ }
109
+
110
+ /**
111
+ * @return string[]
112
+ */
113
+ private function getConfigurationDirectories (): iterable
114
+ {
115
+ yield $ this ->getProjectDir () . '/config ' ;
116
+ $ syliusConfigDir = $ this ->getProjectDir () . '/config/sylius/ ' . SyliusKernel::MAJOR_VERSION . '. ' . SyliusKernel::MINOR_VERSION ;
117
+ if (is_dir ($ syliusConfigDir )) {
118
+ yield $ syliusConfigDir ;
119
+ }
120
+ $ symfonyConfigDir = $ this ->getProjectDir () . '/config/symfony/ ' . BaseKernel::MAJOR_VERSION . '. ' . BaseKernel::MINOR_VERSION ;
121
+ if (is_dir ($ symfonyConfigDir )) {
122
+ yield $ symfonyConfigDir ;
123
+ }
105
124
}
106
125
}
0 commit comments