Mercurial > hg > isophonics-drupal-site
comparison vendor/symfony/console/Helper/HelperSet.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children | 1fec387a4317 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
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 return $this->helpers[$name]; | |
86 } | |
87 | |
88 /** | |
89 * Sets the command associated with this helper set. | |
90 * | |
91 * @param Command $command A Command instance | |
92 */ | |
93 public function setCommand(Command $command = null) | |
94 { | |
95 $this->command = $command; | |
96 } | |
97 | |
98 /** | |
99 * Gets the command associated with this helper set. | |
100 * | |
101 * @return Command A Command instance | |
102 */ | |
103 public function getCommand() | |
104 { | |
105 return $this->command; | |
106 } | |
107 | |
108 /** | |
109 * @return Helper[] | |
110 */ | |
111 public function getIterator() | |
112 { | |
113 return new \ArrayIterator($this->helpers); | |
114 } | |
115 } |