console-helpers /
console-kit
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * This file is part of the Console-Kit library. |
||
| 4 | * For the full copyright and license information, please view |
||
| 5 | * the LICENSE file that was distributed with this source code. |
||
| 6 | * |
||
| 7 | * @copyright Alexander Obuhovich <[email protected]> |
||
| 8 | * @link https://github.com/console-helpers/console-kit |
||
| 9 | */ |
||
| 10 | |||
| 11 | namespace ConsoleHelpers\ConsoleKit\Command; |
||
| 12 | |||
| 13 | |||
| 14 | use ConsoleHelpers\ConsoleKit\Application; |
||
| 15 | use ConsoleHelpers\ConsoleKit\ConsoleIO; |
||
| 16 | use ConsoleHelpers\ConsoleKit\Helper\ContainerHelper; |
||
| 17 | use ConsoleHelpers\ConsoleKit\Container; |
||
| 18 | use Stecman\Component\Symfony\Console\BashCompletion\Completion\CompletionAwareInterface; |
||
| 19 | use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext; |
||
| 20 | use Symfony\Component\Console\Command\Command; |
||
| 21 | use Symfony\Component\Console\Input\ArrayInput; |
||
| 22 | use Symfony\Component\Console\Input\InputInterface; |
||
| 23 | use Symfony\Component\Console\Output\OutputInterface; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Base command class. |
||
| 27 | */ |
||
| 28 | abstract class AbstractCommand extends Command implements CompletionAwareInterface |
||
| 29 | { |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Console IO. |
||
| 33 | * |
||
| 34 | * @var ConsoleIO |
||
| 35 | */ |
||
| 36 | protected $io; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * {@inheritdoc} |
||
| 40 | */ |
||
| 41 | protected function initialize(InputInterface $input, OutputInterface $output) |
||
| 42 | { |
||
| 43 | parent::initialize($input, $output); |
||
| 44 | |||
| 45 | // Don't use IO from container, because it contains outer IO which doesn't reflect sub-command calls. |
||
| 46 | $this->io = new ConsoleIO($input, $output, $this->getHelperSet()); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 47 | |||
| 48 | $this->prepareDependencies(); |
||
| 49 | } |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Return possible values for the named option |
||
| 53 | * |
||
| 54 | * @param string $optionName Option name. |
||
| 55 | * @param CompletionContext $context Completion context. |
||
| 56 | * |
||
| 57 | * @return array |
||
| 58 | */ |
||
| 59 | public function completeOptionValues($optionName, CompletionContext $context) |
||
| 60 | { |
||
| 61 | $this->prepareDependencies(); |
||
| 62 | |||
| 63 | return array(); |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Return possible values for the named argument |
||
| 68 | * |
||
| 69 | * @param string $argumentName Argument name. |
||
| 70 | * @param CompletionContext $context Completion context. |
||
| 71 | * |
||
| 72 | * @return array |
||
| 73 | */ |
||
| 74 | public function completeArgumentValues($argumentName, CompletionContext $context) |
||
| 75 | { |
||
| 76 | $this->prepareDependencies(); |
||
| 77 | |||
| 78 | return array(); |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Prepare dependencies. |
||
| 83 | * |
||
| 84 | * @return void |
||
| 85 | */ |
||
| 86 | protected function prepareDependencies() |
||
| 87 | { |
||
| 88 | |||
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Runs another command. |
||
| 93 | * |
||
| 94 | * @param string $name Command name. |
||
| 95 | * @param array $arguments Arguments. |
||
| 96 | * |
||
| 97 | * @return integer |
||
| 98 | */ |
||
| 99 | protected function runOtherCommand($name, array $arguments = array()) |
||
| 100 | { |
||
| 101 | $arguments['command'] = $name; |
||
| 102 | $cleanup_command = $this->getApplication()->find($name); |
||
| 103 | |||
| 104 | $input = new ArrayInput($arguments); |
||
| 105 | |||
| 106 | return $cleanup_command->run($input, $this->io->getOutput()); |
||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Returns container. |
||
| 111 | * |
||
| 112 | * @return Container |
||
| 113 | */ |
||
| 114 | protected function getContainer() |
||
| 115 | { |
||
| 116 | static $container; |
||
| 117 | |||
| 118 | if ( !isset($container) ) { |
||
| 119 | /** @var ContainerHelper $container_helper */ |
||
| 120 | $container_helper = $this->getHelper('container'); |
||
| 121 | |||
| 122 | $container = $container_helper->getContainer(); |
||
| 123 | } |
||
| 124 | |||
| 125 | return $container; |
||
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Gets the application instance for this command. |
||
| 130 | * |
||
| 131 | * @return Application |
||
| 132 | */ |
||
| 133 | public function getApplication() |
||
| 134 | { |
||
| 135 | return parent::getApplication(); |
||
| 136 | } |
||
| 137 | |||
| 138 | } |
||
| 139 |