1 | <?php |
||
2 | /** |
||
3 | * DronePHP (http://www.dronephp.com) |
||
4 | * |
||
5 | * @link http://github.com/Pleets/DronePHP |
||
6 | * @copyright Copyright (c) 2016-2018 Pleets. (http://www.pleets.org) |
||
7 | * @license http://www.dronephp.com/license |
||
8 | * @author Darío Rivera <[email protected]> |
||
9 | */ |
||
10 | |||
11 | namespace Drone\Mvc; |
||
12 | |||
13 | /** |
||
14 | * ModuleFactory Class |
||
15 | * |
||
16 | * This class creates a module instance |
||
17 | */ |
||
18 | class ModuleFactory |
||
19 | { |
||
20 | /** |
||
21 | * Creates the module instance |
||
22 | * |
||
23 | * @param string $module |
||
24 | * @param array $module_settings |
||
25 | * |
||
26 | * @return null |
||
27 | */ |
||
28 | 3 | public static function create($module, array $module_settings = []) |
|
29 | { |
||
30 | 3 | if (empty($module)) { |
|
31 | throw new \RuntimeException("The module name must be specified"); |
||
32 | } |
||
33 | |||
34 | 3 | if (!is_string($module)) { |
|
0 ignored issues
–
show
introduced
by
![]() |
|||
35 | throw new \InvalidArgumentException("Invalid type given. string expected"); |
||
36 | } |
||
37 | |||
38 | /* |
||
39 | * Module class instantiation |
||
40 | * |
||
41 | * Each module must have a class called Module in her namespace. This class |
||
42 | * is initilized here and can change the behavior of a controller using |
||
43 | * allowExecution() or disallowExecution(). |
||
44 | */ |
||
45 | 3 | $fqn_module = "\\" . $module . "\\Module"; |
|
46 | |||
47 | 3 | if (!class_exists($fqn_module)) { |
|
48 | throw new Exception\ModuleNotFoundException("The module class '$fqn_module' does not exists!"); |
||
49 | } |
||
50 | |||
51 | 3 | $module = new $fqn_module($module); |
|
52 | |||
53 | 3 | if (array_key_exists('config', $module_settings) && !is_null($module_settings["config"])) { |
|
54 | 2 | $module->setConfigFile($module_settings["config"]); |
|
55 | } |
||
56 | |||
57 | 3 | return $module; |
|
0 ignored issues
–
show
|
|||
58 | } |
||
59 | } |
||
60 |