build()方法
build方法用来生成项目的编译文件(app.php)或者ALLINONE模式下的allinone.php文件。
1 2 3
| C(include THINK_PATH.'/Common/convention.php'); if(is_file(CONFIG_PATH.'config.php')) C(include CONFIG_PATH.'config.php');
|
首先加载框架的配置文件和项目配置文件。项目配置文件可以为空,为空的话不加载。
1 2 3
| $runtime = defined('RUNTIME_ALLINONE'); $common = ''; $debug = C('APP_DEBUG') && !$runtime;
|
检查是否定义了ALLINONE模式;$common变量用来存储要编译的内容;读取项目是否开启调试模式APP_DEBUG,ALLINONE不支持调试模式,如果开启了ALLINONE的话,对$runtime变量取反就是false,不箮项目是否开启APP_DEBUG,$debug都是false。
1 2 3 4 5 6 7
| if(is_file(COMMON_PATH.'common.php')) { include COMMON_PATH.'common.php'; if(!$debug) $common .= compile(COMMON_PATH.'common.php',$runtime);
} `
|
这一段代码是对项目下公共文件common.php进行编译。首先判断,然后加载,在debug是false的情况下对公共文件的内容进行编译处理后放到$common变量中。
1 2 3 4 5 6 7 8
| if(is_file(CONFIG_PATH.'app.php')) { $list = include CONFIG_PATH.'app.php'; foreach ($list as $file){ require $file; if(!$debug) $common .= compile($file,$runtime); } }
|
对项目下的app.php文件进行导入并编译。由这段代码可以看出app.php文件的内容大致是:
1 2 3 4 5 6
| return array( 'filename1' => 'filepath1', 'filename2' => 'filepath2', 'filename3' => 'filepath3', ..... )
|
我们可以按照这个模式自定义一些项目需要加载的文件。
1 2 3 4 5
| $list = C('APP_CONFIG_LIST'); foreach ($list as $val){ if(is_file(CONFIG_PATH.$val.'.php')) C('_'.$val.'_',array_change_key_case(include CONFIG_PATH.$val.'.php')); }
|
读取扩展配置文件。在框架的Common目录下的convention.php文件里有对APP_CONFIG_LIST的定义和解释:
‘APP_CONFIG_LIST’ => array(‘taglibs’,’routes’,’tags’,’htmls’,’modules’,’actions’),
项目额外需要加载的配置列表,默认包括:taglibs(标签库定义),routes(路由定义),tags(标签定义),(htmls)静态缓存定义, modules(扩展模块),actions(扩展操作)
比如上节我们见到的tag(‘app_init’),就是标签定义。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| if($debug) { C(include THINK_PATH.'/Common/debug.php'); if(is_file(CONFIG_PATH.'debug.php')) C(include CONFIG_PATH.'debug.php'); }else{ if(defined('RUNTIME_ALLINONE')) { $defs = get_defined_constants(TRUE); $content = array_define($defs['user']); $content .= substr(file_get_contents(RUNTIME_PATH.'~runtime.php'),5); $content .= $common."nreturn ".var_export(C(),true).';'; file_put_contents(RUNTIME_PATH.'~allinone.php',strip_whitespace('<!--?php '.$content));<br /--> }else{ $content = "<!--?php ".$common."nreturn ".var_export(C(),true).";n?-->"; file_put_contents(RUNTIME_PATH.'~app.php',strip_whitespace($content)); } } return ;
|
如果调试模式开启的话,就分别加载框架的调试配置文件和项目的调试配置文件。否则就开始项目文件的编译,开启ALLIONE的话,编译allinone.php文件,否则编译app.php文件。