A language is only as good as its developer is.

Doctrine Profiler

Pradėjus dirbti su ZFDebug pirmas dalykas, kurio pasigedau – Doctrine’os profiler’io palaikymo. Tad nusprendžiau jį parašyti. Juolab, kad neseniai kolega irgi buvo jį parašęs, tad iš esmės dviračio išradinėti neteko :) Tad čia gausis kaip praeito straipsnio tąsa.

Plugin’as parašytas Doctrine 1 versijai (kaip veikia 2 versijoje nebandžiau :) ). Kadangi nenorėjau lįsti prie ZFDebug core kodo, t.y. jo viduje įterpinėti plugin’o – tai jį parašiau kaip atskirai integruojamą.

library/Core/Controller/Plugin/Debug/Plugin/ susikuriam failą Doctrine1.php. Failo turinys:

 * @copyright  Copyright (c) 2008-2009 ZF Debug Bar Team (http://code.google.com/p/zfdebug)
 * @license    http://code.google.com/p/zfdebug/wiki/License     New BSD License
 */
class Core_Controller_Plugin_Debug_Plugin_Doctrine1 extends ZFDebug_Controller_Plugin_Debug_Plugin implements ZFDebug_Controller_Plugin_Debug_Plugin_Interface
{

    /**
     * plugin identified name
     *
     * @var string
     */
    protected $_identifier = 'Doctrine1';

    /**
     * @var Doctrine_Connection_Profiler
     */
    protected $_profiler = null;

    /**
     * Create ZFDebug_Controller_Plugin_Debug_Plugin_Dprofiler
     *
     * @param Zend_Db_Adapter_Abstract|array $adapters
     * @author Paulius Petronis

     * @return void
     */
    public function __construct($profiler = null)
    {
        if(!$profiler) {
            $profiler = new Doctrine_Connection_Profiler();
            $conn = Doctrine_Manager::connection();
            $conn->setListener($profiler);
        }
        $this->_profiler = $profiler;
    }

    /**
     * Gets menu tab for the Debugbar
     * @author Paulius Petronis

     * @return string
     */
    public function getTab()
    {
        if (!$this->_profiler)
            return 'No profiler';

        $time = 0;
        foreach ($this->_profiler as $event) {
            $time += $event->getElapsedSecs();
        }
        $html = "Query: ".$this->_profiler->count().' in '.round($time*1000, 2).' ms';

        return $html;
    }

    /**
     * Gets content panel for the Debugbar
     * @author Paulius Petronis

     * @return string
     */
    public function getPanel()
    {
        if (!$this->_profiler) {
            return '';
        }

        $html = '

Database queries

'; $html .= '
    '; foreach ($this->_profiler as $event) { $html .= '
  1. '.$event->getName() . " " . sprintf("%f", $event->getElapsedSecs()) . " "; $html .= $event->getQuery() . " "; $params = $event->getParams(); if(is_array($params) && !empty ($params)) { $html .= '
    Params: '; $html .= '
      '; foreach($params as $key => $value) { $html .= '
    1. '.$key." = ".$value.'
    2. '; } $html .= '
    '; $html .= '
    '; } } $html .= '
'; return $html; } /** * returns a unique identifier for the specific plugin * * @author aur1mas * @return string */ public function getIdentifier() { return $this->_identifier; } }

application.ini pridedam dar vieną eilutę:

resources.Core_Application_Resource_ZFDebug.params.plugins[] =
 "Core_Controller_Plugin_Debug_Plugin_Doctrine1"

Kadangi pavyzdinis kodas nėra labai gerai skaitomas – tai įkeliu nuorodą į pavyzdį.

P.S. parašiau ZFDebug projekto autoriui, kad leistų būti to projekto commiter’iu. Jei viskas bus ok, tai naujame release’e Doctrine plugin’as bus by default ;)