/** * @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); } }
/** * */ public function callTarget() { $target = is_array($this->route->getTarget('dispatcher')) ? $this->route->getTarget('dispatcher') : [$this->route->getTarget('dispatcher')]; if (!empty($target)) { foreach ($target as $call) { $this->dispatcher = new $call($this->route, $this->response); call_user_func([$this->dispatcher, 'call']); } } }
/** * @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 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); }