/** * Generic factory method that provides access to objects configured using the DI container. * * @param callable $callback All arguments of the callback will be resolved using the DI container. * @return mixed Returns whatever is returned from the given callback. */ public function factory(callable $callback) { if (\is_array($callback)) { $ref = new \ReflectionMethod(\is_object($callback[0]) ? \get_class($callback[0]) : $callback[0], $callback[1]); } elseif (\is_object($callback) && !$callback instanceof \Closure) { $ref = new \ReflectionMethod(\get_class($callback), '__invoke'); } else { $ref = new \ReflectionFunction($callback); } return $callback(...$this->container->populateArguments($ref)); }
public function getDispatcher(Container $container, RouteCompiler $compiler) : Dispatcher { if ($this->dispatcher === null) { if ($this->routable instanceof Routable) { $this->dispatcher = new Dispatcher($container, $this->routable, $compiler); } else { $routable = $this->routable; if (!$routable instanceof Routable && \is_callable($routable)) { if (\is_array($routable)) { $ref = new \ReflectionMethod(\is_object($routable[0]) ? \get_class($routable[0]) : $routable[0], $routable[1]); } elseif (\is_object($routable) && !$routable instanceof \Closure) { $ref = new \ReflectionMethod(\get_class($routable), '__invoke'); } else { $ref = new \ReflectionFunction($routable); } $routable = $routable(...$container->populateArguments($ref)); } else { $routable = $container->get($this->routable); } $this->dispatcher = new Dispatcher($container, $routable, $compiler); } } return $this->dispatcher; }