網站製作學習誌

記錄學習製作網站的一切

ZFDebug - 幫你診斷 Zend Framework 專案

以往在開發 Web 專案時,最麻煩的就是取得整個網站的相關系統資訊了。例如像是整個網站的執行時間、載入了多少程式檔案、記憶體使用量等等,這些資訊在以往的程式裡都要添東加西的,才有辦法取得。

而即便使用了 Zend Framework 後,一般對它的架構不甚瞭解的開發者還是很難從中取得這些資訊。不過這一切在 ZFDebug 出現後,就將會改觀啦!

在使用了 ZFDebug 之後,就會在網頁下方出現一個浮動的資訊列,這樣我們就更很方便地知道這個網站運作時的資訊啦。

主要畫面

接下來我簡單介紹一下怎麼安裝及使用 ZFDebug 。

安裝

首先到 ZFDebug 的官方網站下載最新版本的 ZFDebug ,下載回來後把裡面的 zfdebug/library/ 下的 ZFDebug 資料夾整個放置到 include_path 指定的路徑裡;或著也可以把 ZFDebug 這個資料夾放在 ZF 專案的 library 資料夾裡,總之只要能讓 Zend 的 Loader 能自動載入 ZFDebug_Controller_Plugin_Debug 這個類別就可以了。

設定程式碼

ZFDebug 主要是透過 FrontController Plugin 的機制來完成所有的資訊收集,所以一定要在 FrontController 的 dispatch 動作之前,完成 Plugin 的註冊。

新專案中使用 ZFDebug

如果是用 Zend Framework 1.8 版後提供的 Command Line 指令稿所建置的專案,那麼可以在 application/Bootstrap.php 中加入以下方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
protected function _initZFDebug()
    {
        $autoloader = Zend_Loader_Autoloader::getInstance();
        $autoloader->registerNamespace('ZFDebug');
        $options = array(
            'plugins' => array(
                'Variables',
                'File' => array('base_path' => realpath(APPLICATION_PATH . '/../')),
                'Memory',
                'Time',
                'Registry',
                'Exception',
        ));
        # Instantiate the database adapter and setup the plugin.
        # Alternatively just add the plugin like above and rely on the autodiscovery feature.
        if ($this->hasPluginResource('db')) {
            $this->bootstrap('db');
            $db = $this->getPluginResource('db')->getDbAdapter();
            $options['plugins']['Database']['adapter'] = $db;
        }
        # Setup the cache plugin
        if ($this->hasPluginResource('cache')) {
            $this->bootstrap('cache');
            $cache = $this-getPluginResource('cache')->getDbAdapter();
            $options['plugins']['Cache']['backend'] = $cache->getBackend();
        }
        $debug = new ZFDebug_Controller_Plugin_Debug($options);
        $this->bootstrap('frontController');
        $frontController = $this->getResource('frontController');
        $frontController->registerPlugin($debug);
    }

這樣一來, Bootstrap 會自動幫我們初始化 ZFDebug 。

然後要記得在 template 頁面上加入 <head> 和 <body> 標籤,這樣 ZFDebug 才能自動幫我們產生相關的 CSS 及 HTML 碼。

舊專案中使用 ZFDebug

在舊的 ZF 專案中因為通常沒有 Bootstrap 這個自動載入資源的類別可以修改,但其實還是可以在 FrontController 的 dispatch 動作之前註冊 ZFDebug 這個 Plugin :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
$options = array(
    'plugins' => array(
        'Variables',
        'Html',
        'Database' => array('adapter' => array('standard' => $db)),
        'File' => array('base_path' => WF_ROOT_PATH),
        'Memory',
        'Time',
        'Registry',
        'Cache' => array('backend' => $cache->getBackend()),
        'Exception'
));
$debug = new ZFDebug_Controller_Plugin_Debug($options);
// setup front controller
$frontController = Wacow_Application::getFrontController();
$frontController->registerPlugin($debug);

要特別注意的是, $db 和 $cache 必須在這段程式碼之前就先定義好。

ZFDebug 支援的 Plugins

上面程式碼中,我們有看到 $options['plugins'] 裡有好幾個項目,它們就是 ZFDebug 的幕後大功臣,介紹如下:

Cache

如果有指定 Zend_Cache 物件時,會在這裡顯示相關資訊。 (官方說明)

Cache

Database

我個人很喜歡的一項功能,它可以完整列出整個頁面所執行的 SQL 指令及執行時間。 (官方說明)

Database

File

這個也很棒,它可以完整列出這次程式執行過程中所載入的檔案。 (官方說明)

File

Html

這裡會顯示載入多少 CSS 及 JavaScript 檔案,也可以幫你連到 W3C 做驗證。

Html

Memory

顯示記憶體的峰值使用量,還有每個 Action Controller 用了多少記憶體,另外也會顯示自訂要測量的部份。 (官方說明)

Memory

Registry

顯示 Zend_Registry 裡所註冊的物件內容。 (官方說明)

Registry

Time

這個也是超棒的功能!它會顯示整個網頁執行的時間 (最少時間、最大時間和平均時間) ,也以個別測量我們在程式裡自訂的範圍。 (官方說明)

Time

Variables

列出樣版變數、目前的 Module 、 Controller 及 Action 名稱,還有 $COOKIE 及 $POST 的內容。 (官方說明)

Variables

Exception

這個我還不太會用,主要是顯示異常訊息的內容讓我們可以追蹤。 (官方說明)

心得小結

因為 ZFDebug 主要是收集 Zend Framework 執行時所產生的資訊,或多或少會影響到執行效能。另外在線上使用時,如果沒有用適當的機制來控制 ZFDebug 的話,這些資訊就會顯示在用戶端了。

所以如果真的有需要在線上環境使用它的話,幾個使用建議如下:

  1. 利用一個 Debug Flag 來開關 ZFDebug 。
  2. 只在特定 IP 區段才加入 ZFDebug ,方便開發者線上除錯。

有使用 Zend Framework 開發的朋友們快試試看吧!

Comments