1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* balloon |
7
|
|
|
* |
8
|
|
|
* @copyright Copryright (c) 2012-2019 gyselroth GmbH (https://gyselroth.com) |
9
|
|
|
* @license GPL-3.0 https://opensource.org/licenses/GPL-3.0 |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Balloon\Bootstrap; |
13
|
|
|
|
14
|
|
|
use Composer\Autoload\ClassLoader as Composer; |
15
|
|
|
use Micro\Container\Container; |
16
|
|
|
use Noodlehaus\Config; |
17
|
|
|
use Psr\Container\ContainerInterface; |
18
|
|
|
|
19
|
|
|
class ContainerBuilder |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Init bootstrap. |
23
|
|
|
*/ |
24
|
|
|
public static function get(Composer $composer): ContainerInterface |
25
|
|
|
{ |
26
|
|
|
$config = self::loadConfig(); |
27
|
|
|
$config = self::detectApps($config, $composer); |
28
|
|
|
$container = new Container($config); |
29
|
|
|
|
30
|
|
|
return $container; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Load config. |
35
|
|
|
*/ |
36
|
|
|
protected static function loadConfig(): Config |
37
|
|
|
{ |
38
|
|
|
$configs[] = __DIR__.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'.container.config.php'; |
|
|
|
|
39
|
|
|
|
40
|
|
|
foreach (glob(constant('BALLOON_CONFIG_DIR').DIRECTORY_SEPARATOR.'*.yaml') as $path) { |
41
|
|
|
clearstatcache(true, $path); |
42
|
|
|
$configs[] = $path; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
return new Config($configs); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Find apps. |
50
|
|
|
*/ |
51
|
|
|
protected static function detectApps(Config $master, Composer $composer): Config |
52
|
|
|
{ |
53
|
|
|
$apps = []; |
54
|
|
|
$configs = []; |
|
|
|
|
55
|
|
|
|
56
|
|
|
foreach (glob(constant('BALLOON_PATH').DIRECTORY_SEPARATOR.'src'.DIRECTORY_SEPARATOR.'app'.DIRECTORY_SEPARATOR.'*') as $app) { |
57
|
|
|
$name = basename($app); |
58
|
|
|
$ns = str_replace('.', '\\', $name).'\\'; |
59
|
|
|
$composer->addPsr4($ns, $app); |
60
|
|
|
|
61
|
|
|
if (!file_exists($app.DIRECTORY_SEPARATOR.'.container.config.php')) { |
62
|
|
|
continue; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$file = $app.DIRECTORY_SEPARATOR.'.container.config.php'; |
66
|
|
|
$load_master = isset($master['Apps'][$name]['enabled']) ? $master['Apps'][$name]['enabled'] : null; |
67
|
|
|
|
68
|
|
|
if ($load_master === false) { |
69
|
|
|
continue; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$app = new Config($file); |
73
|
|
|
$load = isset($app['Apps'][$name]['enabled']) ? $app['Apps'][$name]['enabled'] : null; |
74
|
|
|
|
75
|
|
|
if ($load_master === null && $load === null || $load_master === true || $load_master === null && $load === true) { |
76
|
|
|
$apps[] = $app; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$new = new Config([]); |
81
|
|
|
foreach ($apps as $app) { |
82
|
|
|
$new->merge($app); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$new->merge($master); |
86
|
|
|
|
87
|
|
|
return $new; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.