1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BrainExe\Core\DependencyInjection; |
4
|
|
|
|
5
|
|
|
use BrainExe\Core\AnnotationLoader; |
6
|
|
|
use BrainExe\Core\Annotations\Service; |
7
|
|
|
use Symfony\Bridge\ProxyManager\LazyProxy\PhpDumper\ProxyDumper; |
8
|
|
|
use Symfony\Component\DependencyInjection\Container; |
9
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
10
|
|
|
use Symfony\Component\DependencyInjection\Dumper\PhpDumper; |
11
|
|
|
use Symfony\Component\Finder\Finder; |
12
|
|
|
use Symfony\Component\Finder\SplFileInfo; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @Service(shared=false) |
16
|
|
|
*/ |
17
|
|
|
class Rebuild |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @return Container|ContainerBuilder |
22
|
|
|
*/ |
23
|
1 |
|
public function buildContainer() : Container |
24
|
|
|
{ |
25
|
1 |
|
$containerBuilder = new ContainerBuilder(); |
26
|
|
|
|
27
|
1 |
|
$this->readAnnotations($containerBuilder); |
28
|
|
|
|
29
|
1 |
|
$containerBuilder->compile(); |
30
|
1 |
|
$this->dumpContainer($containerBuilder); |
31
|
|
|
|
32
|
1 |
|
return $containerBuilder; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param ContainerBuilder $container |
37
|
|
|
*/ |
38
|
1 |
|
protected function readAnnotations(ContainerBuilder $container) |
39
|
|
|
{ |
40
|
1 |
|
$annotationLoader = new AnnotationLoader($container); |
41
|
1 |
|
$annotationLoader->load(ROOT . 'src'); |
42
|
|
|
|
43
|
1 |
|
if (!is_dir(ROOT . 'vendor/brainexe/')) { |
44
|
1 |
|
return; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$appFinder = new Finder(); |
48
|
|
|
$appFinder->directories() |
49
|
|
|
->in([ROOT . 'vendor/brainexe/']) |
50
|
|
|
->depth('<=1') |
51
|
|
|
->name('src'); |
52
|
|
|
|
53
|
|
|
foreach ($appFinder as $dir) { |
54
|
|
|
/** @var SplFileInfo $dir */ |
55
|
|
|
$annotationLoader->load($dir->getPathname()); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param ContainerBuilder $container |
61
|
|
|
*/ |
62
|
1 |
|
protected function dumpContainer(ContainerBuilder $container) |
63
|
|
|
{ |
64
|
1 |
|
$debug = $container->getParameter('debug'); |
65
|
1 |
|
$containerFile = ROOT . 'cache/dic.php'; |
66
|
1 |
|
$configFile = ROOT . 'cache/config.json'; |
67
|
|
|
|
68
|
1 |
|
$dumper = new PhpDumper($container); |
69
|
1 |
|
$dumper->setProxyDumper(new ProxyDumper()); |
70
|
|
|
|
71
|
1 |
|
$containerContent = $dumper->dump([ |
72
|
1 |
|
'class' => 'DumpedContainer', |
73
|
1 |
|
'debug' => $debug |
74
|
|
|
]); |
75
|
|
|
|
76
|
1 |
|
file_put_contents($containerFile, $containerContent); |
77
|
1 |
|
file_put_contents( |
78
|
|
|
$configFile, |
79
|
|
|
json_encode( |
80
|
1 |
|
$container->getParameterBag()->all(), |
81
|
1 |
|
JSON_PRETTY_PRINT |
82
|
|
|
) |
83
|
|
|
); |
84
|
1 |
|
} |
85
|
|
|
} |
86
|
|
|
|