|
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 |
|
16 /** |
|
17 * HelperSet represents a set of helpers to be used with a command. |
|
18 * |
|
19 * @author Fabien Potencier <fabien@symfony.com> |
|
20 */ |
|
21 class HelperSet |
|
22 { |
|
23 private $helpers; |
|
24 private $command; |
|
25 |
|
26 /** |
|
27 * @param Helper[] $helpers An array of helper. |
|
28 */ |
|
29 public function __construct(array $helpers = array()) |
|
30 { |
|
31 $this->helpers = array(); |
|
32 foreach ($helpers as $alias => $helper) { |
|
33 $this->set($helper, is_int($alias) ? null : $alias); |
|
34 } |
|
35 } |
|
36 |
|
37 /** |
|
38 * Sets a helper. |
|
39 * |
|
40 * @param HelperInterface $helper The helper instance |
|
41 * @param string $alias An alias |
|
42 */ |
|
43 public function set(HelperInterface $helper, $alias = null) |
|
44 { |
|
45 $this->helpers[$helper->getName()] = $helper; |
|
46 if (null !== $alias) { |
|
47 $this->helpers[$alias] = $helper; |
|
48 } |
|
49 |
|
50 $helper->setHelperSet($this); |
|
51 } |
|
52 |
|
53 /** |
|
54 * Returns true if the helper if defined. |
|
55 * |
|
56 * @param string $name The helper name |
|
57 * |
|
58 * @return Boolean true if the helper is defined, false otherwise |
|
59 */ |
|
60 public function has($name) |
|
61 { |
|
62 return isset($this->helpers[$name]); |
|
63 } |
|
64 |
|
65 /** |
|
66 * Gets a helper value. |
|
67 * |
|
68 * @param string $name The helper name |
|
69 * |
|
70 * @return HelperInterface The helper instance |
|
71 * |
|
72 * @throws \InvalidArgumentException if the helper is not defined |
|
73 */ |
|
74 public function get($name) |
|
75 { |
|
76 if (!$this->has($name)) { |
|
77 throw new \InvalidArgumentException(sprintf('The helper "%s" is not defined.', $name)); |
|
78 } |
|
79 |
|
80 return $this->helpers[$name]; |
|
81 } |
|
82 |
|
83 /** |
|
84 * Sets the command associated with this helper set. |
|
85 * |
|
86 * @param Command $command A Command instance |
|
87 */ |
|
88 public function setCommand(Command $command = null) |
|
89 { |
|
90 $this->command = $command; |
|
91 } |
|
92 |
|
93 /** |
|
94 * Gets the command associated with this helper set. |
|
95 * |
|
96 * @return Command A Command instance |
|
97 */ |
|
98 public function getCommand() |
|
99 { |
|
100 return $this->command; |
|
101 } |
|
102 } |