4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\HttpKernel\Debug;
14 use Symfony\Component\HttpFoundation\Request;
15 use Symfony\Component\HttpFoundation\RequestStack;
18 * Formats debug file links.
20 * @author Jérémy Romey <jeremy@free-agent.fr>
22 class FileLinkFormatter implements \Serializable
24 private $fileLinkFormat;
25 private $requestStack;
29 public function __construct($fileLinkFormat = null, RequestStack $requestStack = null, $baseDir = null, $urlFormat = null)
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);
37 $this->fileLinkFormat = $fileLinkFormat;
38 $this->requestStack = $requestStack;
39 $this->baseDir = $baseDir;
40 $this->urlFormat = $urlFormat;
43 public function format($file, $line)
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));
53 return strtr($fmt[0], array('%f' => $file, '%l' => $line));
59 public function serialize()
61 return serialize($this->getFileLinkFormat());
64 public function unserialize($serialized)
66 $this->fileLinkFormat = unserialize($serialized);
69 private function getFileLinkFormat()
71 if ($this->fileLinkFormat) {
72 return $this->fileLinkFormat;
74 if ($this->requestStack && $this->baseDir && $this->urlFormat) {
75 $request = $this->requestStack->getMasterRequest();
76 if ($request instanceof Request) {
78 $request->getSchemeAndHttpHost().$request->getBaseUrl().$this->urlFormat,
79 $this->baseDir.DIRECTORY_SEPARATOR, '',