/**
  * @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);
     }
 }
 /**
  * @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);
     }
 }
Exemple #3
0
 /**
  * @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();
 }
 /**
  * @param $extension
  */
 public function setContentType($extension)
 {
     $this->response->setStatusCode(200);
     isset($this->types[$extension]) ? $this->response->setHeaders(['Content-Type' => $this->types[$extension]]) : $this->response->setHeaders(['Content-Type' => $this->types['html']]);
 }