2015年11月7日 星期六

使用instance來進行Tracy Panel

```php // PHP程式碼 include __DIR__.'/vendor/autoload.php'; use Tracy\Debugger; use Tracy\IBarPanel; class CustomPanel implements IBarPanel { /** * Renders HTML code for custom tab. * @return string */ public function getTab() { return 'sql'; } /** * Renders HTML code for custom panel. * @return string */ public function getPanel() { $data = $this->data; ob_start(); require __DIR__.'/custompanel.php'; return ob_get_clean(); } public function push($data) { $this->data[] = $data; } public $data = []; private static $_instance; public static function instance() { if (static::$_instance === null) { static::$_instance = new static; } return static::$_instance; } } // 開發環境 $environment = Debugger::DEVELOPMENT; // 啟用 Debugger::enable($environment); Debugger::getBar() ->addPanel(CustomPanel::instance()); $i = 0; CustomPanel::instance()->push([ 'sql' => 'select * from user where id = '.++$i, ]); CustomPanel::instance()->push([ 'sql' => 'select * from user where id = '.++$i, ]); CustomPanel::instance()->push([ 'sql' => 'select * from user where id = '.++$i, ]); ``` ```php //樣板<h1>SQL</h1>

<div class="tracy-inner">
    <table>
        <?php foreach ($data as $key => $value): ?>
            <tr><td><?php echo $value['sql']; ?></td></tr>
        <?php endforeach ?>
    </table>
</div> ```