Chinaunix首页 | 论坛 | 博客
  • 博客访问: 267959
  • 博文数量: 59
  • 博客积分: 1368
  • 博客等级: 中尉
  • 技术积分: 1071
  • 用 户 组: 普通用户
  • 注册时间: 2012-02-02 06:06
文章分类

全部博文(59)

文章存档

2012年(59)

我的朋友

分类: 系统运维

2012-03-04 01:13:48

提高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目录:
  1. $cd shell
  2. $ls
  3. abstract.php compiler.php indexer.php log.php
查看使用compiler.php的方法:
  1. $php -f compiler.php help
  2. Usage: php -f compiler.php -- [options]
  3.   state Show Compilation State
  4.   compile Run Compilation Process
  5.   clear Disable Compiler include path and Remove compiled files
  6.   enable Enable Compiler include path
  7.   disable Disable Compiler include path
  8.   help This help
查看当前的编译状态:
  1. $php -f compiler.php state
  2. Compiler Status: Disabled
  3. Compilation State: Not Compiled
  4. Collected Files Count: 0
  5. Compiled Scopes Count: 0
开始编译:
  1. $php -f compiler.php compile
  2. Compilation successfully finished
  3. $php -f compiler.php state
  4. Compiler Status: Enabled
  5. Compilation State: Compiled
  6. Collected Files Count: 6000
  7. Compiled Scopes Count: 4
关闭和开启编译:
  1. $php -f compiler.php disable
  2. Compiler include path disabled
  3. $php -f compiler.php state
  4. Compiler Status: Disabled
  5. Compilation State: Compiled
  6. Collected Files Count: 6000
  7. Compiled Scopes Count: 4
  8. $php -f compiler.php enable
  9. Compiler include path enabled
  10. $php -f compiler.php state
  11. Compiler Status: Enabled
  12. Compilation State: Compiled
  13. Collected Files Count: 6000
  14. Compiled Scopes Count: 4
清除编译:
  1. $php -f compiler.php clear
  2. Compilation successfully cleared
  3. $php -f compiler.php state
  4. Compiler Status: Disabled
  5. Compilation State: Not Compiled
  6. Collected Files Count: 0
  7. Compiled Scopes Count: 0
我们还可以通过修改/includes/src/config.php文件开启和关闭编译模式,在编译成功后config.php将变为:
  1. define('COMPILER_INCLUDE_PATH', dirname(__FILE__).DIRECTORY_SEPARATOR.'src');
此时只需要注释掉这个语句就可以关闭编译模式:
  1. #define('COMPILER_INCLUDE_PATH', dirname(__FILE__).DIRECTORY_SEPARATOR.'src');
在Mage.php文件中我们可以看到下面一段代码:
  1. Mage::register('original_include_path', get_include_path()); // 保存当前的include_path

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

  12.     $appPath = implode(PS, $paths);
  13.     set_include_path($appPath . PS . Mage::registry('original_include_path')); // 添加以上四个目录为include_path
  14.     include_once "Mage/Core/functions.php";
  15.     include_once "Varien/Autoload.php";
  16. }
我们可以发现,Magento通过检查是否定义COMPILER_INCLUDE_PATH常量来切换编译模式并设置对应模式的文件包含路径。
阅读(4877) | 评论(1) | 转发(2) |
给主人留下些什么吧!~~

usheweb2014-09-25 09:28:53

好文章,我已经转载了 http://www.usheweb.com/a/bowen/20140925/1083.html