1 | <?php |
||
11 | class Module |
||
12 | { |
||
13 | /** |
||
14 | * @var string $pathName Module's name |
||
15 | */ |
||
16 | protected $pathName = ''; |
||
17 | |||
18 | /** |
||
19 | * @var \BFW\Config $config Config object for this module |
||
20 | */ |
||
21 | protected $config; |
||
22 | |||
23 | /** |
||
24 | * @var \stdClass $installInfos All install informations for this module |
||
25 | */ |
||
26 | protected $installInfos; |
||
27 | |||
28 | /** |
||
29 | * @var \stdClass $loadInfos All informations about the module running |
||
30 | */ |
||
31 | protected $loadInfos; |
||
32 | |||
33 | /** |
||
34 | * |
||
35 | * @var \stdClass $status Load and run status |
||
36 | */ |
||
37 | protected $status; |
||
38 | |||
39 | /** |
||
40 | * Constructor |
||
41 | * Load all informations if $loadModule is true |
||
42 | * |
||
43 | * @param string $pathName Module name |
||
44 | * @param boolean $loadModule (default true) If run load information |
||
45 | */ |
||
46 | public function __construct($pathName, $loadModule = true) |
||
47 | { |
||
48 | $this->pathName = $pathName; |
||
49 | |||
50 | if ($loadModule === true) { |
||
51 | $this->loadModule(); |
||
52 | } |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * Load informations about the module |
||
57 | * |
||
58 | * @return void |
||
59 | */ |
||
60 | public function loadModule() |
||
71 | |||
72 | /** |
||
73 | * Get installation informations |
||
74 | * |
||
75 | * @param string $sourceFiles Path to module source (in vendor) |
||
76 | * |
||
77 | * @return \stdClass |
||
78 | */ |
||
79 | public static function installInfos($sourceFiles) |
||
80 | { |
||
81 | $currentClass = get_called_class(); //Allow extends |
||
82 | return $currentClass::loadJsonFile($sourceFiles.'/bfwModulesInfos.json'); |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * Get the module's name |
||
87 | * |
||
88 | * @return string |
||
89 | */ |
||
90 | public function getPathName() |
||
91 | { |
||
92 | return $this->pathName; |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * Get the Config object which have config for this module |
||
97 | * |
||
98 | * @return \BFW\Config |
||
99 | */ |
||
100 | public function getConfig() |
||
101 | { |
||
102 | return $this->config; |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * Get the installation informations |
||
107 | * |
||
108 | * @return \stdClass |
||
109 | */ |
||
110 | public function getInstallInfos() |
||
111 | { |
||
112 | return $this->installInfos; |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * Get the load informations |
||
117 | * |
||
118 | * @return \stdClass |
||
119 | */ |
||
120 | public function getLoadInfos() |
||
121 | { |
||
122 | return $this->loadInfos; |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * Get the status object for this module |
||
127 | * |
||
128 | * @return \stdClass |
||
129 | */ |
||
130 | public function getStatus() |
||
131 | { |
||
132 | return $this->status; |
||
133 | } |
||
134 | |||
135 | /** |
||
136 | * Return the load status |
||
137 | * |
||
138 | * @return boolean |
||
139 | */ |
||
140 | public function isLoaded() |
||
141 | { |
||
142 | return $this->status->load; |
||
143 | } |
||
144 | |||
145 | /** |
||
146 | * Return the run status |
||
147 | * |
||
148 | * @return boolean |
||
149 | */ |
||
150 | public function isRun() |
||
151 | { |
||
152 | return $this->status->run; |
||
153 | } |
||
154 | |||
155 | /** |
||
156 | * Instantiate the Config object to obtains module's configuration |
||
157 | * |
||
158 | * @return void |
||
159 | */ |
||
160 | public function loadConfig() |
||
161 | { |
||
162 | if (!file_exists(CONFIG_DIR.$this->pathName)) { |
||
163 | return; |
||
164 | } |
||
165 | |||
166 | $this->config = new \BFW\Config($this->pathName); |
||
167 | $this->config->loadFiles(); |
||
168 | } |
||
169 | |||
170 | /** |
||
171 | * Get load information from json file |
||
172 | * |
||
173 | * @return void |
||
174 | */ |
||
175 | public function loadModuleInfos() |
||
184 | |||
185 | /** |
||
186 | * Read a json file and return datas in json |
||
187 | * |
||
188 | * @param string $jsonFilePath : The path to the file to read |
||
189 | * |
||
190 | * @return mixed Json parsed datas |
||
191 | * |
||
192 | * @throws Exception If the file is not found or for a json parser error |
||
193 | */ |
||
194 | protected static function loadJsonFile($jsonFilePath) |
||
207 | |||
208 | /** |
||
209 | * Add a dependency to module |
||
210 | * Used by needMe property in module infos |
||
211 | * |
||
212 | * @param string $dependencyName The dependency name to add |
||
213 | * |
||
214 | * @return $this |
||
215 | */ |
||
216 | public function addDependency($dependencyName) |
||
230 | |||
231 | /** |
||
232 | * Get path to the runner file |
||
233 | * |
||
234 | * @return string |
||
235 | * |
||
236 | * @throws Exception If the file not exists |
||
237 | */ |
||
238 | protected function getRunnerFile() |
||
263 | |||
264 | /** |
||
265 | * Run the module in a closure |
||
266 | * |
||
267 | * @return void |
||
268 | */ |
||
269 | public function runModule() |
||
285 | } |
||
286 |