-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAkumaKernel.php
More file actions
203 lines (163 loc) · 5.93 KB
/
AkumaKernel.php
File metadata and controls
203 lines (163 loc) · 5.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<?php
namespace Akuma\Bundle\DistributionBundle;
use Akuma\Bundle\DistributionBundle\Configuration\BundleConfiguration;
use Symfony\Component\Config\Definition\Processor;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Yaml\Yaml;
abstract class AkumaKernel extends Kernel
{
const CACHE_FILE_NAME = 'bundles.map';
const BUNDLE_IDENTIFIER = 'bundle.yml';
/**
* Returns an array of bundles to register.
*
* @param array $bundles
*
* @return \Symfony\Component\HttpKernel\Bundle\BundleInterface[] An array of bundle instances
*/
public function registerBundles(array $bundles = [])
{
$cache = $this->getCacheDir() . DIRECTORY_SEPARATOR . static::CACHE_FILE_NAME;
$bundleClasses = array_map(function ($item) {
return get_class($item);
}, $bundles);
$bundles = array_combine(array_values($bundleClasses), array_values($bundles));
if (is_readable($cache)) {
$foundBundleResources = unserialize(file_get_contents($cache));
} else {
$roots = array(
implode(DIRECTORY_SEPARATOR, [$this->getRootDir(), '..', 'src']),
implode(DIRECTORY_SEPARATOR, [$this->getRootDir(), '..', 'vendor']),
);
$foundBundleResources = $this->findBundleResources($roots);
if (!file_exists(dirname($cache))) {
mkdir(dirname($cache), 0777, true);
}
file_put_contents($cache, serialize($foundBundleResources));
}
$foundBundles = $this->findBundleConfigurations($foundBundleResources);
$require = [];
foreach ($foundBundles as $config) {
$bundleClass = $config[BundleConfiguration::NODE_CLASS];
if(!array_key_exists($bundleClass, $bundles)){
$bundles[$bundleClass] = $this->createInstance($config);
}
$require = array_merge($require, $config[BundleConfiguration::NODE_REQUIRE]);
}
foreach ($require as $config){
$bundleClass = $config[BundleConfiguration::NODE_CLASS];
if(!array_key_exists($bundleClass, $bundles)){
$bundles[$bundleClass] = $this->createInstance($config);
}
}
return $bundles;
}
/**
* @param array $config
*
* @return object
*/
protected function createInstance(array $config)
{
$bundleClass = $config[BundleConfiguration::NODE_CLASS];
if ($config[BundleConfiguration::NODE_KERNEL]) {
return new $bundleClass($this);
}
return new $bundleClass;
}
/**
* @param array $config
* @param array $requireBundles
*
* @return array
*/
protected function buildRequire(array $config, array $requireBundles = [])
{
$bundles = [];
foreach ($config[BundleConfiguration::NODE_REQUIRE] as $config) {
$bundleClass = trim($config[BundleConfiguration::NODE_CLASS], '\\');
if (!array_key_exists($bundleClass, $requireBundles)) {
$bundles[$bundleClass] = $this->createInstance($config);
}
}
return $bundles;
}
/**
* @param array $roots
*
* @return array
*/
protected function findBundleResources(array $roots)
{
$resources = [];
foreach ($roots as $root) {
$root = realpath($root);
if (!$root || !is_dir($root) || !is_readable($root)) {
continue;
}
$dir = new \RecursiveDirectoryIterator(
$root,
\FilesystemIterator::FOLLOW_SYMLINKS | \FilesystemIterator::SKIP_DOTS
);
$filter = new \RecursiveCallbackFilterIterator(
$dir,
function (\SplFileInfo $current) use (&$resources) {
if (!$current->getRealPath()) {
return false;
}
$fileName = strtolower($current->getFilename());
if ($fileName === 'tests' || $current->isFile()) {
return false;
}
if (!is_dir($current->getPathname() . '/Resources')) {
return true;
} else {
$file = $current->getPathname() . '/Resources/config/' . static::BUNDLE_IDENTIFIER;
if (is_file($file) && is_readable($file)) {
$resources[] = $file;
}
return false;
}
}
);
$iterator = new \RecursiveIteratorIterator($filter);
$iterator->rewind();
}
return $resources;
}
/**
* @param array $bundleResources
*
* @return array
*
* @throws \Symfony\Component\Yaml\Exception\ParseException
*/
protected function findBundleConfigurations(array $bundleResources = [])
{
$bundleConfigs = [];
foreach ($bundleResources as $file) {
$processor = new Processor();
$config = $processor->processConfiguration(
new BundleConfiguration(),
Yaml::parse(file_get_contents($file))
);
$envs = $config[BundleConfiguration::NODE_ENVIRONMENTS];
if (!empty($envs) && !in_array($this->getEnvironment(), $envs)) {
continue;
}
$bundleConfigs[$config[BundleConfiguration::NODE_CLASS]] = $config;
}
usort($bundleConfigs, function (array $config1, array $config2) {
$b1 = $config1[BundleConfiguration::NODE_PRIORITY];
$b2 = $config2[BundleConfiguration::NODE_PRIORITY];
if ($b1 > $b2) {
return 1;
}
if ($b1 < $b2) {
return -1;
}
return 0;
});
return $bundleConfigs;
}
}