1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* The MIT License |
5
|
|
|
* |
6
|
|
|
* Copyright 2016 Alejandro Peña Florentín ([email protected]). |
7
|
|
|
* |
8
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
9
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
10
|
|
|
* in the Software without restriction, including without limitation the rights |
11
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
12
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
13
|
|
|
* furnished to do so, subject to the following conditions: |
14
|
|
|
* |
15
|
|
|
* The above copyright notice and this permission notice shall be included in |
16
|
|
|
* all copies or substantial portions of the Software. |
17
|
|
|
* |
18
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
19
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
20
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
21
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
22
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
23
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
24
|
|
|
* THE SOFTWARE. |
25
|
|
|
*/ |
26
|
|
|
|
27
|
|
|
namespace Tight\Modules; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* ModuleLoader class |
31
|
|
|
* |
32
|
|
|
* @author Alejandro Peña Florentín ([email protected]) |
33
|
|
|
*/ |
34
|
|
|
class ModuleLoader |
35
|
|
|
{ |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var array Modules added |
39
|
|
|
*/ |
40
|
|
|
private $modules = []; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var \Tight\Tight Tight app |
44
|
|
|
*/ |
45
|
|
|
private $app; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Constructor |
49
|
|
|
* @param \Tight\Tight $app Tight app |
50
|
|
|
*/ |
51
|
|
|
public function __construct(\Tight\Tight $app) { |
52
|
|
|
$this->app = $app; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Adds a new module |
57
|
|
|
* @param \Tight\Modules\AbstractModule $module Module |
58
|
|
|
* @throws \Tight\Modules\ModuleException If the module is already added |
59
|
|
|
*/ |
60
|
|
|
public function add(\Tight\Modules\AbstractModule $module) { |
61
|
|
|
if (!isset($this->modules[$module->getModuleName()])) { |
62
|
|
|
$this->modules[$module->getModuleName()] = $module; |
63
|
|
|
} else { |
64
|
|
|
throw new \Tight\Modules\ModuleException("Module <strong>" . $module->getModuleName() . "</strong> already exists"); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Removes a module |
70
|
|
|
* @param string $moduleName Module name |
71
|
|
|
* @return boolean TRUE on success, FALSE if the module does not exists |
72
|
|
|
*/ |
73
|
|
|
public function remove($moduleName) { |
74
|
|
|
if (isset($this->modules[$moduleName])) { |
75
|
|
|
unset($this->modules[$moduleName]); |
76
|
|
|
return true; |
77
|
|
|
} else { |
78
|
|
|
return false; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Gets all the modules added |
84
|
|
|
* @return array Modules |
85
|
|
|
*/ |
86
|
|
|
public function getModules() { |
87
|
|
|
return $this->modules; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Gets a single module |
92
|
|
|
* @param string $moduleName Module name |
93
|
|
|
* @return \Tight\Module\AbstractModule Module or null if the module name |
94
|
|
|
* cant be found |
95
|
|
|
*/ |
96
|
|
|
public function getModule($moduleName) { |
97
|
|
|
if (isset($this->modules[$moduleName])) { |
98
|
|
|
return $this->modules[$moduleName]; |
99
|
|
|
} else { |
100
|
|
|
return null; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
} |
105
|
|
|
|