/** * @description call anonymous function */ public function call() { if ($this->response->getStatusCode() == 202) { $this->response->setStatusCode(200); $this->response->setHeaders(['Content-Type' => 'text/html']); } $params = $this->route->getParameters() == '' ? [] : $this->route->getParameters(); if (is_array($content = call_user_func_array($this->route->getTarget('closure'), $params))) { $this->route->addTarget('data', $content); } elseif (!is_null($content)) { $this->response->setContent($content); } }
/** * @description set response code */ public function callResponse() { if (isset($this->route->getDetail()['response_templates']) && isset($this->route->getDetail()['response_templates'][$code = $this->response->getStatusCode()])) { $this->route->setCallback($this->route->getDetail()['response_templates'][$code]); $matcher = null; foreach ($this->matcher as $instance) { if ($instance instanceof ArrayMatcher) { $matcher = $instance; } } if (is_null($matcher)) { $matcher = new ArrayMatcher($this); } foreach (call_user_func([$matcher, 'getResolver']) as $match) { if (is_array($target = call_user_func_array([$matcher, $match], [$this->route->getCallback()]))) { call_user_func_array([$matcher, 'setTarget'], [$target]); $this->callTarget(); break; } } $this->response->setStatusCode($code); } $this->response->send(); }
/** * @return object * @throws \Exception */ private function getController() { $reflector = new ReflectionClass($this->route->getTarget('controller')); if (!$reflector->isInstantiable()) { throw new \Exception('Target [' . $this->route->getTarget('controller') . '] is not instantiable.'); } $constructor = $reflector->getConstructor(); if (is_null($constructor)) { $class = $this->route->getTarget('controller'); return call_user_func_array($this->route->getTarget('di'), [$class]); } $dependencies = $constructor->getParameters(); $arguments = []; foreach ($dependencies as $dep) { $class = $dep->getClass()->name; array_push($arguments, call_user_func_array($this->route->getTarget('di'), [$class])); } return $reflector->newInstanceArgs($arguments); }
/** * @description call template file */ public function call() { if ($this->response->getStatusCode() == 202) { $this->setContentType($this->route->getTarget('extension')); } if (isset($this->route->getTarget('callback')[$this->route->getTarget('extension')])) { $this->response->setContent(call_user_func_array($this->route->getTarget('callback')[$this->route->getTarget('extension')], [$this->route])); } else { ob_start(); if (isset($this->route->getTarget()['data'])) { extract($this->route->getTarget('data')); } if (isset($this->route->getParams()['data'])) { extract($this->route->getParams()['data']); } require $this->route->getTarget('template'); $this->response->setContent(ob_get_clean()); } }