b3c2595b0d072a8040218a2d23f3e0d3f5ed357c
[yaffs-website] / 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 use Symfony\Component\Routing\Exception\ExceptionInterface;
17 use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
18
19 /**
20  * Formats debug file links.
21  *
22  * @author Jérémy Romey <jeremy@free-agent.fr>
23  */
24 class FileLinkFormatter implements \Serializable
25 {
26     private $fileLinkFormat;
27     private $requestStack;
28     private $baseDir;
29     private $urlFormat;
30
31     /**
32      * @param string|\Closure $urlFormat the URL format, or a closure that returns it on-demand
33      */
34     public function __construct($fileLinkFormat = null, RequestStack $requestStack = null, $baseDir = null, $urlFormat = null)
35     {
36         $fileLinkFormat = $fileLinkFormat ?: ini_get('xdebug.file_link_format') ?: get_cfg_var('xdebug.file_link_format');
37         if ($fileLinkFormat && !\is_array($fileLinkFormat)) {
38             $i = strpos($f = $fileLinkFormat, '&', max(strrpos($f, '%f'), strrpos($f, '%l'))) ?: \strlen($f);
39             $fileLinkFormat = array(substr($f, 0, $i)) + preg_split('/&([^>]++)>/', substr($f, $i), -1, PREG_SPLIT_DELIM_CAPTURE);
40         }
41
42         $this->fileLinkFormat = $fileLinkFormat;
43         $this->requestStack = $requestStack;
44         $this->baseDir = $baseDir;
45         $this->urlFormat = $urlFormat;
46     }
47
48     public function format($file, $line)
49     {
50         if ($fmt = $this->getFileLinkFormat()) {
51             for ($i = 1; isset($fmt[$i]); ++$i) {
52                 if (0 === strpos($file, $k = $fmt[$i++])) {
53                     $file = substr_replace($file, $fmt[$i], 0, \strlen($k));
54                     break;
55                 }
56             }
57
58             return strtr($fmt[0], array('%f' => $file, '%l' => $line));
59         }
60
61         return false;
62     }
63
64     public function serialize()
65     {
66         return serialize($this->getFileLinkFormat());
67     }
68
69     public function unserialize($serialized)
70     {
71         if (\PHP_VERSION_ID >= 70000) {
72             $this->fileLinkFormat = unserialize($serialized, array('allowed_classes' => false));
73         } else {
74             $this->fileLinkFormat = unserialize($serialized);
75         }
76     }
77
78     /**
79      * @internal
80      */
81     public static function generateUrlFormat(UrlGeneratorInterface $router, $routeName, $queryString)
82     {
83         try {
84             return $router->generate($routeName).$queryString;
85         } catch (ExceptionInterface $e) {
86             return null;
87         }
88     }
89
90     private function getFileLinkFormat()
91     {
92         if ($this->fileLinkFormat) {
93             return $this->fileLinkFormat;
94         }
95         if ($this->requestStack && $this->baseDir && $this->urlFormat) {
96             $request = $this->requestStack->getMasterRequest();
97             if ($request instanceof Request) {
98                 if ($this->urlFormat instanceof \Closure && !$this->urlFormat = \call_user_func($this->urlFormat)) {
99                     return;
100                 }
101
102                 return array(
103                     $request->getSchemeAndHttpHost().$request->getBasePath().$this->urlFormat,
104                     $this->baseDir.\DIRECTORY_SEPARATOR, '',
105                 );
106             }
107         }
108     }
109 }