public function testApply() { $closure = function ($param, $param2, $param3 = 0) { return [$param, $param2, $param3]; }; $this->assertSame(Reflection::apply($closure, ['param' => 1, 'param2' => 2]), [1, 2, 0]); $this->assertSame(Reflection::apply($closure, [9, 10]), [9, 10, 0]); $this->assertSame(Reflection::apply($closure, ['param3' => 17, 'param' => 7, 'param2' => 1]), [7, 1, 17]); }
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(); }