1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the ICanBoogie package. |
5
|
|
|
* |
6
|
|
|
* (c) Olivier Laviale <[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 ICanBoogie\Module; |
13
|
|
|
|
14
|
|
|
use ICanBoogie\ActiveRecord\Model; |
15
|
|
|
use ICanBoogie\Application; |
16
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
17
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
18
|
|
|
use Symfony\Component\DependencyInjection\Extension\Extension; |
19
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Define modules as services. |
23
|
|
|
*/ |
24
|
|
|
class ModuleContainerExtension extends Extension |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* Create a new instance. |
28
|
|
|
* |
29
|
|
|
* @param Application $app |
30
|
|
|
* |
31
|
|
|
* @return static |
32
|
|
|
*/ |
33
|
|
|
static public function from(Application $app) |
34
|
|
|
{ |
35
|
|
|
return new static($app); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var Application |
40
|
|
|
*/ |
41
|
|
|
private $app; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param Application $app |
45
|
|
|
*/ |
46
|
|
|
public function __construct(Application $app) |
47
|
|
|
{ |
48
|
|
|
$this->app = $app; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Loads a specific configuration. |
53
|
|
|
* |
54
|
|
|
* @param array $configs An array of configuration values |
55
|
|
|
* @param ContainerBuilder $container A ContainerBuilder instance |
56
|
|
|
* |
57
|
|
|
* @throws \InvalidArgumentException When provided tag is not defined in this extension |
58
|
|
|
*/ |
59
|
|
|
public function load(array $configs, ContainerBuilder $container) |
60
|
|
|
{ |
61
|
|
|
foreach ($this->app->modules->descriptors as $module_id => $descriptor) |
62
|
|
|
{ |
63
|
|
|
$class = $descriptor[Descriptor::CLASSNAME]; |
64
|
|
|
|
65
|
|
|
$definition = (new Definition($class)) |
66
|
|
|
->setFactory([ new Reference('modules'), 'offsetGet' ]) |
67
|
|
|
->setArguments([ $module_id ]); |
68
|
|
|
|
69
|
|
|
$container->setDefinition("module.$module_id", $definition); |
70
|
|
|
|
71
|
|
|
$this->register_models($module_id, $descriptor[Descriptor::MODELS], $container); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param string $module_id |
77
|
|
|
* @param array $models |
78
|
|
|
* @param ContainerBuilder $container |
79
|
|
|
*/ |
80
|
|
|
private function register_models($module_id, array $models, ContainerBuilder $container) |
81
|
|
|
{ |
82
|
|
|
foreach ($models as $model_id => $definition) |
83
|
|
|
{ |
84
|
|
|
if ($model_id === 'primary') |
85
|
|
|
{ |
86
|
|
|
$model_id = $module_id; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$class = $definition[Model::CLASSNAME]; |
90
|
|
|
|
91
|
|
|
$definition = (new Definition($class)) |
92
|
|
|
->setFactory([ new Reference('models'), 'offsetGet' ]) |
93
|
|
|
->setArguments([ $model_id ]); |
94
|
|
|
|
95
|
|
|
$container->setDefinition("model.$model_id", $definition); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|