Beispiel #1
0
 public function run()
 {
     list($handler, $params, $module) = $this->router->dispatch();
     if (!$handler) {
         return $this->response->notFound();
     }
     set_include_path($this->resolvePath('modules', $module) . PATH_SEPARATOR . get_include_path());
     $this->useMiddleware('handler', function () use($handler) {
         if (is_string($handler)) {
             if (strpos($handler, ':')) {
                 $handler = preg_replace_callback('#:([^/]+)#', function ($matches) {
                     return $this->router->params[$matches[1]];
                 }, $handler);
             }
             $handler = Reflection::getClosure($this->resource->resolve($handler));
         }
         $realHandler = function () use($handler) {
             $params = [];
             foreach (Reflection::parameters($handler) as $param) {
                 if (isset($this->params->{$param->name})) {
                     $params[$param->name] = $this->params->{$param->name};
                 } elseif (!$param->isOptional()) {
                     return [400, "parameter \"{$param->name}\" is required"];
                 }
             }
             return Reflection::apply($handler, $params, $this);
         };
         if ($middlewares = $this->processDocString($handler)) {
             $this->useMiddleware($middlewares);
             $this->useMiddleware('realHandler', $realHandler);
         } else {
             return $realHandler();
         }
     });
     return $this->runAllMiddlewares();
 }
Beispiel #2
0
 public function testParameters()
 {
     $closure = function ($foo, $bar = null) {
     };
     $this->assertEquals(count(Reflection::parameters($closure)), 2);
 }