Namespaces

  • Latte
    • Loaders
    • Macros
    • Runtime
  • Nette
    • Application
      • Responses
      • Routers
      • UI
    • Bridges
      • ApplicationDI
      • ApplicationLatte
      • ApplicationTracy
      • CacheDI
      • CacheLatte
      • DatabaseDI
      • DatabaseTracy
      • DITracy
      • FormsDI
      • FormsLatte
      • Framework
      • HttpDI
      • HttpTracy
      • MailDI
      • ReflectionDI
      • SecurityDI
      • SecurityTracy
    • Caching
      • Storages
    • ComponentModel
    • Database
      • Conventions
      • Drivers
      • Table
    • DI
      • Config
        • Adapters
      • Extensions
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Loaders
    • Localization
    • Mail
    • Neon
    • PhpGenerator
    • Reflection
    • Security
    • Tokenizer
    • Utils
  • Tracy
    • Bridges
      • Nette
  • none

Classes

  • Bar
  • BlueScreen
  • Debugger
  • Dumper
  • FireLogger
  • Helpers
  • Logger
  • OutputDebugger

Interfaces

  • IBarPanel
  • ILogger
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Tracy (https://tracy.nette.org)
  5:  * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
  6:  */
  7: 
  8: namespace Tracy;
  9: 
 10: 
 11: /**
 12:  * Debug Bar.
 13:  */
 14: class Bar
 15: {
 16:     /** @var IBarPanel[] */
 17:     private $panels = [];
 18: 
 19:     /** @var bool  initialized by dispatchAssets() */
 20:     private $useSession = false;
 21: 
 22:     /** @var string|NULL  generated by renderLoader() */
 23:     private $contentId;
 24: 
 25: 
 26:     /**
 27:      * Add custom panel.
 28:      * @param  IBarPanel
 29:      * @param  string
 30:      * @return static
 31:      */
 32:     public function addPanel(IBarPanel $panel, $id = null)
 33:     {
 34:         if ($id === null) {
 35:             $c = 0;
 36:             do {
 37:                 $id = get_class($panel) . ($c++ ? "-$c" : '');
 38:             } while (isset($this->panels[$id]));
 39:         }
 40:         $this->panels[$id] = $panel;
 41:         return $this;
 42:     }
 43: 
 44: 
 45:     /**
 46:      * Returns panel with given id
 47:      * @param  string
 48:      * @return IBarPanel|null
 49:      */
 50:     public function getPanel($id)
 51:     {
 52:         return isset($this->panels[$id]) ? $this->panels[$id] : null;
 53:     }
 54: 
 55: 
 56:     /**
 57:      * Renders loading <script>
 58:      * @return void
 59:      */
 60:     public function renderLoader()
 61:     {
 62:         if (!$this->useSession) {
 63:             throw new \LogicException('Start session before Tracy is enabled.');
 64:         }
 65:         $contentId = $this->contentId = $this->contentId ?: substr(md5(uniqid('', true)), 0, 10);
 66:         $nonce = Helpers::getNonce();
 67:         $async = true;
 68:         require __DIR__ . '/assets/Bar/loader.phtml';
 69:     }
 70: 
 71: 
 72:     /**
 73:      * Renders debug bar.
 74:      * @return void
 75:      */
 76:     public function render()
 77:     {
 78:         $useSession = $this->useSession && session_status() === PHP_SESSION_ACTIVE;
 79:         $redirectQueue = &$_SESSION['_tracy']['redirect'];
 80: 
 81:         foreach (['bar', 'redirect', 'bluescreen'] as $key) {
 82:             $queue = &$_SESSION['_tracy'][$key];
 83:             $queue = array_slice((array) $queue, -10, null, true);
 84:             $queue = array_filter($queue, function ($item) {
 85:                 return isset($item['time']) && $item['time'] > time() - 60;
 86:             });
 87:         }
 88: 
 89:         if (Helpers::isAjax()) {
 90:             if ($useSession) {
 91:                 $rows[] = (object) ['type' => 'ajax', 'panels' => $this->renderPanels('-ajax')];
 92:                 $contentId = $_SERVER['HTTP_X_TRACY_AJAX'] . '-ajax';
 93:                 $_SESSION['_tracy']['bar'][$contentId] = ['content' => self::renderHtmlRows($rows), 'dumps' => Dumper::fetchLiveData(), 'time' => time()];
 94:             }
 95: 
 96:         } elseif (preg_match('#^Location:#im', implode("\n", headers_list()))) { // redirect
 97:             if ($useSession) {
 98:                 Dumper::fetchLiveData();
 99:                 Dumper::$livePrefix = count($redirectQueue) . 'p';
100:                 $redirectQueue[] = [
101:                     'panels' => $this->renderPanels('-r' . count($redirectQueue)),
102:                     'dumps' => Dumper::fetchLiveData(),
103:                     'time' => time(),
104:                 ];
105:             }
106: 
107:         } elseif (Helpers::isHtmlMode()) {
108:             $rows[] = (object) ['type' => 'main', 'panels' => $this->renderPanels()];
109:             $dumps = Dumper::fetchLiveData();
110:             foreach (array_reverse((array) $redirectQueue) as $info) {
111:                 $rows[] = (object) ['type' => 'redirect', 'panels' => $info['panels']];
112:                 $dumps += $info['dumps'];
113:             }
114:             $redirectQueue = null;
115:             $content = self::renderHtmlRows($rows);
116: 
117:             if ($this->contentId) {
118:                 $_SESSION['_tracy']['bar'][$this->contentId] = ['content' => $content, 'dumps' => $dumps, 'time' => time()];
119:             } else {
120:                 $contentId = substr(md5(uniqid('', true)), 0, 10);
121:                 $nonce = Helpers::getNonce();
122:                 $async = false;
123:                 require __DIR__ . '/assets/Bar/loader.phtml';
124:             }
125:         }
126:     }
127: 
128: 
129:     /**
130:      * @return string
131:      */
132:     private function renderHtmlRows(array $rows)
133:     {
134:         ob_start(function () {});
135:         require __DIR__ . '/assets/Bar/panels.phtml';
136:         require __DIR__ . '/assets/Bar/bar.phtml';
137:         return Helpers::fixEncoding(ob_get_clean());
138:     }
139: 
140: 
141:     /**
142:      * @return array
143:      */
144:     private function renderPanels($suffix = null)
145:     {
146:         set_error_handler(function ($severity, $message, $file, $line) {
147:             if (error_reporting() & $severity) {
148:                 throw new \ErrorException($message, 0, $severity, $file, $line);
149:             }
150:         });
151: 
152:         $obLevel = ob_get_level();
153:         $panels = [];
154: 
155:         foreach ($this->panels as $id => $panel) {
156:             $idHtml = preg_replace('#[^a-z0-9]+#i', '-', $id) . $suffix;
157:             try {
158:                 $tab = (string) $panel->getTab();
159:                 $panelHtml = $tab ? (string) $panel->getPanel() : null;
160:                 if ($tab && $panel instanceof \Nette\Diagnostics\IBarPanel) {
161:                     $e = new \Exception('Support for Nette\Diagnostics\IBarPanel is deprecated');
162:                 }
163: 
164:             } catch (\Exception $e) {
165:             } catch (\Throwable $e) {
166:             }
167:             if (isset($e)) {
168:                 while (ob_get_level() > $obLevel) { // restore ob-level if broken
169:                     ob_end_clean();
170:                 }
171:                 $idHtml = "error-$idHtml";
172:                 $tab = "Error in $id";
173:                 $panelHtml = "<h1>Error: $id</h1><div class='tracy-inner'>" . nl2br(Helpers::escapeHtml($e)) . '</div>';
174:                 unset($e);
175:             }
176:             $panels[] = (object) ['id' => $idHtml, 'tab' => $tab, 'panel' => $panelHtml];
177:         }
178: 
179:         restore_error_handler();
180:         return $panels;
181:     }
182: 
183: 
184:     /**
185:      * Renders debug bar assets.
186:      * @return bool
187:      */
188:     public function dispatchAssets()
189:     {
190:         $asset = isset($_GET['_tracy_bar']) ? $_GET['_tracy_bar'] : null;
191:         if ($asset === 'js') {
192:             header('Content-Type: text/javascript');
193:             header('Cache-Control: max-age=864000');
194:             header_remove('Pragma');
195:             header_remove('Set-Cookie');
196:             $this->renderAssets();
197:             return true;
198:         }
199: 
200:         $this->useSession = session_status() === PHP_SESSION_ACTIVE;
201: 
202:         if ($this->useSession && Helpers::isAjax()) {
203:             header('X-Tracy-Ajax: 1'); // session must be already locked
204:         }
205: 
206:         if ($this->useSession && $asset && preg_match('#^content(-ajax)?\.(\w+)$#', $asset, $m)) {
207:             $session = &$_SESSION['_tracy']['bar'][$m[2] . $m[1]];
208:             header('Content-Type: text/javascript');
209:             header('Cache-Control: max-age=60');
210:             header_remove('Set-Cookie');
211:             if (!$m[1]) {
212:                 $this->renderAssets();
213:             }
214:             if ($session) {
215:                 $method = $m[1] ? 'loadAjax' : 'init';
216:                 echo "Tracy.Debug.$method(", json_encode($session['content']), ', ', json_encode($session['dumps']), ');';
217:                 $session = null;
218:             }
219:             $session = &$_SESSION['_tracy']['bluescreen'][$m[2]];
220:             if ($session) {
221:                 echo 'Tracy.BlueScreen.loadAjax(', json_encode($session['content']), ', ', json_encode($session['dumps']), ');';
222:                 $session = null;
223:             }
224:             return true;
225:         }
226:     }
227: 
228: 
229:     private function renderAssets()
230:     {
231:         $css = array_map('file_get_contents', array_merge([
232:             __DIR__ . '/assets/Bar/bar.css',
233:             __DIR__ . '/assets/Toggle/toggle.css',
234:             __DIR__ . '/assets/Dumper/dumper.css',
235:             __DIR__ . '/assets/BlueScreen/bluescreen.css',
236:         ], Debugger::$customCssFiles));
237:         $css = json_encode(preg_replace('#\s+#u', ' ', implode($css)));
238:         echo "(function(){var el = document.createElement('style'); el.className='tracy-debug'; el.textContent=$css; document.head.appendChild(el);})();\n";
239: 
240:         array_map('readfile', array_merge([
241:             __DIR__ . '/assets/Bar/bar.js',
242:             __DIR__ . '/assets/Toggle/toggle.js',
243:             __DIR__ . '/assets/Dumper/dumper.js',
244:             __DIR__ . '/assets/BlueScreen/bluescreen.js',
245:         ], Debugger::$customJsFiles));
246:     }
247: }
248: 
Nette 2.4-20180206 API API documentation generated by ApiGen 2.8.0