下面的部分是对系统的初始化。之前的部分,可能和普通的程序没什么两样,但是下面这一段,我保证你没见过。
<?php
// Init System
require('../../includes/kernel/config.inc.php');
// First Startup? Init the tmpfs
if (!is_dir ($data_root) || !is_dir ($includes_root)) {
if (!is_writable ($tmpfs_root))
die ('TMPFS FAILED!!!');
require_once('../../includes/kernel/pkg.inc.' . $phpEx);
@mkdir ($data_root);
@mkdir ($includes_root);
$pkg = new BsmPkg ();
$pkg->target_dir = $data_root;
$pkg->filename = $tmpfs_pkg_data_filename;
$pkg->unpack_into_dir ();
$pkg->target_dir = $includes_root;
$pkg->filename = $tmpfs_pkg_includes_filename;
$pkg->unpack_into_dir ();
}
?>
包含一个config.inc.php看起来很正常,它里面有一些关于系统的设置参数(这个文件后面会有),然后它会检查$data_root和$includes_root目录是否存在,并检查$tmpfs_root目录是否可写。这里的$data_root和$includes_root按照规定,是属于$tmpfs_root的下一级目录,而$tmpfs_root是整个系统使用的tmpfs根路径,它负责保存我们系统的临时数据,其中$includes_root用来保存那些需要被执行体包含的include文件,$data_root用来存放Cache、模板编译结果等数据文件。// Init System
require('../../includes/kernel/config.inc.php');
// First Startup? Init the tmpfs
if (!is_dir ($data_root) || !is_dir ($includes_root)) {
if (!is_writable ($tmpfs_root))
die ('TMPFS FAILED!!!');
require_once('../../includes/kernel/pkg.inc.' . $phpEx);
@mkdir ($data_root);
@mkdir ($includes_root);
$pkg = new BsmPkg ();
$pkg->target_dir = $data_root;
$pkg->filename = $tmpfs_pkg_data_filename;
$pkg->unpack_into_dir ();
$pkg->target_dir = $includes_root;
$pkg->filename = $tmpfs_pkg_includes_filename;
$pkg->unpack_into_dir ();
}
?>
tmpfs是Linux里的一种特殊分区格式。区别于ext3等,tmpfs创建于内存和交换区上。Linux有一个默认的shm就是tmpfs类型,通常mount在/dev/shm上。tmpfs和ramfs有些相似,不同的是它会用到交换区。
tmpfs的最大好处是IO速度。毕竟纯粹的物理磁盘操作效率无法和内存相比,而且tmpfs使用起来也很方便,它基本不需要做什么其它设置就可以像普通的物理硬盘一样使用,它对程序来说是透明的。
tmpfs的使用方法与Linux挂载其它类型的分区格式一样,可以用mount命令来挂载,也可以在fstab中设置。
* * * * * *
当系统检测到$tmpfs_root确实存在且可写,而$data_root和$include_root不存在,表示这是系统第一次在运行,它会用内置的一个压缩/解压文件的一个类来把事先准备好的data和includes压缩文件解压到$tmpfs_root中,这个类处理的格式是我自创的,它保持了源目录结构,并保存了文件的属性。它也会对每一个文件做文件长度和MD5校验。这个类位于/includes/kernel/pkg.inc.php
这里提及一个细节,我学习了PHPBB中的$phpEx的概念,整个系统中除了调用common.inc.php和config.inc.php外,其它调用php文件的地方都没有写“.php”扩展名,而是用了一个$phpEx变量代替,这个变量的值在config文件中可以修改,这样做的好处是我们随时可以把系统中的php程序改换扩展名。比如我们修改了Apache配置,让php解释器来解释一种叫做.hello的文件,就可以方便地把整个系统的所有被include的php程序扩展名改成.hello,再把config中的$phpEx的值改成“hello”,这样你的系统看起来就像是使用一种没人见过的Hello语言编写的了,哈哈……
includes这个压缩文件中包含了/includes目录中的所有内容,它被解压到$tmpfs_root(我的系统中是/opt/tmp/)中,这样,在/opt/tmp/includes中就有我们想要的所有include文件了,调用它比直接调用/includes要快很多。
下面的部分就是调用已经解压好的一些include文件
<?php
// Include Kernel file
require($includes_root . 'db/' . $global_db_dbms . '.' . $phpEx);
require($includes_root . 'kernel/constants.inc.' . $phpEx);
require($includes_root . 'kernel/template.inc.' . $phpEx);
require($includes_root . 'kernel/session.inc.' . $phpEx);
require($includes_root . 'kernel/cache.inc.' . $phpEx);
require($includes_root . 'kernel/log.inc.' . $phpEx);
require($includes_root . 'kernel/shm.inc.' . $phpEx);
require($includes_root . 'function/basic.function.' . $phpEx);
require($includes_root . 'function/file.function.' . $phpEx);
?>
接着创建一个通用的数据库连接$db,它的属性也都在config.inc.php中设置。// Include Kernel file
require($includes_root . 'db/' . $global_db_dbms . '.' . $phpEx);
require($includes_root . 'kernel/constants.inc.' . $phpEx);
require($includes_root . 'kernel/template.inc.' . $phpEx);
require($includes_root . 'kernel/session.inc.' . $phpEx);
require($includes_root . 'kernel/cache.inc.' . $phpEx);
require($includes_root . 'kernel/log.inc.' . $phpEx);
require($includes_root . 'kernel/shm.inc.' . $phpEx);
require($includes_root . 'function/basic.function.' . $phpEx);
require($includes_root . 'function/file.function.' . $phpEx);
?>
<?php
// Init the DB Connection
$db = new $sql_db;
// Connect to DB
$db->sql_connect($global_db_host, $global_db_user, $global_db_pass, $global_db_name, $global_db_port, false);
?>
创建成功后,记得把密码清空。// Init the DB Connection
$db = new $sql_db;
// Connect to DB
$db->sql_connect($global_db_host, $global_db_user, $global_db_pass, $global_db_name, $global_db_port, false);
?>
