1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BrainExe\Core\DependencyInjection; |
4
|
|
|
|
5
|
|
|
use BrainExe\Annotations\Annotations\Service; |
6
|
|
|
use BrainExe\Annotations\Loader; |
7
|
|
|
use BrainExe\Core\Core; |
8
|
|
|
use BrainExe\Core\DependencyInjection\CompilerPass\GlobalCompilerPass; |
9
|
|
|
use Symfony\Bridge\ProxyManager\LazyProxy\PhpDumper\ProxyDumper; |
10
|
|
|
use Symfony\Component\DependencyInjection\Container; |
11
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
12
|
|
|
use Symfony\Component\DependencyInjection\Dumper\PhpDumper; |
13
|
|
|
use Symfony\Component\Finder\Finder; |
14
|
|
|
use Symfony\Component\Finder\SplFileInfo; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @Service("Core.Rebuild", public=false) |
18
|
|
|
*/ |
19
|
|
|
class Rebuild |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param bool $boot |
24
|
|
|
* @return Container|ContainerBuilder |
25
|
|
|
*/ |
26
|
2 |
|
public function rebuildDIC($boot = true) |
27
|
|
|
{ |
28
|
2 |
|
$containerBuilder = new ContainerBuilder(); |
29
|
|
|
|
30
|
2 |
|
$this->readAnnotations($containerBuilder); |
31
|
|
|
|
32
|
2 |
|
$containerBuilder->addCompilerPass(new GlobalCompilerPass()); |
33
|
2 |
|
$containerBuilder->compile(); |
34
|
2 |
|
$this->dumpContainer($containerBuilder); |
35
|
|
|
|
36
|
2 |
|
if ($boot) { |
37
|
1 |
|
$core = new Core(); |
38
|
1 |
|
return $core->boot(); |
39
|
|
|
} |
40
|
|
|
|
41
|
1 |
|
return $containerBuilder; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param ContainerBuilder $container |
46
|
|
|
*/ |
47
|
2 |
|
protected function readAnnotations(ContainerBuilder $container) |
48
|
|
|
{ |
49
|
2 |
|
$annotationLoader = new Loader($container); |
50
|
2 |
|
$appFinder = new Finder(); |
51
|
|
|
|
52
|
2 |
|
$appFinder->directories() |
53
|
2 |
|
->in([ROOT . 'vendor/brainexe/']) |
54
|
2 |
|
->depth("<=1") |
|
|
|
|
55
|
2 |
|
->name('src'); |
56
|
|
|
|
57
|
2 |
|
$annotationLoader->load('src/'); |
58
|
|
|
|
59
|
2 |
|
foreach ($appFinder as $dir) { |
60
|
|
|
/** @var SplFileInfo $dir */ |
61
|
2 |
|
$dirName = $dir->getPathname(); |
62
|
2 |
|
$annotationLoader->load($dirName); |
63
|
|
|
} |
64
|
2 |
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param ContainerBuilder $container |
68
|
|
|
*/ |
69
|
2 |
|
protected function dumpContainer(ContainerBuilder $container) |
70
|
|
|
{ |
71
|
2 |
|
$randomId = mt_rand(); |
72
|
2 |
|
$className = sprintf('dic_%d', $randomId); |
73
|
2 |
|
$containerFile = 'cache/dic.php'; |
74
|
|
|
|
75
|
2 |
|
$dumper = new PhpDumper($container); |
76
|
2 |
|
$dumper->setProxyDumper(new ProxyDumper()); |
77
|
|
|
|
78
|
2 |
|
$containerContent = $dumper->dump([ |
79
|
2 |
|
'class' => $className, |
80
|
2 |
|
'debug' => $container->getParameter('debug') |
81
|
2 |
|
]); |
82
|
|
|
|
83
|
2 |
|
file_put_contents('cache/dic.php', $containerContent); |
84
|
2 |
|
file_put_contents('cache/dic.txt', $className); |
85
|
2 |
|
@chmod($containerFile, 0777); |
|
|
|
|
86
|
|
|
|
87
|
2 |
|
$debug = $container->getParameter('debug'); |
88
|
2 |
|
file_put_contents( |
89
|
|
|
ROOT . 'cache/config.json', |
90
|
|
|
json_encode( |
91
|
2 |
|
$container->getParameterBag()->all(), |
92
|
|
|
$debug ? JSON_PRETTY_PRINT : null |
93
|
|
|
) |
94
|
|
|
); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
PHP provides two ways to mark string literals. Either with single quotes
'literal'
or with double quotes"literal"
. The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (
\'
) and the backslash (\\
). Every other character is displayed as is.Double quoted string literals may contain other variables or more complex escape sequences.
will print an indented:
Single is Value
If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.
For more information on PHP string literals and available escape sequences see the PHP core documentation.