public function __get($name) { if (empty($name)) { return null; } if (!isset($this->{$name})) { return $this->_di->get($name); } return $this->{$name}; }
/** * @param \swoole_http_request $request * @param \swoole_http_response $response */ public function onRequest($request, $response) { if ($this->doStaticFile($request, $response)) { return; } $this->setGlobal($request); try { $this->di->get('request')->setSwRequest($request); $this->di->get('response')->setSwResponse($response); $this->app->handle($this->di); //清除请求信息 $this->app->clean(); } catch (\Exception $e) { $response->end("<pre>#Msg " . $e->getMessage() . "</pre>"); } $this->unsetGlobal(); }
private function _dispatcher($routeInfo) { $_handler = ['module' => 'Home', 'controller' => 'Index', 'action' => 'index']; $handler = $routeInfo['r_handle']; $type = gettype($handler); switch ($type) { case 'string': if (false != strpos($handler, '@')) { list($_handler['module'], $handler) = explode('@', $handler); } if (false != strpos($handler, '_')) { list($_handler['controller'], $_handler['action']) = explode('_', $handler); } elseif (false != strpos($handler, '/')) { list($_handler['controller'], $_handler['action']) = explode('/', $handler); } break; case 'array': $_handler = array_merge($_handler, $handler); break; case 'object': if ($handler instanceof \Closure) { $this->_response->end(call_user_func($handler)); return; } } empty($_handler['namespace']) || Router::setModule(ucfirst($_handler['namespace'])); empty($_handler['module']) || Router::setModule(ucfirst($_handler['module'])); empty($_handler['controller']) || Router::setController(ucfirst($_handler['controller'])); empty($_handler['action']) || Router::setAction($_handler['action']); $controller = Router::getNamespace() . '\\' . Router::getModule() . '\\' . Router::getControllerSuffix() . '\\' . Router::getController() . Router::getControllerSuffix(); $action = Router::getAction() . Router::getActionSuffix(); /** * @var Controller $obj ; */ $obj = new $controller($routeInfo['request'], $this->_response); $rel = new \ReflectionClass($obj->setDI($this->_di)); if ($rel->hasMethod($action)) { $method = $rel->getMethod($action); if ($method->isPublic() && !$method->isStatic()) { if ($scheduler = $this->_di->get('scheduler')) { if ($method->getNumberOfParameters() > 0) { $result = $method->invokeArgs($obj, $routeInfo['r_var']); } else { $result = $method->invoke($obj); } $scheduler->newTask($result)->run(); return; } } } $this->_response->getSwResponse()->status(404); $this->_response->getSwResponse()->end('<h1>404</h1>'); }