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

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. class LazyProvider implements MenuProviderInterface
  12. {
  13.     /**
  14.      * @var array<string, mixed>
  15.      */
  16.     private $builders;
  17.     /**
  18.      * @phpstan-param array<string, callable|array{\Closure, string}> $builders
  19.      */
  20.     public function __construct(array $builders)
  21.     {
  22.         $this->builders $builders;
  23.     }
  24.     public function get(string $name, array $options = []): ItemInterface
  25.     {
  26.         if (!isset($this->builders[$name])) {
  27.             throw new \InvalidArgumentException(\sprintf('The menu "%s" is not defined.'$name));
  28.         }
  29.         $builder $this->builders[$name];
  30.         if (\is_array($builder) && isset($builder[0]) && $builder[0] instanceof \Closure) {
  31.             $builder[0] = $builder[0]();
  32.         }
  33.         if (!\is_callable($builder)) {
  34.             throw new \LogicException(\sprintf('Invalid menu builder for "%s". A callable or a factory for an object callable are expected.'$name));
  35.         }
  36.         return $builder($options);
  37.     }
  38.     public function has(string $name, array $options = []): bool
  39.     {
  40.         return isset($this->builders[$name]);
  41.     }
  42. }