/** * @param $name * @param $repositoryClass * * @return $this * @throws InvalidRepositoryClassException * @throws RepositoryClassNotFoundException */ public function resolve($name, $repositoryClass) { if (!class_exists($repositoryClass)) { throw new RepositoryClassNotFoundException(s('Class "%s" not found.', $repositoryClass)); } if (!array_contains(class_implements($repositoryClass), ObjectRepository::class)) { throw new InvalidRepositoryClassException(s('Class "%s" must implement interface "%s".', $repositoryClass, ObjectRepository::class)); } $this->router->addResolver($name, function ($parameter, IContainer $container) use($repositoryClass) { $repository = $container->get($repositoryClass); return $repository->find($parameter); }); return $this; }
/** * @param IRouter $router * @param array $config * * @throws InvalidConfigurationException */ protected function processResolvers(IRouter $router, array $config) { $resolvers = array_get($config, 'resolvers', []); if (!is_array($resolvers)) { throw new InvalidConfigurationException('Routing resolvers "resolvers" must be an array.'); } foreach ($resolvers as $resolver) { $name = array_get($resolver, 'name'); $handler = array_get($resolver, 'resolver'); if (empty($name)) { throw new InvalidConfigurationException('Routing resolver must have a "name".'); } if (!is_callable($handler)) { throw new InvalidConfigurationException('Routing resolver must have a callable "resolver".'); } $router->addResolver($name, $handler); } }