6
6
use DI \Container ;
7
7
use DI \ContainerBuilder ;
8
8
use Doctrine \Common \Cache \Cache ;
9
- use Puli \Discovery \Api \Binding \Binding ;
10
9
use Puli \Discovery \Api \Discovery ;
11
- use Puli \Discovery \ Binding \ ResourceBinding ;
10
+ use Puli \Repository \ Api \ Resource \ FilesystemResource ;
12
11
use Puli \Repository \Api \ResourceRepository ;
13
- use Puli \Repository \Resource \FileResource ;
14
12
15
13
/**
16
14
* Application kernel.
19
17
*/
20
18
class Kernel
21
19
{
22
- /**
23
- * Name of the binding for PHP-DI configuration files in Puli.
24
- *
25
- * @see http://docs.puli.io/en/latest/discovery/introduction.html
26
- */
27
- const PULI_BINDING_NAME = 'php-di/configuration ' ;
28
-
29
20
/**
30
21
* If null, defaults to the constant PULI_FACTORY_CLASS defined by Puli.
31
22
*
32
23
* @var string|null
33
24
*/
34
25
private $ puliFactoryClass ;
35
26
36
- public function setPuliFactoryClass ($ class )
27
+ /**
28
+ * @var string[]
29
+ */
30
+ private $ modules ;
31
+
32
+ /**
33
+ * @param array $modules The name of the modules to load.
34
+ */
35
+ public function __construct (array $ modules = [])
37
36
{
38
- $ this ->puliFactoryClass = $ class ;
37
+ $ this ->modules = $ modules ;
39
38
}
40
39
41
40
/**
@@ -55,8 +54,6 @@ public function createContainer()
55
54
$ factory = new $ factoryClass ();
56
55
/** @var ResourceRepository $repository */
57
56
$ repository = $ factory ->createRepository ();
58
- /** @var Discovery $discovery */
59
- $ discovery = $ factory ->createDiscovery ($ repository );
60
57
61
58
$ containerBuilder = new ContainerBuilder ();
62
59
@@ -65,32 +62,31 @@ public function createContainer()
65
62
$ containerBuilder ->setDefinitionCache ($ cache );
66
63
}
67
64
68
- // Discover and load all configuration files registered under `php-di/configuration` in Puli
69
- $ bindings = $ discovery ->findBindings (self ::PULI_BINDING_NAME );
70
- $ bindings = array_filter ($ bindings , function (Binding $ binding ) {
71
- return $ binding instanceof ResourceBinding;
72
- });
73
- /** @var ResourceBinding[] $bindings */
74
- foreach ($ bindings as $ binding ) {
75
- foreach ($ binding ->getResources () as $ resource ) {
76
- if (!$ resource instanceof FileResource) {
77
- throw new \RuntimeException (sprintf ('Cannot load "%s": only file resources are supported ' , $ resource ->getName ()));
78
- }
79
- $ containerBuilder ->addDefinitions ($ resource ->getFilesystemPath ());
80
- }
81
- }
82
-
83
65
// Puli objects
84
66
$ containerBuilder ->addDefinitions ([
85
67
ResourceRepository::class => $ repository ,
86
- Discovery::class => $ discovery ,
68
+ Discovery::class => function () use ($ factory , $ repository ) {
69
+ return $ factory ->createDiscovery ($ repository );
70
+ },
87
71
]);
88
72
73
+ foreach ($ this ->modules as $ module ) {
74
+ $ this ->loadModule ($ containerBuilder , $ repository , $ module );
75
+ }
76
+
89
77
$ this ->configureContainerBuilder ($ containerBuilder );
90
78
91
79
return $ containerBuilder ->build ();
92
80
}
93
81
82
+ /**
83
+ * @param string $class
84
+ */
85
+ public function setPuliFactoryClass ($ class )
86
+ {
87
+ $ this ->puliFactoryClass = $ class ;
88
+ }
89
+
94
90
/**
95
91
* Override this method to configure the cache to use for container definitions.
96
92
*
@@ -107,4 +103,14 @@ protected function getContainerCache()
107
103
protected function configureContainerBuilder (ContainerBuilder $ containerBuilder )
108
104
{
109
105
}
106
+
107
+ private function loadModule (ContainerBuilder $ builder , ResourceRepository $ resources , $ module )
108
+ {
109
+ // Load all config files in the config/ directory
110
+ foreach ($ resources ->find ('/ ' . $ module . '/config/*.php ' ) as $ resource ) {
111
+ if ($ resource instanceof FilesystemResource) {
112
+ $ builder ->addDefinitions ($ resource ->getFilesystemPath ());
113
+ }
114
+ }
115
+ }
110
116
}
0 commit comments