php 编译代码

php 编译代码

编译其实就是把所有的代码整合在于一个文件,减少文件包含时间,加快php解析,虽然优化后时间上提升了不多,但
能优化便多多少少进行优化。下面给一个编译例子,从而引申。

// 定义编译状态
define('COMPILE', true);
// 引入框架入口(项目框架入口)
require_once 'rukou.php';
$result = '';
// 引入想要引入的核心文件
$result .= includeFile(PATH_CORE_DRIVER . "Config/ConfigBase.class.php");
$result .= includeFile(PATH_CORE_DRIVER . 'Config/ConfigPhp.class.php');
$result .= includeFile(PATH_CORE_DRIVER . 'Config/Config.class.php');
$result .= includeFile(PATH_CORE_DRIVER . "Cache/CacheBase.class.php");
$result .= includeFile(PATH_CORE_DRIVER . 'Cache/CacheFile.class.php');
$result .= includeFile(PATH_CORE_DRIVER . 'Cache/Cache.class.php');
$result .= includeFile(PATH_CORE_DRIVER . 'Log/LogBase.class.php');
$result .= includeFile(PATH_CORE_DRIVER . 'Log/LogFile.class.php');
$result .= includeFile(PATH_CORE_DRIVER . 'Log/Log.class.php');
$result .= includeFile(PATH_CORE_DRIVER . "Db/DbBase.class.php");
$result .= includeFile(PATH_CORE_DRIVER . 'Db/DbMysql.class.php');
$result .= includeFile(PATH_CORE_DRIVER . 'Db/Db.class.php');
// 引入核心其它文件
foreach ($arr as $value)
{
    $result .= includeFile(PATH_CORE . "{$value}.class.php");
}
// 定义已编译状态
$fc = strip_whitespace(file_get_contents('Yurun.php'));
$fc = substr($fc, 5);

$fc = "<?php define('IS_COMPILED',true);{$fc}";
// 写出文件
file_put_contents('compile_success.php', str_replace('// {compile}', $result, $fc),LOCK_EX);
header('Content-type: text/html; charset=utf-8');
echo '生成成功!';
/**
 * 将PHP文件读入并去除空格和注释
 *
 * @param type $file            
 * @return type
 */
function includeFile($file)
{
    return substr(strip_whitespace(file_get_contents($file)), 5);
}

/**
 * 去除代码中的空白和注释
 *
 * @param string $content
 *            代码内容
 * @return string
 */
function strip_whitespace($content)
{
    $stripStr = '';
    // 分析php源码
    $tokens = token_get_all($content);
    $last_space = false;
    for ($i = 0, $j = count($tokens); $i < $j; $i ++)
    {
        if (is_string($tokens[$i]))
        {
            $last_space = false;
            $stripStr .= $tokens[$i];
        }
        else
        {
            switch ($tokens[$i][0])
            {
                // 过滤各种PHP注释
                case T_COMMENT :
                case T_DOC_COMMENT :
                    if (stripos($tokens[$i][1], '{compile}') !== false)
                    {
                        $stripStr .= "// {compile}\n";
                    }
                    break;
                // 过滤空格
                case T_WHITESPACE :
                    if (! $last_space)
                    {
                        $stripStr .= ' ';
                        $last_space = true;
                    }
                    break;
                case T_START_HEREDOC :
                    $stripStr .= "<<<\n";
                    break;
                case T_END_HEREDOC :
                    $stripStr .= "\n";
                    for ($k = $i + 1; $k < $j; $k ++)
                    {
                        if (is_string($tokens[$k]) && $tokens[$k] === ';')
                        {
                            $i = $k;
                            break;
                        }
                        else if ($tokens[$k][0] === T_CLOSE_TAG)
                        {
                            break;
                        }
                    }
                    break;
                default :
                    $last_space = false;
                    $stripStr .= $tokens[$i][1];
            }
        }
    }
    return $stripStr;
}

生成之后,代码会糅合在一起。

posted @ 2017-05-02 11:02 hongxinerke 阅读( ...) 评论( ...) 编辑 收藏
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值