Blame view

htmlpurifier/tests/Debugger.php 3.88 KB
Maulyanda authored
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
<?php

/**
 * Debugging tools.
 *
 * This file gives a developer a set of tools useful for performing code
 * consistency checks. This includes scoping code blocks to ensure that
 * only the interesting iteration of a loop gets outputted, a paint()
 * function that acts like var_dump() with pre tags, and conditional
 * printing.
 */

/*
TODO
 * Integrate into SimpleTest so it tells us whether or not there were any
   not cleaned up debug calls.
 * Custom var_dump() that ignores blacklisted properties
 * DEPRECATE AND REMOVE ALL CALLS!
*/

/**#@+
 * Convenience global functions. Corresponds to method on Debugger.
 */
function paint($mixed)
{
    $Debugger =& Debugger::instance();
    return $Debugger->paint($mixed);
}
function paintIf($mixed, $conditional)
{
    $Debugger =& Debugger::instance();
    return $Debugger->paintIf($mixed, $conditional);
}
function paintWhen($mixed, $scopes = array())
{
    $Debugger =& Debugger::instance();
    return $Debugger->paintWhen($mixed, $scopes);
}
function paintIfWhen($mixed, $conditional, $scopes = array())
{
    $Debugger =& Debugger::instance();
    return $Debugger->paintIfWhen($mixed, $conditional, $scopes);
}
function addScope($id = false)
{
    $Debugger =& Debugger::instance();
    return $Debugger->addScope($id);
}
function removeScope($id)
{
    $Debugger =& Debugger::instance();
    return $Debugger->removeScope($id);
}
function resetScopes()
{
    $Debugger =& Debugger::instance();
    return $Debugger->resetScopes();
}
function isInScopes($array = array())
{
    $Debugger =& Debugger::instance();
    return $Debugger->isInScopes($array);
}
/**#@-*/


/**
 * The debugging singleton. Most interesting stuff happens here.
 */
class Debugger
{

    public $shouldPaint = false;
    public $paints  = 0;
    public $current_scopes = array();
    public $scope_nextID = 1;
    public $add_pre = true;

    public function Debugger()
    {
        $this->add_pre = !extension_loaded('xdebug');
    }

    public static function &instance() {
        static $soleInstance = false;
        if (!$soleInstance) $soleInstance = new Debugger();
        return $soleInstance;
    }

    public function paintIf($mixed, $conditional)
    {
        if (!$conditional) return;
        $this->paint($mixed);
    }

    public function paintWhen($mixed, $scopes = array())
    {
        if (!$this->isInScopes($scopes)) return;
        $this->paint($mixed);
    }

    public function paintIfWhen($mixed, $conditional, $scopes = array())
    {
        if (!$conditional) return;
        if (!$this->isInScopes($scopes)) return;
        $this->paint($mixed);
    }

    public function paint($mixed)
    {
        $this->paints++;
        if($this->add_pre) echo '<pre>';
        var_dump($mixed);
        if($this->add_pre) echo '</pre>';
    }

    public function addScope($id = false)
    {
        if ($id == false) {
            $id = $this->scope_nextID++;
        }
        $this->current_scopes[$id] = true;
    }

    public function removeScope($id)
    {
        if (isset($this->current_scopes[$id])) unset($this->current_scopes[$id]);
    }

    public function resetScopes()
    {
        $this->current_scopes = array();
        $this->scope_nextID = 1;
    }

    public function isInScopes($scopes = array())
    {
        if (empty($this->current_scopes)) {
            return false;
        }
        if (!is_array($scopes)) {
            $scopes = array($scopes);
        }
        foreach ($scopes as $scope_id) {
            if (empty($this->current_scopes[$scope_id])) {
                return false;
            }
        }
        if (empty($scopes)) {
            if ($this->scope_nextID == 1) {
                return false;
            }
            for($i = 1; $i < $this->scope_nextID; $i++) {
                if (empty($this->current_scopes[$i])) {
                    return false;
                }
            }
        }
        return true;
    }

}

// vim: et sw=4 sts=4