yii的入口文件
yii的入口文件
這里使用了一個(gè)第三方的配置管理插件:marcovwout,來管理Yii的配置,細(xì)節(jié)我就不說了。剩下的就是就是一些基本的全局變量設(shè)置了。往Yii::createWebApplication里面?zhèn)魅肱渲玫臄?shù)組仁连,然后調(diào)用run方法,一個(gè)web應(yīng)用是不是就這么跑起來了,是的饭冬,抽象到最高層就是這樣:我往一個(gè)容器里面?zhèn)魅雽?yīng)的配置使鹅,然后這個(gè)應(yīng)用可以基于該配置正常運(yùn)行起來。
說YiiBase中的兩個(gè)比較重要的方法 (import昌抠,autoload)
YiiBase中的幾個(gè)靜態(tài)變量
然后看看YiiBase中的import方法就知道這些靜態(tài)變量是干嘛用的了:
/* Yii::import()
* $alias: 要導(dǎo)入的類名或路徑
* $forceInclude false:只導(dǎo)入不include類文件患朱,true則導(dǎo)入并include類文件
*/
public static function import($alias, $forceInclude = false){
//Yii把所有的依賴放入到這個(gè)全局的$_imports數(shù)組中,名字不能重復(fù)
//如果當(dāng)前依賴已經(jīng)被引入過了,那么直接返回
if (isset(self::$_imports[$alias])) {
return self::$_imports[$alias];
}
//class_exists和interface_exists方法的第二個(gè)參數(shù)的值為false表示不autoload
if (class_exists($alias, false) || interface_exists($alias, false)) {
return self::$_imports[$alias] = $alias;
}
//如果傳進(jìn)來的是一個(gè)php5.3版本的命名空間格式的類(例如:\a\b\c.php)
if (($pos = strrpos($alias, '\\')) !== false) {
//$namespace = a.b
$namespace = str_replace('\\', '.', ltrim(substr($alias, 0, $pos), '\\'));
//判斷a.b這個(gè)路徑是否存在炊苫,或者a.b只是alias里面的一個(gè)鍵裁厅,調(diào)用該方法返回這個(gè)鍵對應(yīng)的值,比如'email' => realpath(__DIR__ . '/../vendor/cornernote/yii-email-module/email')
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 {
// try to autoload the class with an autoloader
if (class_exists($alias, true)) {
return self::$_imports[$alias] = $alias;
} else {
throw new CException(Yii::t('yii', 'Alias "{alias}" is invalid. Make sure it points to an existing directory or file.', array('{alias}' => $namespace)));
}
}
}
if (($pos = strrpos($alias, '.')) === false) // a simple class name
{
// 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) {
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;
}
// $alias是’system.web.*’這樣的已*結(jié)尾的路徑侨艾,將路徑加到include_path中
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)));
}
}
是的执虹,上面這個(gè)方法最后就把要加載的東西都放到$_imports,$_includePaths中去了。這就是Yii的import方法唠梨,好的袋励,接下來我們看看autoload方法:
public static function autoload($className, $classMapOnly = false){ // use include so that the error PHP file may appear
if (isset(self::$classMap[$className])) {
include(self::$classMap[$className]);
} elseif (isset(self::$_coreClasses[$className])) {
include(YII_PATH . self::$_coreClasses[$className]);
} elseif ($classMapOnly) {
return false;
} 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);
if (YII_DEBUG && basename(realpath($classFile)) !== $className . '.php') {
throw new CException(Yii::t('yii', 'Class name "{class}" does not match class file "{file}".', array( '{class}' => $className, '{file}' => $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;}
config文件中的 import 項(xiàng)里的類或路徑在腳本啟動(dòng)中會(huì)被自動(dòng)導(dǎo)入。用戶應(yīng)用里個(gè)別類需要引入的類可以在類定義前加入 Yii::import() 語句姻成。