Actualización

This commit is contained in:
Xes
2025-04-10 12:24:57 +02:00
parent 8969cc929d
commit 45420b6f0d
39760 changed files with 4303286 additions and 0 deletions

View File

@@ -0,0 +1,46 @@
<?php
namespace Knp\Menu\Provider;
/**
* A menu provider getting the menus from a class implementing ArrayAccess.
*
* In case the value stored in the registry is a callable rather than an ItemInterface,
* it will be called with the options as first argument and the registry as second argument
* and is expected to return a menu item.
*/
class ArrayAccessProvider implements MenuProviderInterface
{
private $registry;
private $menuIds;
/**
* @param \ArrayAccess $registry
* @param array $menuIds The map between menu identifiers and registry keys
*/
public function __construct(\ArrayAccess $registry, array $menuIds = [])
{
$this->registry = $registry;
$this->menuIds = $menuIds;
}
public function get($name, array $options = [])
{
if (!isset($this->menuIds[$name])) {
throw new \InvalidArgumentException(\sprintf('The menu "%s" is not defined.', $name));
}
$menu = $this->registry[$this->menuIds[$name]];
if (\is_callable($menu)) {
$menu = \call_user_func($menu, $options, $this->registry);
}
return $menu;
}
public function has($name, array $options = [])
{
return isset($this->menuIds[$name]);
}
}

View File

@@ -0,0 +1,38 @@
<?php
namespace Knp\Menu\Provider;
class ChainProvider implements MenuProviderInterface
{
private $providers;
/**
* @param MenuProviderInterface[]|iterable $providers
*/
public function __construct($providers)
{
$this->providers = $providers;
}
public function get($name, array $options = [])
{
foreach ($this->providers as $provider) {
if ($provider->has($name, $options)) {
return $provider->get($name, $options);
}
}
throw new \InvalidArgumentException(\sprintf('The menu "%s" is not defined.', $name));
}
public function has($name, array $options = [])
{
foreach ($this->providers as $provider) {
if ($provider->has($name, $options)) {
return true;
}
}
return false;
}
}

View File

@@ -0,0 +1,44 @@
<?php
namespace Knp\Menu\Provider;
/**
* A menu provider building menus lazily thanks to builder callables.
*
* Builders can either be callables or a factory for an object callable
* represented as array(Closure, method), where the Closure gets called
* to instantiate the object.
*/
class LazyProvider implements MenuProviderInterface
{
private $builders;
public function __construct(array $builders)
{
$this->builders = $builders;
}
public function get($name, array $options = [])
{
if (!isset($this->builders[$name])) {
throw new \InvalidArgumentException(\sprintf('The menu "%s" is not defined.', $name));
}
$builder = $this->builders[$name];
if (\is_array($builder) && isset($builder[0]) && $builder[0] instanceof \Closure) {
$builder[0] = $builder[0]();
}
if (!\is_callable($builder)) {
throw new \LogicException(\sprintf('Invalid menu builder for "%s". A callable or a factory for an object callable are expected.', $name));
}
return \call_user_func($builder, $options);
}
public function has($name, array $options = [])
{
return isset($this->builders[$name]);
}
}

View File

@@ -0,0 +1,28 @@
<?php
namespace Knp\Menu\Provider;
interface MenuProviderInterface
{
/**
* Retrieves a menu by its name
*
* @param string $name
* @param array $options
*
* @return \Knp\Menu\ItemInterface
*
* @throws \InvalidArgumentException if the menu does not exists
*/
public function get($name, array $options = []);
/**
* Checks whether a menu exists in this provider
*
* @param string $name
* @param array $options
*
* @return bool
*/
public function has($name, array $options = []);
}

View File

@@ -0,0 +1,18 @@
<?php
namespace Knp\Menu\Provider;
@trigger_error('The '.__NAMESPACE__.'\PimpleProvider class is deprecated since version 2.1 and will be removed in 3.0. Use the '.__NAMESPACE__.'\ArrayAccessProvider class instead.', E_USER_DEPRECATED);
/**
* Menu provider getting menus from a Pimple 1 container
*
* @deprecated use the ArrayAccessProvider instead.
*/
class PimpleProvider extends ArrayAccessProvider
{
public function __construct(\Pimple $pimple, array $menuIds = [])
{
parent::__construct($pimple, $menuIds);
}
}

View File

@@ -0,0 +1,35 @@
<?php
namespace Knp\Menu\Provider;
use Psr\Container\ContainerInterface;
/**
* A menu provider getting the menus from a PSR-11 container.
*
* This menu provider does not support using options, as it cannot pass them to the container
* to alter the menu building. Use a different provider in case you need support for options.
*/
class PsrProvider implements MenuProviderInterface
{
private $container;
public function __construct(ContainerInterface $container)
{
$this->container = $container;
}
public function get($name, array $options = [])
{
if (!$this->container->has($name)) {
throw new \InvalidArgumentException(\sprintf('The menu "%s" is not defined.', $name));
}
return $this->container->get($name);
}
public function has($name, array $options = [])
{
return $this->container->has($name);
}
}