/** * 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; }
/** * Matches a route to a request. * * @param Request $request * @return Route */ public function matchRequest(Request $request) { $url = $request->get('p', 'index'); foreach ($this->controllers->getRoutes() as $route) { if ($route->getMethods() && !in_array($request->getMethod(), $route->getMethods())) { continue; } if ($route->matches($url)) { if ($params = $route->getParams()) { $request->add($params); } return $route; } } throw new HttpException(404, sprintf('No route found for "%s"', $url)); }
/** * Gets controller arguments and automatically sets values. * * @param Request $request * @param mixed $controller * @param \ReflectionParameter[] $parameters * @return array */ protected function doGetArguments(Request $request, $controller, array $parameters) { $arguments = array(); foreach ($parameters as $param) { if ($param->getClass() && $param->getClass()->isInstance($this->app)) { $arguments[] = $this->app; } elseif (($value = $request->get($param->name)) !== null) { $arguments[] = $value; } elseif ($param->isDefaultValueAvailable()) { $arguments[] = $param->getDefaultValue(); } else { if (is_array($controller)) { $ctrl = sprintf('%s::%s()', get_class($controller[0]), $controller[1]); } elseif (is_object($controller)) { $ctrl = get_class($controller); } else { $ctrl = $controller; } throw new \RuntimeException(sprintf('Controller "%s" requires that you provide a value for the "$%s" argument.', $ctrl, $param->name)); } } return $arguments; }
/** * 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; }