/** * 初始化request * * @param Delegate $delegate */ private function __construct(Delegate $delegate) { $this->delegate = $delegate; $this->request = $delegate->getRequest(); $this->request_type = $this->getRequestType(); $this->request_string = $delegate->getRouter()->getUriRequest('/'); }
/** * request response view * * @param string $property * @return Response|Request|View|Config|null */ function __get($property) { switch ($property) { case 'config': return $this->config = $this->delegate->getConfig(); case 'request': return $this->request = $this->delegate->getRequest(); case 'response': return $this->response = $this->delegate->getResponse(); case 'view': return $this->view = $this->initView(); } return null; }
/** * 运行框架 * * @param object|string $router * @param null $args 指定参数 * @param bool $return_response_content 是否输出执行结果 * @return array|mixed|string * @throws CoreException */ public function dispatcher($router, $args = null, $return_response_content = false) { $init_prams = true; $router = $this->parseRouter($router, $args, $init_prams); $cr = $this->initController($router['controller'], $router['action']); $annotate_config = $this->getAnnotateConfig(); if ($init_prams) { $action_params = array(); if (isset($annotate_config['params'])) { $action_params = $annotate_config['params']; } $this->initParams($router['params'], $action_params); } else { $this->setParams($router['params']); } $this->delegate->getClosureContainer()->run('dispatcher'); $cache = false; if (isset($annotate_config['cache']) && $this->delegate->getRequest()->isGetRequest()) { $cache = $this->initRequestCache($annotate_config['cache']); } if (isset($annotate_config['before'])) { $this->getClassInstanceByName($annotate_config['before']); } if (!empty($annotate_config['basicAuth'])) { $this->delegate->getResponse()->basicAuth($annotate_config['basicAuth']); } if ($cache && $cache->getExpireTime()) { $response_content = $cache->get(); } else { $action = $this->getAction(); $controller_name = $this->getController(); $runtime_config = array('action_annotate' => $annotate_config, 'view_controller_namespace' => $this->getViewControllerNameSpace($controller_name), 'controller' => $controller_name, 'action' => $action, 'params' => $this->getParams()); $this->delegate->getClosureContainer()->add('~controller~runtime~', function () use($runtime_config) { return $runtime_config; }); try { $cr->setStaticPropertyValue('app_delegate', $this->delegate); $controller = $cr->newInstance(); } catch (Exception $e) { throw new CoreException($e->getMessage()); } if ($this->delegate->getResponse()->isEndFlush()) { return true; } ob_start(); $response_content = $controller->{$action}(); if (!$response_content) { $response_content = ob_get_contents(); } ob_end_clean(); if ($cache) { $cache->set(null, $response_content); } } if (!empty($annotate_config['response'])) { $this->setResponseConfig($annotate_config['response']); } if ($return_response_content) { return $response_content; } else { $this->delegate->getResponse()->display($response_content); } if (isset($annotate_config['after'])) { $this->getClassInstanceByName($annotate_config['after']); } return true; }