public function __call($name, $params) { if (isset($this->_registered[$name])) { $closure = $this->_registered[$name]; } else { $this->_namespace[] = $name; $fn = implode('/', $this->_namespace); $this->_namespace = null; if (!($fullname = $this->resolve($fn))) { throw new Exception("can't locate {$fn} in {$this->_path}"); } $closure = Reflection::getClosure($fullname); } if ($this->_context) { $closure = $closure->bindTo($this->_context); } return call_user_func_array($closure, $params); }
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(); }