public function __construct(Request $request, PagingResult $pagingResult) { if ($request->getHeader('REQUEST_URI')) { $urlElement = $request->parseUrl(); $host = $request->getHeader('SERVER_NAME'); $scheme = $request->getHeader('HTTPS') === 'on' ? 'https' : 'http'; $query = null; if (isset($urlElement['query'])) { parse_str($urlElement['query'], $temp); if ($temp) { unset($temp['page'], $temp['limit'], $temp['order'], $temp['direction']); if ($temp) { $query = http_build_query($temp); } } } $this->baseUrl = "{$scheme}://{$host}" . $urlElement['path'] . ($query ? '?' . $query : ''); } $this->request = $request; $this->pagingResult = $pagingResult; }
/** * routing(className and methodName) by Hoimi\Request * * this is template method * subclass need to implement "resolveClassName" and "resolveMethodName" * @param Request $request * @return array(instance of Action, methodName) * @throws \RuntimeException * @throws Exception\NotFoundException */ public function run(Request $request) { $url = $request->parseUrl(); if (!isset($url['path']) || !$url['path']) { $url['path'] = '/'; } $className = $this->resolveClassName($request); if ($className === null || !class_exists($className)) { throw new NotFoundException('Unsupported class:' . $className); } $clazz = new \ReflectionClass($className); if (!$clazz->isSubClassOf('Hoimi\\BaseAction')) { throw new \RuntimeException('InvalidClass:' . $className); } $requestMethod = $this->resolveMethodName($request); if (!$clazz->hasMethod($requestMethod)) { throw new NotFoundException('unsupported method :' . $requestMethod . '@' . $url['path']); } $action = $clazz->newInstance(); return array($action, $requestMethod); }
/** * @param Request $request * @return mixed * @throws Exception\NotFoundException */ public function resolveClassName(Request $request) { $path = $this->trimPath($request->parseUrl()['path']); if (isset($this->staticRoutes[$path])) { return $this->staticRoutes[$path]; } foreach ($this->dynamicRoutes as $key => $value) { if (!preg_match_all('/(\\{(.+?)\\})/uS', $key, $names)) { continue; } $key = str_replace('#', '\\#', str_replace($names[1], '(.+?)', $key)); if (!preg_match("#^{$key}\$#u", $path, $values)) { continue; } $result = array(); for ($i = 0, $length = count($names[2]); $i < $length; $i++) { $result[$names[2][$i]] = $values[$i + 1]; } $request->setUrlParameters($result); return $value; } throw new NotFoundException('path not found' . $path); }