Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\HttpKernel\Debug; Chris@0: Chris@0: use Symfony\Component\HttpFoundation\Request; Chris@0: use Symfony\Component\HttpFoundation\RequestStack; Chris@0: Chris@0: /** Chris@0: * Formats debug file links. Chris@0: * Chris@0: * @author Jérémy Romey Chris@0: */ Chris@0: class FileLinkFormatter implements \Serializable Chris@0: { Chris@0: private $fileLinkFormat; Chris@0: private $requestStack; Chris@0: private $baseDir; Chris@0: private $urlFormat; Chris@0: Chris@0: public function __construct($fileLinkFormat = null, RequestStack $requestStack = null, $baseDir = null, $urlFormat = null) Chris@0: { Chris@0: $fileLinkFormat = $fileLinkFormat ?: ini_get('xdebug.file_link_format') ?: get_cfg_var('xdebug.file_link_format'); Chris@0: if ($fileLinkFormat && !is_array($fileLinkFormat)) { Chris@0: $i = strpos($f = $fileLinkFormat, '&', max(strrpos($f, '%f'), strrpos($f, '%l'))) ?: strlen($f); Chris@0: $fileLinkFormat = array(substr($f, 0, $i)) + preg_split('/&([^>]++)>/', substr($f, $i), -1, PREG_SPLIT_DELIM_CAPTURE); Chris@0: } Chris@0: Chris@0: $this->fileLinkFormat = $fileLinkFormat; Chris@0: $this->requestStack = $requestStack; Chris@0: $this->baseDir = $baseDir; Chris@0: $this->urlFormat = $urlFormat; Chris@0: } Chris@0: Chris@0: public function format($file, $line) Chris@0: { Chris@0: if ($fmt = $this->getFileLinkFormat()) { Chris@0: for ($i = 1; isset($fmt[$i]); ++$i) { Chris@0: if (0 === strpos($file, $k = $fmt[$i++])) { Chris@0: $file = substr_replace($file, $fmt[$i], 0, strlen($k)); Chris@0: break; Chris@0: } Chris@0: } Chris@0: Chris@0: return strtr($fmt[0], array('%f' => $file, '%l' => $line)); Chris@0: } Chris@0: Chris@0: return false; Chris@0: } Chris@0: Chris@0: public function serialize() Chris@0: { Chris@0: return serialize($this->getFileLinkFormat()); Chris@0: } Chris@0: Chris@0: public function unserialize($serialized) Chris@0: { Chris@0: $this->fileLinkFormat = unserialize($serialized); Chris@0: } Chris@0: Chris@0: private function getFileLinkFormat() Chris@0: { Chris@0: if ($this->fileLinkFormat) { Chris@0: return $this->fileLinkFormat; Chris@0: } Chris@0: if ($this->requestStack && $this->baseDir && $this->urlFormat) { Chris@0: $request = $this->requestStack->getMasterRequest(); Chris@0: if ($request instanceof Request) { Chris@0: return array( Chris@0: $request->getSchemeAndHttpHost().$request->getBaseUrl().$this->urlFormat, Chris@0: $this->baseDir.DIRECTORY_SEPARATOR, '', Chris@0: ); Chris@0: } Chris@0: } Chris@0: } Chris@0: }