|
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\Config\Container; |
|
16
|
|
|
use Fuel\Foundation\Exception\ComponentLoad; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Keeps track of fuel's components |
|
20
|
|
|
* |
|
21
|
|
|
* @package Fuel\Foundation |
|
22
|
|
|
*/ |
|
23
|
|
|
class ComponentManager implements ComponentManagerInterface |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* Name of the component class to look for. |
|
27
|
|
|
* |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
protected static $componentClassName = 'FuelComponent'; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var ComponentInterface[] |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $loadedComponents = []; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var Container |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $configContainer; |
|
41
|
|
|
|
|
42
|
10 |
|
public function __construct(Container $configContainer) |
|
43
|
|
|
{ |
|
44
|
10 |
|
$this->configContainer = $configContainer; |
|
45
|
10 |
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* {@inheritdoc} |
|
49
|
|
|
*/ |
|
50
|
1 |
|
public function get(string $name) : ComponentInterface |
|
51
|
|
|
{ |
|
52
|
1 |
|
if ($this->loaded($name)) |
|
53
|
|
|
{ |
|
54
|
1 |
|
return $this->loadedComponents[$name]; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
1 |
|
return $this->load($name); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* {@inheritdoc} |
|
62
|
|
|
*/ |
|
63
|
5 |
|
public function load(string $name) : ComponentInterface |
|
64
|
|
|
{ |
|
65
|
5 |
|
$fullName = $name . '\\' . static::$componentClassName; |
|
66
|
|
|
|
|
67
|
|
|
// Check if component class exists |
|
68
|
5 |
|
if ( ! class_exists($fullName)) |
|
69
|
|
|
{ |
|
70
|
1 |
|
throw new ComponentLoad("FOU-001: Unable to load [$fullName]: Class not found"); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
// Check if it implements the correct interface |
|
74
|
4 |
|
if ( ! in_array('Fuel\Foundation\ComponentInterface', class_implements($fullName))) |
|
75
|
|
|
{ |
|
76
|
1 |
|
throw new ComponentLoad("FOU-002: Unable to load [$fullName]: Does not implement ComponentInterface"); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
// Load the component |
|
80
|
3 |
|
$component = new $fullName(); |
|
81
|
|
|
|
|
82
|
3 |
|
$this->loadedComponents[$name] = $component; |
|
83
|
|
|
|
|
84
|
3 |
|
$this->addConfigPath($component); |
|
85
|
|
|
|
|
86
|
3 |
|
return $component; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
3 |
|
protected function addConfigPath(ComponentInterface $component) |
|
90
|
|
|
{ |
|
91
|
3 |
|
$this->configContainer->addPath($component->getConfigPath()); |
|
92
|
3 |
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* {@inheritdoc} |
|
96
|
|
|
*/ |
|
97
|
3 |
|
public function loaded(string $name) : bool |
|
98
|
|
|
{ |
|
99
|
3 |
|
return isset($this->loadedComponents[$name]); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* {@inheritdoc} |
|
104
|
|
|
*/ |
|
105
|
1 |
|
public function unload(string $name) : ComponentManagerInterface |
|
106
|
|
|
{ |
|
107
|
1 |
|
if ($this->loaded($name)) |
|
108
|
|
|
{ |
|
109
|
1 |
|
unset($this->loadedComponents[$name]); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
1 |
|
return $this; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
} |
|
116
|
|
|
|