/** * @param string $mvcCtrlClassName * @param string $actMethodName */ private function executeAct($mvcCtrlClassName, $actMethodName) { if (!class_exists($mvcCtrlClassName, true)) { // second argument means we use autoload impl to find the class throw new DispatchException('ctrl class: ' . $mvcCtrlClassName . ' is not found', 404); } $mvcCtrlInstance = new $mvcCtrlClassName(); if (!$mvcCtrlInstance instanceof Ctrl) { throw new DispatchException('Ctrl class: ' . $mvcCtrlClassName . ' is not subclass of Ctrl', 404); } // predispatch $mvcCtrlInstance->preDispatch(); $methodVar = array($mvcCtrlInstance, $actMethodName); if (!method_exists($mvcCtrlInstance, $actMethodName) || !is_callable($methodVar, true, $callable_name)) { throw new DispatchException('ctrl: ' . $mvcCtrlClassName . ' has no act called: ' . $actMethodName, 404); } $mvcCtrlInstance->withRequest($this->_request); // init $mvcCtrlInstance->init(); // plugin $mvcCtrlInstance->activatePlugins(); $viewModel = $this->invokeCtrlAct($mvcCtrlInstance, $actMethodName); // http header(s) foreach ($viewModel->concreteHeader() as $header_name => $header_value) { $this->_response->withHeader($header_name, $header_value); } // http body $this->_response->withBody($viewModel->concreteBody()); // postDispatch $mvcCtrlInstance->postDispatch(); }
/** * $path: user/profile.json * * @param $path * @param Request $request * @param Response $response * @return bool|\Closure */ public function apply(&$path, Request &$request, Response &$response) { if (false !== ($postfixPos = strrpos($path, '.'))) { $realPath = substr($path, 0, $postfixPos); $postfix = substr($path, $postfixPos + 1); if ($postfix == $this->_ruleStr) { $path = $realPath; Loader::getInstance()->import("lib/Hydrogen/Http/Response/MIME.php"); // automatically setup Content-type if (null !== ($content_type = getMIMEheader($postfix))) { $response->withHeader(HTTP_HEADER_CONTENT_TYPE, $content_type); } $this->performCallback(); } } return false; }