| 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 |  |  | use Drone\Mvc\Exception; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * ModuleFactory Class | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * This class creates a module instance | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | class ModuleFactory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * Creates the module instance | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * @param string             $module | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * @param array              $module_settings | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * @return null | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 29 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 30 |  |  |     public static function create($module, Array $module_settings = []) | 
            
                                                        
            
                                    
            
            
                | 31 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 32 |  |  |         if (empty($module)) | 
            
                                                        
            
                                    
            
            
                | 33 |  |  |             throw new \RuntimeException("The module name must be specified"); | 
            
                                                        
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 35 |  |  |         if (!is_string($module)) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 36 |  |  |             throw new \InvalidArgumentException("Invalid type given. string expected"); | 
            
                                                        
            
                                    
            
            
                | 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 |  |  |         $fqn_module = "\\" . $module . "\\Module"; | 
            
                                                        
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 47 |  |  |         if (!class_exists($fqn_module)) | 
            
                                                        
            
                                    
            
            
                | 48 |  |  |             throw new Exception\ModuleNotFoundException("The module class '$fqn_module' does not exists!"); | 
            
                                                        
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 50 |  |  |         $module = new $fqn_module($module); | 
            
                                                        
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 52 |  |  |         if (array_key_exists('path', $module_settings)) | 
            
                                                        
            
                                    
            
            
                | 53 |  |  |             $module->setModulePath($module_settings["path"]); | 
            
                                                        
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 55 |  |  |         if (array_key_exists('classes', $module_settings)) | 
            
                                                        
            
                                    
            
            
                | 56 |  |  |             $module->setClassPath($module_settings["classes"]); | 
            
                                                        
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 58 |  |  |         if (array_key_exists('views', $module_settings)) | 
            
                                                        
            
                                    
            
            
                | 59 |  |  |             $module->setViewPath($module_settings["views"]); | 
            
                                                        
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 61 |  |  |         if (array_key_exists('config', $module_settings) && !is_null($module_settings["config"])) | 
            
                                                        
            
                                    
            
            
                | 62 |  |  |             $module->setConfigFile($module_settings["config"]); | 
            
                                                        
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 64 |  |  |         return $module; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 65 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 66 |  |  | } |