Magento编译模式 - Magento Compiler Mode

提高Magento性能的一个很重要的方法是开启Magento的编译模式,可以在后台System>Tools>Compilation,点击Run Compilation Process按钮,一段时间后,我们发现Compiler Status由Disabled变为Enabled,已经编译成功了,打开/includes/src目录,会发现生成了很多文件。

因为在Magento中,我们模块的代码可以放在/app/code/local,/app/code/community,以及/app/code/core这三个目录下,如果Magento需要包含某个文件,系统会依次搜索这三个目录,直到找到这个文件为止,这为我们重写系统的某些文件提供了方便,比如需要修改core目录下的Mage/Catalog/Model/Product.php文件,只需要复制这个文件到local目录下的Mage/Catalog/Model/Product.php,然后修改这个文件即可,这样无需修改核心文件,为我们以后升级系统提供了方便,但由于系统需要在不同的目录中搜索文件,所以效率比较低,使用Compiler功能,系统将把这三个目录下的文件按照优先级顺序复制到/includes/src目录,比如Mage/Catalog/Model/Product.php文件将复制为Mage_Catalog_Model_Product.php,这样系统能够很快的找到需要包含的文件,大幅的提高了效率。

如果我们需要安装新的模块,或者修改已有的模块,我们需要关闭Magento的编译模式,可以在后台System>Tools>Compilation,点击Disable按钮,Compiler Status将由Enabled变为Disabled,添加或修改好模块之后,需要点击Run Compilation Process按钮重新生成编译文件。

此外,Magento提供了一个脚本工具使我们无需进入后台就可以查看和切换编译模式,该脚本位于根目录下的/shell/compiler.php

打开命令行,切换至shell目录:
$cd shell
$ls
abstract.php compiler.php indexer.php log.php

查看使用compiler.php的方法:
$php -f compiler.php help
Usage: php -f compiler.php -- [options]
  state Show Compilation State
  compile Run Compilation Process
  clear Disable Compiler include path and Remove compiled files
  enable Enable Compiler include path
  disable Disable Compiler include path
  help This help

查看当前的编译状态:
$php -f compiler.php state
Compiler Status: Disabled
Compilation State: Not Compiled
Collected Files Count: 0
Compiled Scopes Count: 0

开始编译:
$php -f compiler.php compile
Compilation successfully finished
$php -f compiler.php state
Compiler Status: Enabled
Compilation State: Compiled
Collected Files Count: 6000
Compiled Scopes Count: 4

关闭和开启编译:
$php -f compiler.php disable
Compiler include path disabled
$php -f compiler.php state
Compiler Status: Disabled
Compilation State: Compiled
Collected Files Count: 6000
Compiled Scopes Count: 4
$php -f compiler.php enable
Compiler include path enabled
$php -f compiler.php state
Compiler Status: Enabled
Compilation State: Compiled
Collected Files Count: 6000
Compiled Scopes Count: 4

清除编译:
$php -f compiler.php clear
Compilation successfully cleared
$php -f compiler.php state
Compiler Status: Disabled
Compilation State: Not Compiled
Collected Files Count: 0
Compiled Scopes Count: 0

我们还可以通过修改/includes/src/config.php文件开启和关闭编译模式,在编译成功后config.php将变为:
define('COMPILER_INCLUDE_PATH', dirname(__FILE__).DIRECTORY_SEPARATOR.'src');

此时只需要注释掉这个语句就可以关闭编译模式:
#define('COMPILER_INCLUDE_PATH', dirname(__FILE__).DIRECTORY_SEPARATOR.'src');

在Mage.php文件中我们可以看到下面一段代码:
Mage::register('original_include_path', get_include_path()); // 保存当前的include_path

if (defined('COMPILER_INCLUDE_PATH')) { // 如果设置为编译模式
    $appPath = COMPILER_INCLUDE_PATH;
    set_include_path($appPath . PS . Mage::registry('original_include_path')); // 添加includes/src为include_path
    include_once "Mage_Core_functions.php";
    include_once "Varien_Autoload.php";
} else { // 没有设置为编译模式
    $paths[] = BP . DS . 'app' . DS . 'code' . DS . 'local';
    $paths[] = BP . DS . 'app' . DS . 'code' . DS . 'community';
    $paths[] = BP . DS . 'app' . DS . 'code' . DS . 'core';
    $paths[] = BP . DS . 'lib';

    $appPath = implode(PS, $paths);
    set_include_path($appPath . PS . Mage::registry('original_include_path')); // 添加以上四个目录为include_path
    include_once "Mage/Core/functions.php";
    include_once "Varien/Autoload.php";
}

我们可以发现,Magento通过检查是否定义COMPILER_INCLUDE_PATH常量来切换编译模式并设置对应模式的文件包含路径
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值