Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
/*
4
 * This file is part of the Symfony package.
5
 *
6
 * (c) Fabien Potencier <fabien@symfony.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
 
12
namespace Symfony\Component\Console\Helper;
13
 
14
use Symfony\Component\Console\Exception\InvalidArgumentException;
15
 
16
/**
17
 * HelperSet represents a set of helpers to be used with a command.
18
 *
19
 * @author Fabien Potencier <fabien@symfony.com>
20
 *
21
 * @implements \IteratorAggregate<string, HelperInterface>
22
 */
23
class HelperSet implements \IteratorAggregate
24
{
25
    /** @var array<string, HelperInterface> */
26
    private array $helpers = [];
27
 
28
    /**
29
     * @param HelperInterface[] $helpers
30
     */
31
    public function __construct(array $helpers = [])
32
    {
33
        foreach ($helpers as $alias => $helper) {
34
            $this->set($helper, \is_int($alias) ? null : $alias);
35
        }
36
    }
37
 
38
    public function set(HelperInterface $helper, string $alias = null)
39
    {
40
        $this->helpers[$helper->getName()] = $helper;
41
        if (null !== $alias) {
42
            $this->helpers[$alias] = $helper;
43
        }
44
 
45
        $helper->setHelperSet($this);
46
    }
47
 
48
    /**
49
     * Returns true if the helper if defined.
50
     */
51
    public function has(string $name): bool
52
    {
53
        return isset($this->helpers[$name]);
54
    }
55
 
56
    /**
57
     * Gets a helper value.
58
     *
59
     * @throws InvalidArgumentException if the helper is not defined
60
     */
61
    public function get(string $name): HelperInterface
62
    {
63
        if (!$this->has($name)) {
64
            throw new InvalidArgumentException(sprintf('The helper "%s" is not defined.', $name));
65
        }
66
 
67
        return $this->helpers[$name];
68
    }
69
 
70
    public function getIterator(): \Traversable
71
    {
72
        return new \ArrayIterator($this->helpers);
73
    }
74
}