YiiBase

CDbHttpSession

Yii Framework v1.1.10 类参考

YiiBase

system
继承 class YiiBase
子类 Yii
源自 1.0
版本 $Id: YiiBase.php 3564 2012-02-13 01:29:03Z qiang.xue $
源码 framework/YiiBase.php
YiiBase是一个助手类,它服务于整个框架。

不要直接使用YiiBase。相反,你应该使用它的子类Yii, 你可以在Yii中定制YiiBase的方法。

公共属性

隐藏继承属性

属性类型描述定义在
classMap array 用于Yii的自动加载类地图. YiiBase
enableIncludePath boolean 是否要依靠PHP包含路径到自动加载类文件。默认为true。 如果你的宿主环境不允许你改变PHP包含路径,可以设置为false。 或者你想添加另外的自动加载器到默认的Yii自动加载器。 YiiBase

公共方法

隐藏继承方法

方法描述定义在
app() 返回应用程序单例,如果单例还没创建为null。 YiiBase
autoload() 类自动加载器。 YiiBase
beginProfile() 标记分析一块代码的开始位置。 YiiBase
createApplication() 创建一个指定类的应用程序。 YiiBase
createComponent() 创建一个对象并根据指定的配置初始化。 YiiBase
createConsoleApplication() 创建一个控制台应用程序实例。 YiiBase
createWebApplication() 创建一个Web应用程序实例。 YiiBase
endProfile() 标记分析一块代码的结束位置。 YiiBase
getFrameworkPath() 返回框架的路径 YiiBase
getLogger() 返回日志信息器 YiiBase
getPathOfAlias() 翻译一个别名为一个文件路径。 YiiBase
getVersion() 返回返回Yii framework版本号 YiiBase
import() 导入一个类或一个目录。 YiiBase
log() 写入一条日志信息。 YiiBase
powered() 返回一个可以在网页中显示的Powered-by-Yii字符串信息 YiiBase
registerAutoloader() 注册一个新的类自动加载器。 YiiBase
setApplication() 在类静态成员中存储应用程序实例。 YiiBase
setLogger() 设置日志记录器对象。 YiiBase
setPathOfAlias() 创建一个路径别名。 YiiBase
t() 翻译一条为指定语言的信息。 YiiBase
trace() 写入一条跟踪信息。 YiiBase

属性详细

classMap 属性 (可用自 v1.1.5)
public static array $classMap;

用于Yii的自动加载类地图. 数组中的键是类名,数组中的值是相应类文件的路径。

enableIncludePath 属性 (可用自 v1.1.8)
public static boolean $enableIncludePath;

是否要依靠PHP包含路径到自动加载类文件。默认为true。 如果你的宿主环境不允许你改变PHP包含路径,可以设置为false。 或者你想添加另外的自动加载器到默认的Yii自动加载器。

方法详细

app() 方法
public static CApplication app()
{return} CApplication 应用程序单例。如果单例还没创建为null。
源码: framework/YiiBase.php#134 (显示)
public static function app()
{
    return 
self::$_app;
}

返回应用程序单例,如果单例还没创建为null。

autoload() 方法
public static boolean autoload(string $className)
$className string 类名
{return} boolean 是否该类已经被成功载入
源码: framework/YiiBase.php#393 (显示)
public static function autoload($className)
{
    
// use include so that the error PHP file may appear
    
if(isset(self::$classMap[$className]))
        include(
self::$classMap[$className]);
    else if(isset(
self::$_coreClasses[$className]))
        include(
YII_PATH.self::$_coreClasses[$className]);
    else
    {
        
// include class file relying on include_path
        
if(strpos($className,'\\')===false)  // class without namespace
        
{
            if(
self::$enableIncludePath===false)
            {
                foreach(
self::$_includePaths as $path)
                {
                    
$classFile=$path.DIRECTORY_SEPARATOR.$className.'.php';
                    if(
is_file($classFile))
                    {
                        include(
$classFile);
                        break;
                    }
                }
            }
            else
                include(
$className.'.php');
        }
        else  
// class name with namespace in PHP 5.3
        
{
            
$namespace=str_replace('\\','.',ltrim($className,'\\'));
            if((
$path=self::getPathOfAlias($namespace))!==false)
                include(
$path.'.php');
            else
                return 
false;
        }
        return 
class_exists($className,false) || interface_exists($className,false);
    }
    return 
true;
}

类自动加载器。 这个方法是提供给__autoload()魔术方法使用。

beginProfile() 方法
public static void beginProfile(string $token, string $category='application')
$token string 代码块标记
$category string 日志信息类别
源码: framework/YiiBase.php#497 (显示)
public static function beginProfile($token,$category='application')
{
    
self::log('begin:'.$token,CLogger::LEVEL_PROFILE,$category);
}

标记分析一块代码的开始位置。 标记开始分析一块代码。它必须有一个同样的endProfile()匹配。 begin-和end-调用必须是正确的嵌套,例如,

Yii::beginProfile('block1');
Yii::beginProfile('block2');
Yii::endProfile('block2');
Yii::endProfile('block1');
下面的语句是无效的:
Yii::beginProfile('block1');
Yii::beginProfile('block2');
Yii::endProfile('block1');
Yii::endProfile('block2');

参见

createApplication() 方法
public static mixed createApplication(string $class, mixed $config=NULL)
$class string 应用程序类名
$config mixed 应用程序配置。 这个参数将会传递给应用程序类的构造函数。
{return} mixed 这个应用程序实例
源码: framework/YiiBase.php#125 (显示)
public static function createApplication($class,$config=null)
{
    return new 
$class($config);
}

创建一个指定类的应用程序。

createComponent() 方法
public static mixed createComponent(mixed $config)
$config mixed 配置,它可能是一个字符串或数组。
{return} mixed 创建后的对象
源码: framework/YiiBase.php#182 (显示)
public static function createComponent($config)
{
    if(
is_string($config))
    {
        
$type=$config;
        
$config=array();
    }
    else if(isset(
$config['class']))
    {
        
$type=$config['class'];
        unset(
$config['class']);
    }
    else
        throw new 
CException(Yii::t('yii','Object configuration must be an array containing a "class" element.'));

    if(!
class_exists($type,false))
        
$type=Yii::import($type,true);

    if((
$n=func_num_args())>1)
    {
        
$args=func_get_args();
        if(
$n===2)
            
$object=new $type($args[1]);
        else if(
$n===3)
            
$object=new $type($args[1],$args[2]);
        else if(
$n===4)
            
$object=new $type($args[1],$args[2],$args[3]);
        else
        {
            unset(
$args[0]);
            
$class=new ReflectionClass($type);
            
// Note: ReflectionClass::newInstanceArgs() is available for PHP 5.1.3+
            // $object=$class->newInstanceArgs($args);
            
$object=call_user_func_array(array($class,'newInstance'),$args);
        }
    }
    else
        
$object=new $type;

    foreach(
$config as $key=>$value)
        
$object->$key=$value;

    return 
$object;
}

创建一个对象并根据指定的配置初始化。

指定的配置可以是一个字符串或一个数组。 如果是前者,该字符串被当作(指定一个类名的)对象类型,或 类的路径别名。 如果是后者,‘class’元素对当作对象类型对待, 并且数组中其余的键值对被用作初始化 相应的对象属性。

传递给这个方法的任何额外的参数 将传递给将要创建的对象的构造方法。

createConsoleApplication() 方法
public static CConsoleApplication createConsoleApplication(mixed $config=NULL)
$config mixed 应用程序配置。 如果是一个字符串,它将被当作文件的路径,其中包含配置; 如果是一个数组,它将被当作具体的配置信息。 请务必在配置文件中指定basePath属性, 通常应该指向目录包含所有的应用程序逻辑,模板和数据。 如果没有,那么默认将指定为‘protected’目录。
{return} CConsoleApplication
源码: framework/YiiBase.php#113 (显示)
public static function createConsoleApplication($config=null)
{
    return 
self::createApplication('CConsoleApplication',$config);
}

创建一个控制台应用程序实例。

createWebApplication() 方法
public static CWebApplication createWebApplication(mixed $config=NULL)
$config mixed 应用程序配置。 如果是一个字符串,它将被当作文件的路径,其中含有配置; 如果是一个数组,它将被当作具体的配置信息。 请务必在配置文件中指定basePath属性, 通常应该指向目录包含所有的应用程序逻辑、模板和数据。 如果没有,那么默认将指定为‘protected’目录。
{return} CWebApplication
源码: framework/YiiBase.php#98 (显示)
public static function createWebApplication($config=null)
{
    return 
self::createApplication('CWebApplication',$config);
}

创建一个Web应用程序实例。

endProfile() 方法
public static void endProfile(string $token, string $category='application')
$token string 代码块标记
$category string 日志信息类别
源码: framework/YiiBase.php#509 (显示)
public static function endProfile($token,$category='application')
{
    
self::log('end:'.$token,CLogger::LEVEL_PROFILE,$category);
}

标记分析一块代码的结束位置。 标记分析一块代码结束位置。它必须有一个同样的beginProfile()匹配。

参见

getFrameworkPath() 方法
public static string getFrameworkPath()
{return} string 框架的路径
源码: framework/YiiBase.php#160 (显示)
public static function getFrameworkPath()
{
    return 
YII_PATH;
}

getLogger() 方法
public static CLogger getLogger()
{return} CLogger 日志信息器
源码: framework/YiiBase.php#517 (显示)
public static function getLogger()
{
    if(
self::$_logger!==null)
        return 
self::$_logger;
    else
        return 
self::$_logger=new CLogger;
}

getPathOfAlias() 方法
public static mixed getPathOfAlias(string $alias)
$alias string 别名(例如:system.web.CController)
{return} mixed 别名对应的文件路径,如果名称无效返回false。
源码: framework/YiiBase.php#354 (显示)
public static function getPathOfAlias($alias)
{
    if(isset(
self::$_aliases[$alias]))
        return 
self::$_aliases[$alias];
    else if((
$pos=strpos($alias,'.'))!==false)
    {
        
$rootAlias=substr($alias,0,$pos);
        if(isset(
self::$_aliases[$rootAlias]))
            return 
self::$_aliases[$alias]=rtrim(self::$_aliases[$rootAlias].DIRECTORY_SEPARATOR.str_replace('.',DIRECTORY_SEPARATOR,substr($alias,$pos+1)),'*'.DIRECTORY_SEPARATOR);
        else if(
self::$_app instanceof CWebApplication)
        {
            if(
self::$_app->findModule($rootAlias)!==null)
                return 
self::getPathOfAlias($alias);
        }
    }
    return 
false;
}

翻译一个别名为一个文件路径。 注意,这个方法不能保证返回的文件路径存在。 它仅检测别名的根目录是否有效。

getVersion() 方法
public static string getVersion()
{return} string 返回Yii framework版本号
源码: framework/YiiBase.php#83 (显示)
public static function getVersion()
{
    return 
'1.1.10';
}

import() 方法
public static string import(string $alias, boolean $forceInclude=false)
$alias string 导入的路径别名
$forceInclude boolean 是否立即包含类文件。 如果为flase,则类文件仅在被使用时包含。 这个参数仅当使用一个类的路径别名时才会用到。
{return} string 别名所指的类名或目录
源码: framework/YiiBase.php#265 (显示)
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// a class name in PHP 5.3 namespace format
    
{
        
$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.',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)  // a simple class name
    
{
        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.',array('{alias}'=>$alias)));
                
self::$_imports[$alias]=$className;
            }
            else
                
self::$classMap[$className]=$path.'.php';
            return 
$className;
        }
        else  
// a directory
        
{
            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)));
}

导入一个类或一个目录。

导入一个类就像包含相应的类文件。 主要区别是导入一个类比较轻巧, 它仅在类文件首次引用时包含。

导入一个目录相当于添加一个目录到PHP包含路径中。 如果有多个目录被导入,在类文件搜索时, 后导入的目录优先于之前导入目录(例如,它们被添加到了PHP包含路径的前面)。

路径别名是用来导入一个类或目录。例如,

  • application.components.GoogleMap: import the GoogleMap class.
  • application.components.*: import the components directory.


相同的路径别名可以多次导入,但只有首次有效。 导入一个目录将不会导入其下面的任何子目录。

从1.1.5版本开始,这个方法也可以使用命令空间格式导入一个类 (仅PHP 5.3及以上版本可用)。 它类似使用路径别名格式导入一个类, 除了“.”分隔符被替换成“\”分隔符,例如,导入 application\components\GoogleMap与导入application.components.GoogleMap相似。 不同的是前者使用合法的名称,后者是不合法的。

注意,如果我们使用反斜线代替点号 使用命名空间格式导入一个类要求命名空间相对应于一个有效的路径别名。 例如,命名空间application\components必须对应一个有效的 路径别名application.components

log() 方法
public static void log(string $msg, string $level='info', string $category='application')
$msg string 日志信息
$level string 信息等级(例如‘trace’,‘warning’,‘error’)。它大小写不敏感。
$category string 信息类别(例如‘system.web’)。它大小写不敏感。
源码: framework/YiiBase.php#455 (显示)
public static function log($msg,$level=CLogger::LEVEL_INFO,$category='application')
{
    if(
self::$_logger===null)
        
self::$_logger=new CLogger;
    if(
YII_DEBUG && YII_TRACE_LEVEL>&& $level!==CLogger::LEVEL_PROFILE)
    {
        
$traces=debug_backtrace();
        
$count=0;
        foreach(
$traces as $trace)
        {
            if(isset(
$trace['file'],$trace['line']) && strpos($trace['file'],YII_PATH)!==0)
            {
                
$msg.="\nin ".$trace['file'].' ('.$trace['line'].')';
                if(++
$count>=YII_TRACE_LEVEL)
                    break;
            }
        }
    }
    
self::$_logger->log($msg,$level,$category);
}

写入一条日志信息。 日志信息可以通过这个方法CLogger::getLogs 返回并且可以使用CLogRouter 记录在不同媒体上,例如:file,email,database。

powered() 方法
public static string powered()
{return} string 一个可以在你的网页上显示的Powered-by-Yii信息
源码: framework/YiiBase.php#539 (显示)
public static function powered()
{
    return 
Yii::t('yii','Powered by {yii}.', array('{yii}'=>'<a href="http://www.yiiframework.com/" rel="external">Yii Framework</a>'));
}

返回一个可以在网页中显示的Powered-by-Yii字符串信息

registerAutoloader() 方法
public static void registerAutoloader(callback $callback, boolean $append=false)
$callback callback 一个有效的PHP回调(函数名或array($className,$methodName))).
$append boolean 是否在默认的Yii类自动加载器后附加新的类自动加载器。
源码: framework/YiiBase.php#612 (显示)
public static function registerAutoloader($callback$append=false)
{
    if(
$append)
    {
        
self::$enableIncludePath=false;
        
spl_autoload_register($callback);
    }
    else
    {
        
spl_autoload_unregister(array('YiiBase','autoload'));
        
spl_autoload_register($callback);
        
spl_autoload_register(array('YiiBase','autoload'));
    }
}

注册一个新的类自动加载器。 新的类自动加载器将置于autoload之前或 其他已经存在的加载器之后。

setApplication() 方法
public static void setApplication(CApplication $app)
$app CApplication 应用程序实例。如果为null, 这个已经存在的应用程序实例将被移除。
源码: framework/YiiBase.php#149 (显示)
public static function setApplication($app)
{
    if(
self::$_app===null || $app===null)
        
self::$_app=$app;
    else
        throw new 
CException(Yii::t('yii','Yii application can only be created once.'));
}

在类静态成员中存储应用程序实例。 这个方法帮助实现CApplication的单例模块。 重复调用该方法或CApplication构造器 将抛出一个异常。 使用app()获取应用程序实例。

setLogger() 方法 (可用自 v1.1.8)
public static void setLogger(CLogger $logger)
$logger CLogger 日志记录器对象。
源码: framework/YiiBase.php#530 (显示)
public static function setLogger($logger)
{
    
self::$_logger=$logger;
}

设置日志记录器对象。

setPathOfAlias() 方法
public static void setPathOfAlias(string $alias, string $path)
$alias string 路径别名
$path string 对应的路径别名,如果为null, 则对应的路径别名将被移除。
源码: framework/YiiBase.php#379 (显示)
public static function setPathOfAlias($alias,$path)
{
    if(empty(
$path))
        unset(
self::$_aliases[$alias]);
    else
        
self::$_aliases[$alias]=rtrim($path,'\\/');
}

创建一个路径别名。 注意,该方法既不检测路径是否存在也不检测是否规范。

t() 方法
public static string t(string $category, string $message, array $params=array ( ), string $source=NULL, string $language=NULL)
$category string 信息类别。请仅使用一个单词字母。 注意,类别‘yii’是预留给Yii框架核心代码使用的,参考CPhpMessageSource 获取更多关于信息类别的解释。
$message string 原始信息
$params array 参数被应用到信息中使用的strtr。 第一个参数可以是一个没有键的数字。 并且在这种情况下,这个方法将调用CChoiceFormat::format 选择一个适当的信息(进行)翻译。 自1.1.6开始,你为CChoiceFormat::format 传递复数参数不必封装成数组。
$source string 指定消息源使用的应用组件。 默认为null,意思是消息使用的‘coreMessages’属于‘yii’类别, 而剩余的消息使用‘messages’。
$language string 目标语言。如果为空(默认),将使用application language
{return} string 已翻译的信息
源码: framework/YiiBase.php#567 (显示)
public static function t($category,$message,$params=array(),$source=null,$language=null)
{
    if(
self::$_app!==null)
    {
        if(
$source===null)
            
$source=($category==='yii'||$category==='zii')?'coreMessages':'messages';
        if((
$source=self::$_app->getComponent($source))!==null)
            
$message=$source->translate($category,$message,$language);
    }
    if(
$params===array())
        return 
$message;
    if(!
is_array($params))
        
$params=array($params);
    if(isset(
$params[0])) // number choice
    
{
        if(
strpos($message,'|')!==false)
        {
            if(
strpos($message,'#')===false)
            {
                
$chunks=explode('|',$message);
                
$expressions=self::$_app->getLocale($language)->getPluralRules();
                if(
$n=min(count($chunks),count($expressions)))
                {
                    for(
$i=0;$i<$n;$i++)
                        
$chunks[$i]=$expressions[$i].'#'.$chunks[$i];

                    
$message=implode('|',$chunks);
                }
            }
            
$message=CChoiceFormat::format($message,$params[0]);
        }
        if(!isset(
$params['{n}']))
            
$params['{n}']=$params[0];
        unset(
$params[0]);
    }
    return 
$params!==array() ? strtr($message,$params) : $message;
}

翻译一条为指定语言的信息。 这个方法支持选择格式(参见CChoiceFormat), 例如,根据给定的数字值从几个侯选条件中返回消息。 假设一条信息在一些语言中有不同的复数格式, 此功能主要用来解决复数格式问题。

trace() 方法
public static void trace(string $msg, string $category='application')
$msg string 日志信息
$category string 消息类别
源码: framework/YiiBase.php#440 (显示)
public static function trace($msg,$category='application')
{
    if(
YII_DEBUG)
        
self::log($msg,CLogger::LEVEL_TRACE,$category);
}

写入一条跟踪信息。 这个方法仅在调试模式下写入一条日志。

参见

Copyright © 2008-2011 by Yii Software LLC
All Rights Reserved.