|
1
|
|
|
<?php |
|
2
|
|
|
namespace Flynt; |
|
3
|
|
|
|
|
4
|
|
|
class ComponentManager |
|
5
|
|
|
{ |
|
6
|
|
|
|
|
7
|
|
|
protected $components = []; |
|
8
|
|
|
protected static $instance = null; |
|
9
|
|
|
|
|
10
|
|
|
public static function getInstance() |
|
11
|
|
|
{ |
|
12
|
|
|
if (null === self::$instance) { |
|
13
|
|
|
self::$instance = new self; |
|
14
|
|
|
} |
|
15
|
|
|
return self::$instance; |
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* clone |
|
20
|
|
|
* |
|
21
|
|
|
* Prevent cloning with 'protected' keyword |
|
22
|
|
|
**/ |
|
23
|
|
|
protected function __clone() |
|
24
|
|
|
{ |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* constructor |
|
29
|
|
|
* |
|
30
|
|
|
* Prevent instantiation with 'protected' keyword |
|
31
|
|
|
**/ |
|
32
|
|
|
protected function __construct() |
|
33
|
|
|
{ |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function registerComponent($componentName, $componentPath = null) |
|
37
|
|
|
{ |
|
38
|
|
|
// check if component already registered |
|
39
|
|
|
if ($this->isRegistered($componentName)) { |
|
40
|
|
|
trigger_error("Component {$componentName} is already registered!", E_USER_WARNING); |
|
41
|
|
|
return false; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
// register component / require functions.php |
|
45
|
|
|
$componentPath = trailingslashit(apply_filters('Flynt/componentPath', $componentPath, $componentName)); |
|
46
|
|
|
|
|
47
|
|
|
// add component to internal list (array) |
|
48
|
|
|
$this->add($componentName, $componentPath); |
|
49
|
|
|
|
|
50
|
|
|
do_action('Flynt/registerComponent', $componentName); |
|
51
|
|
|
do_action("Flynt/registerComponent?name={$componentName}", $componentName); |
|
52
|
|
|
|
|
53
|
|
|
return true; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function getComponentFilePath($componentName, $fileName = 'index.php') |
|
57
|
|
|
{ |
|
58
|
|
|
$componentDir = $this->getComponentDirPath($componentName); |
|
59
|
|
|
|
|
60
|
|
|
if (false === $componentDir) { |
|
61
|
|
|
return false; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
// dir path already has a trailing slash |
|
65
|
|
|
$filePath = $componentDir . $fileName; |
|
66
|
|
|
|
|
67
|
|
|
return is_file($filePath) ? $filePath : false; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public function getComponentDirPath($componentName) |
|
71
|
|
|
{ |
|
72
|
|
|
$dirPath = $this->get($componentName); |
|
73
|
|
|
|
|
74
|
|
|
// check if dir exists |
|
75
|
|
|
if (!is_dir($dirPath)) { |
|
76
|
|
|
return false; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return $dirPath; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
protected function add($name, $path) |
|
83
|
|
|
{ |
|
84
|
|
|
$this->components[$name] = $path; |
|
85
|
|
|
return true; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function get($componentName) |
|
89
|
|
|
{ |
|
90
|
|
|
// check if component exists / is registered |
|
91
|
|
|
if (!$this->isRegistered($componentName)) { |
|
92
|
|
|
trigger_error("Cannot get component: Component '{$componentName}' is not registered!", E_USER_WARNING); |
|
93
|
|
|
return false; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
return $this->components[$componentName]; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
public function remove($componentName) |
|
100
|
|
|
{ |
|
101
|
|
|
unset($this->components[$componentName]); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public function getAll() |
|
105
|
|
|
{ |
|
106
|
|
|
return $this->components; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
public function removeAll() |
|
110
|
|
|
{ |
|
111
|
|
|
$this->components = []; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
public function isRegistered($componentName) |
|
115
|
|
|
{ |
|
116
|
|
|
return array_key_exists($componentName, $this->components); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|