public static function import($alias, $forceInclude = false) { if (isset(self::$_imports[$alias])) { // previously imported return self::$_imports[$alias]; } if (class_exists($alias, false) || interface_exists($alias, false)) { return self::$_imports[$alias] = $alias; } if (($pos = strrpos($alias, '\\')) !== false) { $namespace = str_replace('\\', '.', ltrim(substr($alias, 0, $pos), '\\')); if (($path = self::getPathOfAlias($namespace)) !== false) { $classFile = $path . DIRECTORY_SEPARATOR . substr($alias, $pos + 1) . '.php'; if ($forceInclude) { if (is_file($classFile)) { require $classFile; } else { throw new CException(Yii::t('yii', 'Alias "{alias}" is invalid. Make sure it points to an existing PHP file and the file is readable.', array('{alias}' => $alias))); } self::$_imports[$alias] = $alias; } else { self::$classMap[$alias] = $classFile; } return $alias; } else { throw new CException(Yii::t('yii', 'Alias "{alias}" is invalid. Make sure it points to an existing directory.', array('{alias}' => $namespace))); } } if (($pos = strrpos($alias, '.')) === false) { if ($forceInclude && self::autoload($alias)) { self::$_imports[$alias] = $alias; } return $alias; } $className = (string) substr($alias, $pos + 1); $isClass = $className !== '*'; if ($isClass && (class_exists($className, false) || interface_exists($className, false))) { return self::$_imports[$alias] = $className; } if (($path = self::getPathOfAlias($alias)) !== false) { if ($isClass) { if ($forceInclude) { if (is_file($path . '.php')) { require $path . '.php'; } else { throw new CException(Yii::t('yii', 'Alias "{alias}" is invalid. Make sure it points to an existing PHP file and the file is readable.', array('{alias}' => $alias))); } self::$_imports[$alias] = $className; } else { self::$classMap[$className] = $path . '.php'; } return $className; } else { if (self::$_includePaths === null) { self::$_includePaths = array_unique(explode(PATH_SEPARATOR, get_include_path())); if (($pos = array_search('.', self::$_includePaths, true)) !== false) { unset(self::$_includePaths[$pos]); } } array_unshift(self::$_includePaths, $path); if (self::$enableIncludePath && set_include_path('.' . PATH_SEPARATOR . implode(PATH_SEPARATOR, self::$_includePaths)) === false) { self::$enableIncludePath = false; } return self::$_imports[$alias] = $path; } } else { throw new CException(Yii::t('yii', 'Alias "{alias}" is invalid. Make sure it points to an existing directory or file.', array('{alias}' => $alias))); } }
public static function import($alias, $forceInclude = false) { if (isset(self::$_imports[$alias])) { //是否已经存在路径 return self::$_imports[$alias]; } if (class_exists($alias, false) || interface_exists($alias, false)) { //类是否已经定义,针对如urlManager这样的已定义于$_coreClasses[]的类 return self::$_imports[$alias] = $alias; } if (($pos = strrpos($alias, '.')) === false) { // try to autoload the class with an autoloader if $forceInclude is true if ($forceInclude && (Yii::autoload($alias, true) || class_exists($alias, true))) { self::$_imports[$alias] = $alias; } return $alias; } $className = (string) substr($alias, $pos + 1); $isClass = $className !== '*'; //是否为路径+类名 if ($isClass && (class_exists($className, false) || interface_exists($className, false))) { return self::$_imports[$alias] = $className; } //获取真实路径 if (($path = self::getPathOfAlias($alias)) !== false) { //是否以*结尾,如application.utils.* if ($isClass) { if ($forceInclude) { if (is_file($path . '.php')) { require $path . '.php'; } else { throw new CException(Yii::t('yii', 'Alias "{alias}" is invalid. Make sure it points to an existing PHP file and the file is readable.', array('{alias}' => $alias))); } self::$_imports[$alias] = $className; } else { self::$classMap[$className] = $path . '.php'; } return $className; } else { if (self::$_includePaths === null) { self::$_includePaths = array_unique(explode(PATH_SEPARATOR, get_include_path())); if (($pos = array_search('.', self::$_includePaths, true)) !== false) { unset(self::$_includePaths[$pos]); } } array_unshift(self::$_includePaths, $path); if (self::$enableIncludePath && set_include_path('.' . PATH_SEPARATOR . implode(PATH_SEPARATOR, self::$_includePaths)) === false) { self::$enableIncludePath = false; } return self::$_imports[$alias] = $path; } } }
/** * Imports the definition of a class or a directory of class files. * * Path aliases are used to refer to the class file or directory being imported. * If importing a path alias ending with '.*', the alias is considered as a directory * which will be added to the PHP include paths; Otherwise, the alias is translated * to the path of a class file which is included when needed. * * For example, importing 'system.web.*' will add the 'web' directory of the framework * to the PHP include paths; while importing 'system.web.CController' will include * the class file 'web/CController.php' when needed. * * The same alias can be imported multiple times, but only the first time is effective. * * @param string path alias to be imported * @param boolean whether to include the class file immediately. If false, the class file * will be included only when the class is being used. * @return string the class name or the directory that this alias refers to * @throws CException if the alias is invalid */ public static function import($alias, $forceInclude = false) { if (isset(self::$_imports[$alias])) { // previously imported return self::$_imports[$alias]; } if (class_exists($alias, false) || interface_exists($alias, false)) { return self::$_imports[$alias] = $alias; } if (($pos = strrpos($alias, '.')) === false) { if ($forceInclude && self::autoload($alias)) { self::$_imports[$alias] = $alias; } return $alias; } if (($className = (string) substr($alias, $pos + 1)) !== '*' && (class_exists($className, false) || interface_exists($className, false))) { return self::$_imports[$alias] = $className; } if (($path = self::getPathOfAlias($alias)) !== false) { if ($className !== '*') { if ($forceInclude) { require $path . '.php'; self::$_imports[$alias] = $className; } else { self::$_classes[$className] = $path . '.php'; } return $className; } else { if (self::$_includePaths === null) { self::$_includePaths = array_unique(explode(PATH_SEPARATOR, get_include_path())); if (($pos = array_search('.', self::$_includePaths, true)) !== false) { unset(self::$_includePaths[$pos]); } } array_unshift(self::$_includePaths, $path); if (set_include_path('.' . PATH_SEPARATOR . implode(PATH_SEPARATOR, self::$_includePaths)) === false) { throw new CException(Yii::t('yii', 'Unable to import "{alias}". Please check your server configuration to make sure you are allowed to change PHP include_path.', array('{alias}' => $alias))); } return self::$_imports[$alias] = $path; } } else { throw new CException(Yii::t('yii', 'Alias "{alias}" is invalid. Make sure it points to an existing directory or file.', array('{alias}' => $alias))); } }
/** * Imports the definition of a class or a directory of class files. * * Path aliases are used to refer to the class file or directory being imported. * If importing a path alias ending with '.*', the alias is considered as a directory * which will be added to the PHP include paths; Otherwise, the alias is translated * to the path of a class file which is included when needed. * * For example, importing 'system.web.*' will add the 'web' directory of the framework * to the PHP include paths; while importing 'system.web.CController' will include * the class file 'web/CController.php' when needed. * * The same alias can be imported multiple times, but only the first time is effective. * * @param string path alias to be imported * @param boolean whether to include the class file immediately. If false, the class file * will be included only when the class is being used. * @return string the class name or the directory that this alias refers to * @throws CException if the alias is invalid */ public static function import($alias, $forceInclude = false) { if (isset(self::$_imports[$alias])) { // previously imported return self::$_imports[$alias]; } if (class_exists($alias, false) || interface_exists($alias, false)) { return self::$_imports[$alias] = $alias; } if (isset(self::$_coreClasses[$alias]) || ($pos = strrpos($alias, '.')) === false) { self::$_imports[$alias] = $alias; if ($forceInclude) { if (isset(self::$_coreClasses[$alias])) { // a core class require YII_PATH . self::$_coreClasses[$alias]; } else { require $alias . '.php'; } } return $alias; } if (($className = (string) substr($alias, $pos + 1)) !== '*' && (class_exists($className, false) || interface_exists($className, false))) { return self::$_imports[$alias] = $className; } if (($path = self::getPathOfAlias($alias)) !== false) { if ($className !== '*') { self::$_imports[$alias] = $className; if ($forceInclude) { require $path . '.php'; } else { self::$_classes[$className] = $path . '.php'; } return $className; } else { if (self::$_includePaths === null) { self::$_includePaths = array_unique(explode(PATH_SEPARATOR, get_include_path())); if (($pos = array_search('.', self::$_includePaths, true)) !== false) { unset(self::$_includePaths[$pos]); } } array_unshift(self::$_includePaths, $path); set_include_path('.' . PATH_SEPARATOR . implode(PATH_SEPARATOR, self::$_includePaths)); return self::$_imports[$alias] = $path; } } else { throw new CException(Yii::t('yii', 'Alias "{alias}" is invalid. Make sure it points to an existing directory or file.', array('{alias}' => $alias))); } }