b50386cb961c9cdb7acf83b2ac8e15d106ef4d8e
[yaffs-website] / vendor / symfony / http-kernel / DataCollector / DumpDataCollector.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\HttpKernel\DataCollector;
13
14 use Symfony\Component\HttpFoundation\Request;
15 use Symfony\Component\HttpFoundation\RequestStack;
16 use Symfony\Component\HttpFoundation\Response;
17 use Symfony\Component\Stopwatch\Stopwatch;
18 use Symfony\Component\VarDumper\Cloner\Data;
19 use Symfony\Component\VarDumper\Cloner\VarCloner;
20 use Symfony\Component\VarDumper\Dumper\CliDumper;
21 use Symfony\Component\VarDumper\Dumper\HtmlDumper;
22 use Symfony\Component\VarDumper\Dumper\DataDumperInterface;
23 use Twig\Template;
24
25 /**
26  * @author Nicolas Grekas <p@tchwork.com>
27  */
28 class DumpDataCollector extends DataCollector implements DataDumperInterface
29 {
30     private $stopwatch;
31     private $fileLinkFormat;
32     private $dataCount = 0;
33     private $isCollected = true;
34     private $clonesCount = 0;
35     private $clonesIndex = 0;
36     private $rootRefs;
37     private $charset;
38     private $requestStack;
39     private $dumper;
40     private $dumperIsInjected;
41
42     public function __construct(Stopwatch $stopwatch = null, $fileLinkFormat = null, $charset = null, RequestStack $requestStack = null, DataDumperInterface $dumper = null)
43     {
44         $this->stopwatch = $stopwatch;
45         $this->fileLinkFormat = $fileLinkFormat ?: ini_get('xdebug.file_link_format') ?: get_cfg_var('xdebug.file_link_format');
46         $this->charset = $charset ?: ini_get('php.output_encoding') ?: ini_get('default_charset') ?: 'UTF-8';
47         $this->requestStack = $requestStack;
48         $this->dumper = $dumper;
49         $this->dumperIsInjected = null !== $dumper;
50
51         // All clones share these properties by reference:
52         $this->rootRefs = array(
53             &$this->data,
54             &$this->dataCount,
55             &$this->isCollected,
56             &$this->clonesCount,
57         );
58     }
59
60     public function __clone()
61     {
62         $this->clonesIndex = ++$this->clonesCount;
63     }
64
65     public function dump(Data $data)
66     {
67         if ($this->stopwatch) {
68             $this->stopwatch->start('dump');
69         }
70         if ($this->isCollected) {
71             $this->isCollected = false;
72         }
73
74         $trace = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT | DEBUG_BACKTRACE_IGNORE_ARGS, 7);
75
76         $file = $trace[0]['file'];
77         $line = $trace[0]['line'];
78         $name = false;
79         $fileExcerpt = false;
80
81         for ($i = 1; $i < 7; ++$i) {
82             if (isset($trace[$i]['class'], $trace[$i]['function'])
83                 && 'dump' === $trace[$i]['function']
84                 && 'Symfony\Component\VarDumper\VarDumper' === $trace[$i]['class']
85             ) {
86                 $file = $trace[$i]['file'];
87                 $line = $trace[$i]['line'];
88
89                 while (++$i < 7) {
90                     if (isset($trace[$i]['function'], $trace[$i]['file']) && empty($trace[$i]['class']) && 0 !== strpos($trace[$i]['function'], 'call_user_func')) {
91                         $file = $trace[$i]['file'];
92                         $line = $trace[$i]['line'];
93
94                         break;
95                     } elseif (isset($trace[$i]['object']) && $trace[$i]['object'] instanceof Template) {
96                         $template = $trace[$i]['object'];
97                         $name = $template->getTemplateName();
98                         $src = method_exists($template, 'getSourceContext') ? $template->getSourceContext()->getCode() : (method_exists($template, 'getSource') ? $template->getSource() : false);
99                         $info = $template->getDebugInfo();
100                         if (isset($info[$trace[$i - 1]['line']])) {
101                             $line = $info[$trace[$i - 1]['line']];
102                             $file = method_exists($template, 'getSourceContext') ? $template->getSourceContext()->getPath() : null;
103
104                             if ($src) {
105                                 $src = explode("\n", $src);
106                                 $fileExcerpt = array();
107
108                                 for ($i = max($line - 3, 1), $max = min($line + 3, count($src)); $i <= $max; ++$i) {
109                                     $fileExcerpt[] = '<li'.($i === $line ? ' class="selected"' : '').'><code>'.$this->htmlEncode($src[$i - 1]).'</code></li>';
110                                 }
111
112                                 $fileExcerpt = '<ol start="'.max($line - 3, 1).'">'.implode("\n", $fileExcerpt).'</ol>';
113                             }
114                         }
115                         break;
116                     }
117                 }
118                 break;
119             }
120         }
121
122         if (false === $name) {
123             $name = str_replace('\\', '/', $file);
124             $name = substr($name, strrpos($name, '/') + 1);
125         }
126
127         if ($this->dumper) {
128             $this->doDump($data, $name, $file, $line);
129         }
130
131         $this->data[] = compact('data', 'name', 'file', 'line', 'fileExcerpt');
132         ++$this->dataCount;
133
134         if ($this->stopwatch) {
135             $this->stopwatch->stop('dump');
136         }
137     }
138
139     public function collect(Request $request, Response $response, \Exception $exception = null)
140     {
141         // Sub-requests and programmatic calls stay in the collected profile.
142         if ($this->dumper || ($this->requestStack && $this->requestStack->getMasterRequest() !== $request) || $request->isXmlHttpRequest() || $request->headers->has('Origin')) {
143             return;
144         }
145
146         // In all other conditions that remove the web debug toolbar, dumps are written on the output.
147         if (!$this->requestStack
148             || !$response->headers->has('X-Debug-Token')
149             || $response->isRedirection()
150             || ($response->headers->has('Content-Type') && false === strpos($response->headers->get('Content-Type'), 'html'))
151             || 'html' !== $request->getRequestFormat()
152             || false === strripos($response->getContent(), '</body>')
153         ) {
154             if ($response->headers->has('Content-Type') && false !== strpos($response->headers->get('Content-Type'), 'html')) {
155                 $this->dumper = new HtmlDumper('php://output', $this->charset);
156                 $this->dumper->setDisplayOptions(array('fileLinkFormat' => $this->fileLinkFormat));
157             } else {
158                 $this->dumper = new CliDumper('php://output', $this->charset);
159             }
160
161             foreach ($this->data as $dump) {
162                 $this->doDump($dump['data'], $dump['name'], $dump['file'], $dump['line']);
163             }
164         }
165     }
166
167     public function serialize()
168     {
169         if ($this->clonesCount !== $this->clonesIndex) {
170             return 'a:0:{}';
171         }
172
173         $this->data[] = $this->fileLinkFormat;
174         $this->data[] = $this->charset;
175         $ser = serialize($this->data);
176         $this->data = array();
177         $this->dataCount = 0;
178         $this->isCollected = true;
179         if (!$this->dumperIsInjected) {
180             $this->dumper = null;
181         }
182
183         return $ser;
184     }
185
186     public function unserialize($data)
187     {
188         parent::unserialize($data);
189         $charset = array_pop($this->data);
190         $fileLinkFormat = array_pop($this->data);
191         $this->dataCount = count($this->data);
192         self::__construct($this->stopwatch, $fileLinkFormat, $charset);
193     }
194
195     public function getDumpsCount()
196     {
197         return $this->dataCount;
198     }
199
200     public function getDumps($format, $maxDepthLimit = -1, $maxItemsPerDepth = -1)
201     {
202         $data = fopen('php://memory', 'r+b');
203
204         if ('html' === $format) {
205             $dumper = new HtmlDumper($data, $this->charset);
206             $dumper->setDisplayOptions(array('fileLinkFormat' => $this->fileLinkFormat));
207         } else {
208             throw new \InvalidArgumentException(sprintf('Invalid dump format: %s', $format));
209         }
210         $dumps = array();
211
212         foreach ($this->data as $dump) {
213             $dumper->dump($dump['data']->withMaxDepth($maxDepthLimit)->withMaxItemsPerDepth($maxItemsPerDepth));
214             $dump['data'] = stream_get_contents($data, -1, 0);
215             ftruncate($data, 0);
216             rewind($data);
217             $dumps[] = $dump;
218         }
219
220         return $dumps;
221     }
222
223     public function getName()
224     {
225         return 'dump';
226     }
227
228     public function __destruct()
229     {
230         if (0 === $this->clonesCount-- && !$this->isCollected && $this->data) {
231             $this->clonesCount = 0;
232             $this->isCollected = true;
233
234             $h = headers_list();
235             $i = count($h);
236             array_unshift($h, 'Content-Type: '.ini_get('default_mimetype'));
237             while (0 !== stripos($h[$i], 'Content-Type:')) {
238                 --$i;
239             }
240
241             if ('cli' !== PHP_SAPI && stripos($h[$i], 'html')) {
242                 $this->dumper = new HtmlDumper('php://output', $this->charset);
243                 $this->dumper->setDisplayOptions(array('fileLinkFormat' => $this->fileLinkFormat));
244             } else {
245                 $this->dumper = new CliDumper('php://output', $this->charset);
246             }
247
248             foreach ($this->data as $i => $dump) {
249                 $this->data[$i] = null;
250                 $this->doDump($dump['data'], $dump['name'], $dump['file'], $dump['line']);
251             }
252
253             $this->data = array();
254             $this->dataCount = 0;
255         }
256     }
257
258     private function doDump($data, $name, $file, $line)
259     {
260         if ($this->dumper instanceof CliDumper) {
261             $contextDumper = function ($name, $file, $line, $fmt) {
262                 if ($this instanceof HtmlDumper) {
263                     if ($file) {
264                         $s = $this->style('meta', '%s');
265                         $f = strip_tags($this->style('', $file));
266                         $name = strip_tags($this->style('', $name));
267                         if ($fmt && $link = is_string($fmt) ? strtr($fmt, array('%f' => $file, '%l' => $line)) : $fmt->format($file, $line)) {
268                             $name = sprintf('<a href="%s" title="%s">'.$s.'</a>', strip_tags($this->style('', $link)), $f, $name);
269                         } else {
270                             $name = sprintf('<abbr title="%s">'.$s.'</abbr>', $f, $name);
271                         }
272                     } else {
273                         $name = $this->style('meta', $name);
274                     }
275                     $this->line = $name.' on line '.$this->style('meta', $line).':';
276                 } else {
277                     $this->line = $this->style('meta', $name).' on line '.$this->style('meta', $line).':';
278                 }
279                 $this->dumpLine(0);
280             };
281             $contextDumper = $contextDumper->bindTo($this->dumper, $this->dumper);
282             $contextDumper($name, $file, $line, $this->fileLinkFormat);
283         } else {
284             $cloner = new VarCloner();
285             $this->dumper->dump($cloner->cloneVar($name.' on line '.$line.':'));
286         }
287         $this->dumper->dump($data);
288     }
289
290     private function htmlEncode($s)
291     {
292         $html = '';
293
294         $dumper = new HtmlDumper(function ($line) use (&$html) { $html .= $line; }, $this->charset);
295         $dumper->setDumpHeader('');
296         $dumper->setDumpBoundaries('', '');
297
298         $cloner = new VarCloner();
299         $dumper->dump($cloner->cloneVar($s));
300
301         return substr(strip_tags($html), 1, -1);
302     }
303 }