Ejemplo n.º 1
0
 /**
  * Reads all modules and initializes the main ones.
  */
 public function loadModules()
 {
     if (!$this->enabled) {
         return;
     }
     // Initialize structures
     $this->moduleHandlerLog = "Loading modules...\n";
     $this->allModules = array();
     $this->activeModules = $this->db->getActiveModules();
     // array
     // Iterate through the modules folder and generate the module references for the active modules.
     $basedir = dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . CRM_MODULES_BASEDIR;
     $this->moduleHandlerLog .= "Looking for modules in path {$basedir}\n";
     $files = scandir($basedir);
     foreach ($files as $filename) {
         // iterate throuhg files/directories.
         $realpath = $basedir . DIRECTORY_SEPARATOR . $filename;
         // If it's a directory (except for "." & "..")
         if (is_dir($realpath) && substr($filename, 0, 1) !== '.') {
             // possible module.
             $this->moduleHandlerLog .= "Analyzing {$filename}...\n";
             $mainModuleFilePath = $realpath . DIRECTORY_SEPARATOR . CRM_MODULES_MAIN_FILENAME;
             $classHierarchy = \creamy\ModuleHandler::getClassHierarchyInFile($mainModuleFilePath);
             $this->moduleHandlerLog .= "Class hierarchy: " . var_export($classHierarchy, true) . "\n";
             if (is_array($classHierarchy) && count($classHierarchy) > 0) {
                 $classes = isset($classHierarchy["classes"]) ? $classHierarchy["classes"] : array();
                 $namespace = isset($classHierarchy["namespace"]) ? $classHierarchy["namespace"] : null;
                 // Look for a valid module class.
                 foreach ($classes as $class) {
                     if (strtolower($class["type"]) == "class") {
                         // we have a class here.
                         try {
                             // try to generate the module definition and add it to the modules.
                             $classname = $class["name"];
                             $this->moduleHandlerLog .= "Instantiating module with short name: {$filename}, file path: {$mainModuleFilePath}, class name: {$classname}, namespace: {$namespace}\n";
                             $def = new \creamy\ModuleReference($filename, $mainModuleFilePath, $classname, $namespace);
                             $this->allModules[$classname] = $def;
                             $this->moduleHandlerLog .= "Successfully loaded module {$classname} from {$realpath}\n";
                             break;
                             // success. We don't need to look any further in this file.
                         } catch (\Exception $exception) {
                             // Log module loading failure.
                             $this->moduleHandlerLog .= "Unable to load module " . $class["name"] . " from {$realpath}: " . $exception->getMessage() . "\n";
                         }
                     }
                 }
             }
         }
     }
     //error_log("Module loading process:\n".$this->moduleHandlerLog);
 }