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