03e3cf89549bacf84cb617e8da57cd36ae79c045
[yaffs-website] / vendor / symfony / http-kernel / Debug / FileLinkFormatter.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\Debug;
13
14 use Symfony\Component\HttpFoundation\Request;
15 use Symfony\Component\HttpFoundation\RequestStack;
16
17 /**
18  * Formats debug file links.
19  *
20  * @author Jérémy Romey <jeremy@free-agent.fr>
21  */
22 class FileLinkFormatter implements \Serializable
23 {
24     private $fileLinkFormat;
25     private $requestStack;
26     private $baseDir;
27     private $urlFormat;
28
29     public function __construct($fileLinkFormat = null, RequestStack $requestStack = null, $baseDir = null, $urlFormat = null)
30     {
31         $fileLinkFormat = $fileLinkFormat ?: ini_get('xdebug.file_link_format') ?: get_cfg_var('xdebug.file_link_format');
32         if ($fileLinkFormat && !is_array($fileLinkFormat)) {
33             $i = strpos($f = $fileLinkFormat, '&', max(strrpos($f, '%f'), strrpos($f, '%l'))) ?: strlen($f);
34             $fileLinkFormat = array(substr($f, 0, $i)) + preg_split('/&([^>]++)>/', substr($f, $i), -1, PREG_SPLIT_DELIM_CAPTURE);
35         }
36
37         $this->fileLinkFormat = $fileLinkFormat;
38         $this->requestStack = $requestStack;
39         $this->baseDir = $baseDir;
40         $this->urlFormat = $urlFormat;
41     }
42
43     public function format($file, $line)
44     {
45         if ($fmt = $this->getFileLinkFormat()) {
46             for ($i = 1; isset($fmt[$i]); ++$i) {
47                 if (0 === strpos($file, $k = $fmt[$i++])) {
48                     $file = substr_replace($file, $fmt[$i], 0, strlen($k));
49                     break;
50                 }
51             }
52
53             return strtr($fmt[0], array('%f' => $file, '%l' => $line));
54         }
55
56         return false;
57     }
58
59     public function serialize()
60     {
61         return serialize($this->getFileLinkFormat());
62     }
63
64     public function unserialize($serialized)
65     {
66         $this->fileLinkFormat = unserialize($serialized);
67     }
68
69     private function getFileLinkFormat()
70     {
71         if ($this->fileLinkFormat) {
72             return $this->fileLinkFormat;
73         }
74         if ($this->requestStack && $this->baseDir && $this->urlFormat) {
75             $request = $this->requestStack->getMasterRequest();
76             if ($request instanceof Request) {
77                 return array(
78                     $request->getSchemeAndHttpHost().$request->getBaseUrl().$this->urlFormat,
79                     $this->baseDir.DIRECTORY_SEPARATOR, '',
80                 );
81             }
82         }
83     }
84 }