vendor/knplabs/knp-menu/src/Knp/Menu/Provider/LazyProvider.php line 25

Open in your IDE?
  1. <?php
  2. namespace Knp\Menu\Provider;
  3. use Knp\Menu\ItemInterface;
  4. /**
  5. * A menu provider building menus lazily thanks to builder callables.
  6. *
  7. * Builders can either be callables or a factory for an object callable
  8. * represented as [Closure, method], where the Closure gets called
  9. * to instantiate the object.
  10. *
  11. * @final since 3.8.0
  12. */
  13. class LazyProvider implements MenuProviderInterface
  14. {
  15. /**
  16. * @phpstan-param array<string, (callable(): ItemInterface)|array{\Closure(): object, string}> $builders
  17. */
  18. public function __construct(private array $builders)
  19. {
  20. }
  21. public function get(string $name, array $options = []): ItemInterface
  22. {
  23. if (!isset($this->builders[$name])) {
  24. throw new \InvalidArgumentException(\sprintf('The menu "%s" is not defined.', $name));
  25. }
  26. $builder = $this->builders[$name];
  27. if (\is_array($builder) && isset($builder[0]) && $builder[0] instanceof \Closure) {
  28. $builder[0] = $builder[0]();
  29. }
  30. if (!\is_callable($builder)) {
  31. throw new \LogicException(\sprintf('Invalid menu builder for "%s". A callable or a factory for an object callable are expected.', $name));
  32. }
  33. return $builder($options);
  34. }
  35. public function has(string $name, array $options = []): bool
  36. {
  37. return isset($this->builders[$name]);
  38. }
  39. }