-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bundles.php
128 lines (112 loc) · 2.94 KB
/
Bundles.php
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
<?php
namespace Rad\Core;
use Composer\Autoload\ClassLoader;
use InvalidArgumentException;
use Rad\Core\Exception\MissingBundleException;
use Rad\Utility\Inflection;
/**
* Bundles Loader
*
* @package Rad\Core
*/
class Bundles
{
/**
* @var ClassLoader
*/
protected static $classLoader;
/**
* @var array
*/
protected static $bundlesLoaded = [];
/**
* Load bundle
*
* @param BundleInterface $bundle
*
* @throws MissingBundleException
*/
public static function load(BundleInterface $bundle)
{
if (is_dir($bundle->getPath())) {
self::$bundlesLoaded[$bundle->getName()] = [
'namespace' => $bundle->getNamespace(),
'path' => $bundle->getPath()
];
if (!self::$classLoader) {
self::$classLoader = new ClassLoader();
}
self::$classLoader->addPsr4($bundle->getNamespace(), $bundle->getPath());
self::$classLoader->register();
} else {
throw new MissingBundleException(sprintf('Bundle "%s" could not be found.', $bundle->getName()));
}
}
/**
* Load all bundles
*
* @param array $bundles
*
* @throws MissingBundleException
*/
public static function loadAll(array $bundles)
{
foreach ($bundles as $bundle) {
if (!$bundle instanceof BundleInterface) {
throw new InvalidArgumentException('Bundle must be instance of "Rad\Core\BundleInterface".');
}
self::load($bundle);
}
}
/**
* Check bundle is loaded
*
* @param string $bundleName Bundle name
*
* @return bool
*/
public static function isLoaded($bundleName)
{
$bundleName = Inflection::camelize($bundleName);
return isset(self::$bundlesLoaded[$bundleName]);
}
/**
* Get all loaded bundles
*
* @return array
*/
public static function getLoaded()
{
return array_keys(self::$bundlesLoaded);
}
/**
* Get bundle namespace
*
* @param string $bundleName Bundle name
*
* @return string
* @throws MissingBundleException
*/
public static function getNamespace($bundleName)
{
if (isset(self::$bundlesLoaded[$bundleName])) {
return self::$bundlesLoaded[$bundleName]['namespace'];
}
throw new MissingBundleException(sprintf('Bundle "%s" could not be found.', $bundleName));
}
/**
* Get bundle path
*
* @param string $bundleName Bundle name
*
* @return string
* @throws MissingBundleException
*/
public static function getPath($bundleName)
{
if (isset(self::$bundlesLoaded[$bundleName])) {
return self::$bundlesLoaded[$bundleName]['path'];
}
throw new MissingBundleException(sprintf('Bundle "%s" could not be found.', $bundleName));
}
}