Yaffs site version 1.1
[yaffs-website] / vendor / symfony / console / Helper / HelperSet.php
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\Command\Command;
15 use Symfony\Component\Console\Exception\InvalidArgumentException;
16
17 /**
18  * HelperSet represents a set of helpers to be used with a command.
19  *
20  * @author Fabien Potencier <fabien@symfony.com>
21  */
22 class HelperSet implements \IteratorAggregate
23 {
24     /**
25      * @var Helper[]
26      */
27     private $helpers = array();
28     private $command;
29
30     /**
31      * Constructor.
32      *
33      * @param Helper[] $helpers An array of helper
34      */
35     public function __construct(array $helpers = array())
36     {
37         foreach ($helpers as $alias => $helper) {
38             $this->set($helper, is_int($alias) ? null : $alias);
39         }
40     }
41
42     /**
43      * Sets a helper.
44      *
45      * @param HelperInterface $helper The helper instance
46      * @param string          $alias  An alias
47      */
48     public function set(HelperInterface $helper, $alias = null)
49     {
50         $this->helpers[$helper->getName()] = $helper;
51         if (null !== $alias) {
52             $this->helpers[$alias] = $helper;
53         }
54
55         $helper->setHelperSet($this);
56     }
57
58     /**
59      * Returns true if the helper if defined.
60      *
61      * @param string $name The helper name
62      *
63      * @return bool true if the helper is defined, false otherwise
64      */
65     public function has($name)
66     {
67         return isset($this->helpers[$name]);
68     }
69
70     /**
71      * Gets a helper value.
72      *
73      * @param string $name The helper name
74      *
75      * @return HelperInterface The helper instance
76      *
77      * @throws InvalidArgumentException if the helper is not defined
78      */
79     public function get($name)
80     {
81         if (!$this->has($name)) {
82             throw new InvalidArgumentException(sprintf('The helper "%s" is not defined.', $name));
83         }
84
85         if ('dialog' === $name && $this->helpers[$name] instanceof DialogHelper) {
86             @trigger_error('"Symfony\Component\Console\Helper\DialogHelper" is deprecated since version 2.5 and will be removed in 3.0. Use "Symfony\Component\Console\Helper\QuestionHelper" instead.', E_USER_DEPRECATED);
87         } elseif ('progress' === $name && $this->helpers[$name] instanceof ProgressHelper) {
88             @trigger_error('"Symfony\Component\Console\Helper\ProgressHelper" is deprecated since version 2.5 and will be removed in 3.0. Use "Symfony\Component\Console\Helper\ProgressBar" instead.', E_USER_DEPRECATED);
89         } elseif ('table' === $name && $this->helpers[$name] instanceof TableHelper) {
90             @trigger_error('"Symfony\Component\Console\Helper\TableHelper" is deprecated since version 2.5 and will be removed in 3.0. Use "Symfony\Component\Console\Helper\Table" instead.', E_USER_DEPRECATED);
91         }
92
93         return $this->helpers[$name];
94     }
95
96     /**
97      * Sets the command associated with this helper set.
98      *
99      * @param Command $command A Command instance
100      */
101     public function setCommand(Command $command = null)
102     {
103         $this->command = $command;
104     }
105
106     /**
107      * Gets the command associated with this helper set.
108      *
109      * @return Command A Command instance
110      */
111     public function getCommand()
112     {
113         return $this->command;
114     }
115
116     /**
117      * @return Helper[]
118      */
119     public function getIterator()
120     {
121         return new \ArrayIterator($this->helpers);
122     }
123 }