magento 手工 編譯命令模式

http://blog.chinaunix.net/uid-26559452-id-3093121.html


提高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常量來切換編譯模式並設置對應模式的文件包含路徑。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章