/** * Get the URL to a path. * * @param string $path * @param array $parameters * @param mixed $referenceType * @return string */ public function to($path, $parameters = array(), $referenceType = self::ABSOLUTE_PATH) { $basePath = strtr($this->request->getBasePath(), '\\', '/'); $realPath = realpath(dirname(__FILE__) . '/../../../../../../../../../../'); if ($query = substr(strstr($path, '?'), 1)) { parse_str($query, $params); $path = strstr($path, '?', true); $parameters = array_replace($parameters, $params); } if ($query = http_build_query($parameters)) { $query = '?' . $query; } if ($path and !$this->isAbsolutePath($path)) { $path = $this->locator->find($path) ?: $path; } $path = strtr($path, '\\', '/'); if ($basePath && strpos($path, $basePath) === 0) { $path = ltrim(substr($path, strlen($basePath)), '/'); } if ($realPath && strpos($path, $realPath) === 0) { $path = ltrim(substr($path, strlen($realPath)), '/'); } if ($path and preg_match('/^(?!\\/|[a-z]+:\\/\\/)/i', $path)) { $path = $this->base($referenceType) . '/' . $path; } return $path . $query; }
/** * Constructor. * * @param Request $request */ public function __construct(Request $request) { $this->baseUrl = parse_url($request->getBaseUrl(), PHP_URL_PATH); $this->basePath = $request->getBasePath(); }
/** * Constructor. * * @param Request $request * @param LocatorInterface $locator */ public function __construct(Request $request, LocatorInterface $locator) { $this->baseUrl = $request->getBaseUrl(); $this->basePath = strtr($request->getBasePath(), '\\', '/'); $this->locator = $locator; }