Exemple #1
0
 /**
  * Registra o path de uma classe (ou conjunto de classes) para posterior importação.
  * "import" não carrega a classe: apenas registra o path em self::autoload, para o "autoload" carregar a classe quando ela for efetivamente instanciada.
  * @param string $path Path no formato x\y\z
  * @param string $className Nome da classe
  * @return string Retorna o path registrado para a classe.
  */
 public static function import($namespace, $classAlias = '')
 {
     mdump('import namespace = ' . $namespace);
     $path = str_replace('\\', DIRECTORY_SEPARATOR, $namespace);
     $tokens = explode(DIRECTORY_SEPARATOR, $path);
     $wildcard = end($tokens) == '*';
     if ($wildcard) {
         $path = substr($path, 0, -2);
         $registerAlias = true;
     }
     // verifica se o primeiro token é a app ou um módulo, e obtem o path completo
     $structure = MApp::getStructure(self::getApp());
     if ($structure->modules[$tokens[0]]) {
         $basePath = MApp::getPathModules() . DIRECTORY_SEPARATOR;
     } else {
         $basePath = Manager::$appsPath . DIRECTORY_SEPARATOR;
     }
     $fullPath = $basePath . $path;
     //mdump($fullPath);
     // seja o wildcard *, seja um unico arquivo, normaliza no array $files
     //$fullPath = realpath($fullPath);
     $files = array();
     //$file = basename($fullPath);
     //$wildcard = ($file == '*');
     if ($wildcard) {
         MUtil::dirToArray($fullPath, $files);
         //    $registerAlias = true;
     } else {
         $fullPath .= '.php';
         if (file_exists($fullPath)) {
             $files[] = $fullPath;
         }
     }
     //mdump($files);
     if (count($files)) {
         $classOriginal = $classAlias;
         foreach ($files as $file) {
             $fileName = basename($file, '.php');
             //$classPath = $wildcard ? $file : $fullPath;
             $className = strtolower($classOriginal ?: $fileName);
             //self::$autoload[$className] = $classPath;
             $fileName = str_replace([$fullPath . DIRECTORY_SEPARATOR, '.php'], '', $file);
             $fileName = str_replace(DIRECTORY_SEPARATOR, '\\', $fileName);
             $nsClass = $wildcard ? str_replace('*', $fileName, $namespace) : $namespace;
             class_alias($nsClass, $className);
             //mdump('*************'.$className . ' - ' . $classPath . ' - ' . $nsClass);
             //mdump('*************'.$className . ' - ' . $nsClass);
         }
     }
     //mdump(self::$autoload);
     //mdump(self::$classAlias);
     mdump('import result = ' . $fullPath);
     return $fullPath;
 }