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\Binding\SymfonyDependencyInjection\Extension; |
13
|
|
|
|
14
|
|
|
use ICanBoogie\Application; |
15
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
16
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
17
|
|
|
use Symfony\Component\DependencyInjection\Extension\Extension; |
18
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
19
|
|
|
|
20
|
|
|
class ApplicationExtension extends Extension |
21
|
|
|
{ |
22
|
|
|
const GETTER_PREFIX = 'get_'; |
23
|
|
|
const LAZY_GETTER_PREFIX = 'lazy_get_'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Create a new instance. |
27
|
|
|
* |
28
|
|
|
* @param Application $app |
29
|
|
|
* |
30
|
|
|
* @return static |
31
|
|
|
*/ |
32
|
|
|
static public function from(Application $app) |
33
|
|
|
{ |
34
|
|
|
return new static($app); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var Application |
39
|
|
|
*/ |
40
|
|
|
private $app; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param Application $app |
44
|
|
|
*/ |
45
|
|
|
public function __construct(Application $app) |
46
|
|
|
{ |
47
|
|
|
$this->app = $app; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @inheritdoc |
52
|
|
|
*/ |
53
|
|
|
public function load(array $configs, ContainerBuilder $container) |
54
|
|
|
{ |
55
|
|
|
$container->setDefinition( |
56
|
|
|
'app', |
57
|
|
|
(new Definition(Application::class)) |
58
|
|
|
->setSynthetic(true) |
59
|
|
|
); |
60
|
|
|
|
61
|
|
|
$this->add_parameters($container); |
62
|
|
|
$this->add_services($container); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param ContainerBuilder $container |
67
|
|
|
*/ |
68
|
|
|
private function add_parameters(ContainerBuilder $container) |
69
|
|
|
{ |
70
|
|
|
foreach ($this->app->config as $param => $value) |
71
|
|
|
{ |
72
|
|
|
$param = $this->normalize_param($param); |
73
|
|
|
$container->setParameter($param, $value); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param ContainerBuilder $container |
79
|
|
|
*/ |
80
|
|
|
private function add_services(ContainerBuilder $container) |
81
|
|
|
{ |
82
|
|
|
foreach ($this->app->prototype as $method => $callable) |
83
|
|
|
{ |
84
|
|
|
if (strpos($method, self::LAZY_GETTER_PREFIX) === 0) |
85
|
|
|
{ |
86
|
|
|
$id = substr($method, strlen(self::LAZY_GETTER_PREFIX)); |
87
|
|
|
} |
88
|
|
|
elseif (strpos($method, self::GETTER_PREFIX) === 0) |
89
|
|
|
{ |
90
|
|
|
$id = substr($method, strlen(self::GETTER_PREFIX)); |
91
|
|
|
} |
92
|
|
|
else |
93
|
|
|
{ |
94
|
|
|
continue; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$definition = (new Definition('ICanBoogie\Dummy' . uniqid())) |
98
|
|
|
->setFactory([ new Reference('app'), '__get' ]) |
99
|
|
|
->setArguments([ $id ]); |
100
|
|
|
|
101
|
|
|
$container->setDefinition($id, $definition); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param string $param |
107
|
|
|
* |
108
|
|
|
* @return string |
109
|
|
|
*/ |
110
|
|
|
private function normalize_param($param) |
111
|
|
|
{ |
112
|
|
|
return strtr($param, [ |
113
|
|
|
|
114
|
|
|
' ' => '.', |
115
|
|
|
'/' => '.', |
116
|
|
|
'-' => '_', |
117
|
|
|
|
118
|
|
|
]); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|