/** * @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 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()); } }
/** * @return mixed * @throws \Exception */ public function call() { $reflectionMethod = new ReflectionMethod($this->route->getTarget('controller'), $this->route->getTarget('action')); $dependencies = []; $count = 0; foreach ($reflectionMethod->getParameters() as $arg) { is_null($arg->getClass()) ? $count++ : ($dependencies[] = call_user_func_array($this->route->getTarget('di'), [$arg->getClass()->name])); } if ($count == count($this->route->getParameters()) || $this->route->getParameters() == '' && $count == 0) { $dependencies = array_merge($dependencies, $this->route->getParameters() == '' ? [] : $this->route->getParameters()); if ($this->response->getStatusCode() == 202) { $this->response->setStatusCode(200); } if (is_array($content = $reflectionMethod->invokeArgs($this->getController(), $dependencies))) { $this->route->addTarget('data', $content); } elseif (!is_null($content)) { $this->response->setContent($content); } } else { $this->response->setStatusCode(404); } }
/** * @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(); }