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 string */ public function resolveMethodName(Request $request) { return strtolower($request->getHeader('REQUEST_METHOD')); }