/** * Call the given function using the given parameters. * * Missing parameters will be resolved from the container. * * @param callable $callable Function to call. * @param array $parameters Parameters to use. Can be indexed by the parameter names * or not indexed (same order as the parameters). * The array can also contain DI definitions, e.g. DI\get(). * * @throws \Exception * @return mixed Result of the function. */ public function call($callable, array $parameters = []) { if (!method_exists($this->container, 'call')) { throw new Exception('Container method not found'); } return $this->container->call($callable, $parameters); }
/** * Registers/Runs a Hook File. * * @param string $file The filepath to where the hook exists. * @return void */ private function registerHook($file) { $hook = import($file); if (is_callable($hook)) { $this->container->call($hook, ['config' => $this->config]); } }
/** * Finds all theme route files and adds the routes to the RouteCollection. * * @return void */ private function discoverRoutes() { // Where are our routes located? $parentRoutes = $this->config->paths->theme->parent->routes; $childRoutes = $this->config->paths->theme->child->routes; $files = $this->finder->createFinder()->files()->name('*.php')->in($parentRoutes); if ($parentRoutes != $childRoutes && is_dir($childRoutes)) { $files = $files->in($childRoutes); } // Collect all the files $splFiles = []; foreach ($files as $file) { $splFiles[] = $file; } // Now we need to sort them ourselves because it seems Finder's // sortByName(), only sorts per in('dir') and not across the // entire list. $orderedFiles = Linq::from($splFiles)->orderBy('$v', function ($a, $b) { return strnatcasecmp($a->getBasename('.php'), $b->getBasename('.php')); }); // Finally lets add some routes. foreach ($orderedFiles as $file) { $route = import($file->getRealPath(), ['route' => $this->routes]); if (is_callable($route)) { $this->container->call($route, ['config' => $this->config]); } } }
/** * Dispatch a route. * * @param ResolvedRequestInterface $resolvedRequest A resolved request. * @return string The response. */ public function dispatch(ResolvedRequestInterface $resolvedRequest) { $route = $resolvedRequest->getRoute(); $controller = $this->container->make($route->getController(), ['resolvedRequest' => $resolvedRequest]); // Call the before hook, if defined. if (method_exists($controller, 'before')) { $this->container->call([$controller, 'before'], ['resolvedRequest' => $resolvedRequest]); } // Call the action. $response = $this->container->call([$controller, 'action' . $route->getAction()], ['resolvedRequest' => $resolvedRequest]); // Call the after hook, if defined. if (method_exists($controller, 'after')) { $this->container->call([$controller, 'after'], ['resolvedRequest' => $resolvedRequest]); } return $response; }
/** * {@inheritdoc} */ public function handle(Route $route, array $inputs) { return $this->invoker->call($route->getHandler(), [$inputs]); }
/** * Used by Relay\Runner when it dispatches the middleware stack. * * @param string $file * @return Closure */ private function resolve($file) { return function (IServerRequest $request, IResponse $response, callable $next) use($file) { return $this->container->call(import($file), ['request' => $request, 'response' => $response, 'next' => $next, 'config' => $this->config]); }; }