Beispiel #1
0
 /**
  * Factory method. Used to instantiate a subclass
  * @param string $id, the module id to load
  * @param string $type, the type of module to load (web/api)
  */
 public static function factory($id, $type = null)
 {
     if ($id == 'kurogo') {
         set_exception_handler("exceptionHandlerForError");
     }
     if (!self::isValidModuleName($id)) {
         throw new KurogoException(Kurogo::getLocalizedString('ERROR_INVALID_MODULE'));
     }
     Kurogo::log(LOG_INFO, "Initializing {$type} module {$id}", 'module');
     $configModule = $id;
     $configStore = Kurogo::configStore();
     //attempt to load config/$id/module.ini
     if ($moduleData = $configStore->getOptionalSection('module', 'module', $configModule)) {
         $id = Kurogo::arrayVal($moduleData, 'id', $configModule);
     } else {
         throw new KurogoModuleNotFound(Kurogo::getLocalizedString('ERROR_MODULE_NOT_FOUND', $id));
     }
     // see if the class location has been cached
     if ($moduleFile = Kurogo::getCache(self::cacheKey($id, $type))) {
         $className = basename($moduleFile, '.php');
         include_once $moduleFile;
         $module = new $className();
         if (is_a($module, KurogoWebBridge::STUB_API_CLASS)) {
             $module->setID($id);
         }
         $module->setConfigModule($configModule);
         Kurogo::addModuleLib($id);
         return $module;
     }
     // when run without a type it will find either
     $classNames = array('web' => ucfirst($id) . 'WebModule', 'api' => ucfirst($id) . 'APIModule', 'shell' => ucfirst($id) . 'ShellModule');
     // if we specified a type, include only that type in the array
     if ($type) {
         if (isset($classNames[$type])) {
             $classNames = array($classNames[$type]);
         } else {
             throw new KurogoException("Invalid module type {$type}");
         }
     }
     // possible module paths.
     // 1. Site Folder SiteMODULEIDXXXModule
     // 2. Site Folder MODULEIDXXXModule
     // 3. Project folder MODULEIDXXXModule
     // Note: The PHP class name MUST be the basename of the file path.
     $modulePaths = array(SITE_MODULES_DIR . "/{$id}/Site%s.php", SITE_MODULES_DIR . "/{$id}/%s.php", SHARED_MODULES_DIR . "/{$id}/Site%s.php", SHARED_MODULES_DIR . "/{$id}/%s.php", MODULES_DIR . "/{$id}/%s.php");
     if ($type == 'api' && KurogoWebBridge::moduleHasMediaAssets($configModule)) {
         $modulePaths[] = LIB_DIR . '/' . KurogoWebBridge::STUB_API_CLASS . ".php";
     }
     //cycle module paths
     foreach ($modulePaths as $path) {
         $className = basename($path, '.php');
         //cycle class names to find a valid module
         foreach ($classNames as $class) {
             $className = sprintf($className, $class);
             $path = sprintf($path, $class);
             Kurogo::log(LOG_DEBUG, "Looking for {$path} for {$id}", 'module');
             // see if it exists
             $moduleFile = realpath_exists($path);
             if ($moduleFile && (include_once $moduleFile)) {
                 //found it
                 $info = new ReflectionClass($className);
                 if (!$info->isAbstract()) {
                     Kurogo::log(LOG_INFO, "Found {$moduleFile} for {$id}", 'module');
                     $module = new $className();
                     if (is_a($module, KurogoWebBridge::STUB_API_CLASS)) {
                         $module->setID($id);
                     }
                     $module->setConfigModule($configModule);
                     // cache the location of the class (which also includes the classname)
                     Kurogo::setCache(self::cacheKey($id, $type), $moduleFile);
                     Kurogo::addModuleLib($id);
                     return $module;
                 }
                 Kurogo::log(LOG_NOTICE, "{$class} found at {$moduleFile} is abstract and cannot be used for {$id}", 'module');
                 return false;
             }
         }
     }
     Kurogo::log(LOG_NOTICE, "No valid {$type} class found for module {$id}", 'module');
     throw new KurogoModuleNotFound(Kurogo::getLocalizedString('ERROR_MODULE_NOT_FOUND', $id));
 }
Beispiel #2
0
 /**
  * Factory method. Used to instantiate a subclass
  * @param string $id, the module id to load
  * @param string $type, the type of module to load (web/api)
  */
 public static function factory($id, $type = null)
 {
     if ($id == 'error') {
         set_exception_handler("exceptionHandlerForError");
     }
     Kurogo::log(LOG_INFO, "Initializing {$type} module {$id}", 'module');
     $configModule = $id;
     //attempt to load config/$id/module.ini
     if ($config = ModuleConfigFile::factory($id, 'module', ModuleConfigFile::OPTION_DO_NOT_CREATE)) {
         //use the ID parameter if it's present, otherwise use the included id
         $id = $config->getOptionalVar('id', $id);
     } elseif (!Kurogo::getOptionalSiteVar('CREATE_DEFAULT_CONFIG', false, 'modules')) {
         Kurogo::log(LOG_ERR, "Module config file not found for module {$id}", 'module');
         throw new KurogoModuleNotFound(Kurogo::getLocalizedString('ERROR_MODULE_NOT_FOUND', $id));
     }
     // see if the class location has been cached
     if ($moduleFile = Kurogo::getCache(self::cacheKey($id, $type))) {
         $className = basename($moduleFile, '.php');
         include_once $moduleFile;
         $module = new $className();
         if (is_a($module, KurogoWebBridge::STUB_API_CLASS)) {
             $module->setID($id);
         }
         $module->setConfigModule($configModule);
         if ($config) {
             $module->setConfig('module', $config);
         }
         Kurogo::addModuleLib($id);
         return $module;
     }
     // when run without a type it will find either
     $classNames = array('web' => ucfirst($id) . 'WebModule', 'api' => ucfirst($id) . 'APIModule', 'shell' => ucfirst($id) . 'ShellModule');
     // if we specified a type, include only that type in the array
     if ($type) {
         if (isset($classNames[$type])) {
             $classNames = array($classNames[$type]);
         } else {
             throw new KurogoException("Invalid module type {$type}");
         }
     }
     // possible module paths.
     // 1. Site Folder SiteMODULEIDXXXModule
     // 2. Site Folder MODULEIDXXXModule
     // 3. Project folder MODULEIDXXXModule
     $modulePaths = array(SITE_MODULES_DIR . "/{$id}/Site%s.php" => "Site%s", SITE_MODULES_DIR . "/{$id}/%s.php" => "%s", MODULES_DIR . "/{$id}/%s.php" => "%s");
     if ($type == 'api' && KurogoWebBridge::moduleHasMediaAssets($configModule)) {
         $modulePaths[KurogoWebBridge::STUB_API_CLASS_FILE] = KurogoWebBridge::STUB_API_CLASS;
     }
     //cycle module paths
     foreach ($modulePaths as $path => $className) {
         //cycle class names to find a valid module
         foreach ($classNames as $class) {
             $className = sprintf($className, $class);
             $path = sprintf($path, $class);
             Kurogo::log(LOG_DEBUG, "Looking for {$path} for {$id}", 'module');
             // see if it exists
             $moduleFile = realpath_exists($path);
             if ($moduleFile && (include_once $moduleFile)) {
                 //found it
                 $info = new ReflectionClass($className);
                 if (!$info->isAbstract()) {
                     Kurogo::log(LOG_INFO, "Found {$moduleFile} for {$id}", 'module');
                     $module = new $className();
                     if (is_a($module, KurogoWebBridge::STUB_API_CLASS)) {
                         $module->setID($id);
                     }
                     $module->setConfigModule($configModule);
                     if ($config) {
                         $module->setConfig('module', $config);
                     }
                     // cache the location of the class (which also includes the classname)
                     Kurogo::setCache(self::cacheKey($id, $type), $moduleFile);
                     Kurogo::addModuleLib($id);
                     return $module;
                 }
                 Kurogo::log(LOG_NOTICE, "{$class} found at {$moduleFile} is abstract and cannot be used for {$id}", 'module');
                 return false;
             }
         }
     }
     Kurogo::log(LOG_ERR, "No valid class found for module {$id}", 'module');
     throw new KurogoModuleNotFound(Kurogo::getLocalizedString('ERROR_MODULE_NOT_FOUND', $id));
 }