1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package Fuel\Foundation |
4
|
|
|
* @version 2.0 |
5
|
|
|
* @author Fuel Development Team |
6
|
|
|
* @license MIT License |
7
|
|
|
* @copyright 2010 - 2016 Fuel Development Team |
8
|
|
|
* @link http://fuelphp.com |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
declare(strict_types=1); |
12
|
|
|
|
13
|
|
|
namespace Fuel\Foundation; |
14
|
|
|
|
15
|
|
|
use Fuel\Foundation\Exception\ComponentLoad; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Keeps track of fuel's components |
19
|
|
|
* |
20
|
|
|
* @package Fuel\Foundation |
21
|
|
|
*/ |
22
|
|
|
class ComponentManager implements ComponentManagerInterface |
23
|
|
|
{ |
24
|
|
|
protected static $componentClassName = 'FuelComponent'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var ComponentInterface[] |
28
|
|
|
*/ |
29
|
|
|
protected $loadedComponents = []; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Contains a list of component paths for components that have already been requested, indexed by component name. |
33
|
|
|
* |
34
|
|
|
* @var string[] |
35
|
|
|
*/ |
36
|
|
|
protected $availableComponents = []; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Gets a component, loading it if needed. |
40
|
|
|
* |
41
|
|
|
* @param string $name |
42
|
|
|
* |
43
|
|
|
* @return ComponentInterface |
44
|
|
|
*/ |
45
|
1 |
|
public function get(string $name) : ComponentInterface |
46
|
|
|
{ |
47
|
1 |
|
if ($this->loaded($name)) |
48
|
|
|
{ |
49
|
1 |
|
return $this->loadedComponents[$name]; |
50
|
|
|
} |
51
|
|
|
|
52
|
1 |
|
return $this->load($name); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Loads the given component. |
57
|
|
|
* |
58
|
|
|
* TODO: Also load dependant components |
59
|
|
|
* |
60
|
|
|
* @param string $name |
61
|
|
|
* |
62
|
|
|
* @return ComponentInterface |
63
|
|
|
*/ |
64
|
4 |
|
public function load(string $name) : ComponentInterface |
65
|
|
|
{ |
66
|
4 |
|
$fullName = $name . '\\' . static::$componentClassName; |
67
|
|
|
|
68
|
|
|
// Check if component class exists |
69
|
4 |
|
if ( ! class_exists($fullName)) |
70
|
|
|
{ |
71
|
1 |
|
throw new ComponentLoad("FOU-001: Unable to load [$fullName]: Class not found"); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
// Check if it implements the correct interface |
75
|
3 |
|
if ( ! in_array('Fuel\Foundation\ComponentInterface', class_implements($fullName))) |
76
|
|
|
{ |
77
|
1 |
|
throw new ComponentLoad("FOU-002: Unable to load [$fullName]: Does not implement ComponentInterface"); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
// Load the component |
81
|
2 |
|
$component = new $fullName(); |
82
|
|
|
|
83
|
2 |
|
$this->loadedComponents[$name] = $component; |
84
|
|
|
|
85
|
2 |
|
return $component; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Loads a component by name. |
90
|
|
|
* |
91
|
|
|
* @param string $name |
92
|
|
|
* |
93
|
|
|
* @return bool |
94
|
|
|
*/ |
95
|
2 |
|
public function loaded(string $name) : bool |
96
|
|
|
{ |
97
|
2 |
|
return isset($this->loadedComponents[$name]); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Unloads a component. |
102
|
|
|
* |
103
|
|
|
* @param string $name |
104
|
|
|
* |
105
|
|
|
* @return ComponentManagerInterface |
106
|
|
|
*/ |
107
|
1 |
|
public function unload(string $name) : ComponentManagerInterface |
108
|
|
|
{ |
109
|
1 |
|
if ($this->loaded($name)) |
110
|
|
|
{ |
111
|
1 |
|
unset($this->loadedComponents[$name]); |
112
|
|
|
} |
113
|
|
|
|
114
|
1 |
|
return $this; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
} |
118
|
|
|
|