Gets the helper set.
public getHelperSet ( ) : Symfony\Component\Console\Helper\HelperSet | ||
Результат | Symfony\Component\Console\Helper\HelperSet | A HelperSet instance |
/** * Mocks question helper. */ protected function mockQuestionHelper() { $question_helper = $this->createMock('Symfony\\Component\\Console\\Helper\\QuestionHelper'); foreach ($this->answers as $key => $answer) { // @TODO: Figure out where this key ofset comes from. $question_helper->expects($this->at($key + 2))->method('ask')->willReturn($answer); } // We override the question helper with our mock. $this->command->getHelperSet()->set($question_helper, 'question'); }
public static function postInstall(Event $event) { // Check if npm is installed exec('hash npm 2>/dev/null || { exit 1; }', $result, $returnVar); $npmInstalled = $returnVar == 0 ? true : false; if ($npmInstalled === true) { // Instantiate the Command $command = new Command('post-create'); $command->setApplication(new Application()); // Load the dialog helper $dialog = $command->getHelperSet()->get('dialog'); // Write output to the console $output = new ConsoleOutput(); if ($dialog->askConfirmation($output, "\n<info>Install Gulp and it's dependencies?</info> [<comment>Y,n</comment>]? ", false)) { // Check if npm is installed exec('hash gulp 2>/dev/null || { exit 1; }', $out, $returnVar); $gulpInstalled = $returnVar == 0 ? true : false; if ($gulpInstalled === false) { $output->writeln("\n<info>Installing Gulp</info>\n"); $output->writeln("\$ sudo npm install -g gulp\n"); exec('sudo npm install -g gulp'); } $output->writeln("\n<info>Installing dependencies</info>\n"); $output->writeln("\$ npm install\n"); exec('npm install'); return; } } }
/** * @param Command $command * @param HelperInterface[] $helpers * @return CommandTester */ protected function getCommandTester(Command $command, array $helpers = []) { if (!empty($helpers)) { foreach ($helpers as $helper) { if ($helper instanceof HelperInterface) { $command->getHelperSet()->set($helper, $helper->getName()); } else { throw new \InvalidArgumentException('Cannot create CommandTester because a given helper is not an instance of HelperInterface.'); } } } return new CommandTester($command); }
public function createPlayerDialog(Command $command, OutputInterface $output) { /** @var $dialog DialogHelper */ $dialog = $command->getHelperSet()->get("dialog"); /** @var $translations TranslationsHelperInterface */ $translations = $command->getHelper("translations"); $player = new PlayerDataHelper(); $player->name = $dialog->ask($output, $translations->getLine("player_name") . ": "); $player->sex = $dialog->ask($output, $translations->getLine("player_sex_cmd") . ": "); $player->class = $dialog->ask($output, $translations->getLine("player_class_cmd") . ": "); $player->race = $dialog->ask($output, $translations->getLine("player_race_cmd") . ": "); return $player; }
protected function execute(InputInterface $input, OutputInterface $output) { if (!parent::getHelperSet()->has('link.connection')) { throw new \BadMethodCallException('You MUST provide a connection helper to run this command.'); } /** @var Connection $connection */ $connection = parent::getHelper('link.connection')->getConnection(); $dest = parent::getHelperSet()->has('link.destination') ? $this->getDestinationHelper()->getDestination() : $input->getArgument('destination'); $klass = str_replace('/', '\\', $input->getArgument('entity')); $entityName = substr($klass, strrpos($klass, '\\') + 1); $reader = new SqlReader(StringUtil::pluralize(strtolower($entityName)), $connection); $relations = []; foreach ($reader->getConstraints() as $constraint) { if (!$constraint instanceof ForeignKeyConstraint) { continue; } $relations = array_merge($relations, array_map(function (ColumnInterface $column) { return $column->getField(); }, $constraint->getColumns())); } $classBuilder = ClassBuilder::create($entityName, null, substr($klass, 0, strrpos($klass, '\\')))->extends(Entity::class); $commentBuilder = BlockCommentBuilder::create(); foreach ($reader->getColumns() as $column) { if (in_array($column->getField(), $relations)) { continue; } $retType = ColumnTypeHelper::toPhpType($column->getType()); $commentBuilder->addDocMethod($this->buildGetterName($column, $retType), false, [$retType]); if ($column->getField() !== 'id') { $field = StringUtil::classify($column->getField()); $commentBuilder->addDocMethod('set' . $field, false, ['$this'], [lcfirst($field) => ColumnTypeHelper::toPhpType($column->getType(), null)]); } } if (sizeof($relations) > 0) { $output->writeln(['<comment>This entity has foreign keys, which means it probably relates another entity (or ', 'entities) in your application. I cannot generate them, so you will need to add them yourself. ', 'Please see https://goo.gl/dFvGYo for information on how to do this.', '</>']); } $classBuilder->comment($commentBuilder->build()); if (!file_exists($dest)) { mkdir($dest, 0755, true); } if (file_exists($filePath = $dest . $entityName . '.php') && !$input->getOption('force')) { throw new \InvalidArgumentException('File already exists at ' . $dest); } $result = file_put_contents($filePath, '<?php' . PHP_EOL . $classBuilder->build()->build(1)); if ($result) { $output->writeln('<info>Generated class written to ' . $filePath . '</>'); } else { $output->writeln('<error>Could not write to ' . $filePath . '</>'); } }
/** * Runs the current command. * * If an event dispatcher has been attached to the application, * events are also dispatched during the life-cycle of the command. * * @param Command $command A Command instance * @param InputInterface $input An Input instance * @param OutputInterface $output An Output instance * * @return int 0 if everything went fine, or an error code */ protected function doRunCommand(Command $command, InputInterface $input, OutputInterface $output) { foreach ($command->getHelperSet() as $helper) { if ($helper instanceof InputAwareInterface) { $helper->setInput($input); } } if (null === $this->dispatcher) { return $command->run($input, $output); } $event = new ConsoleCommandEvent($command, $input, $output); $this->dispatcher->dispatch(ConsoleEvents::COMMAND, $event); try { $exitCode = $command->run($input, $output); } catch (\Exception $e) { $event = new ConsoleTerminateEvent($command, $input, $output, $e->getCode()); $this->dispatcher->dispatch(ConsoleEvents::TERMINATE, $event); $event = new ConsoleExceptionEvent($command, $input, $output, $e, $event->getExitCode()); $this->dispatcher->dispatch(ConsoleEvents::EXCEPTION, $event); throw $event->getException(); } $event = new ConsoleTerminateEvent($command, $input, $output, $exitCode); $this->dispatcher->dispatch(ConsoleEvents::TERMINATE, $event); return $event->getExitCode(); }
/** * Runs the current command. * * If an event dispatcher has been attached to the application, * events are also dispatched during the life-cycle of the command. * * @param Command $command A Command instance * @param InputInterface $input An Input instance * @param OutputInterface $output An Output instance * * @return int 0 if everything went fine, or an error code * * @throws \Exception when the command being run threw an exception */ protected function doRunCommand(Command $command, InputInterface $input, OutputInterface $output) { foreach ($command->getHelperSet() as $helper) { if ($helper instanceof InputAwareInterface) { $helper->setInput($input); } } if (null === $this->dispatcher) { return $command->run($input, $output); } // bind before the console.command event, so the listeners have access to input options/arguments try { $command->mergeApplicationDefinition(); $input->bind($command->getDefinition()); } catch (ExceptionInterface $e) { // ignore invalid options/arguments for now, to allow the event listeners to customize the InputDefinition } $event = new ConsoleCommandEvent($command, $input, $output); $this->dispatcher->dispatch(ConsoleEvents::COMMAND, $event); if ($event->commandShouldRun()) { try { $exitCode = $command->run($input, $output); } catch (\Exception $e) { $event = new ConsoleExceptionEvent($command, $input, $output, $e, $e->getCode()); $this->dispatcher->dispatch(ConsoleEvents::EXCEPTION, $event); $e = $event->getException(); $event = new ConsoleTerminateEvent($command, $input, $output, $e->getCode()); $this->dispatcher->dispatch(ConsoleEvents::TERMINATE, $event); throw $e; } } else { $exitCode = ConsoleCommandEvent::RETURN_CODE_DISABLED; } $event = new ConsoleTerminateEvent($command, $input, $output, $exitCode); $this->dispatcher->dispatch(ConsoleEvents::TERMINATE, $event); return $event->getExitCode(); }
/** * {@inheritdoc} */ public function getHelperSet() { return $this->decoratedCommand->getHelperSet(); }
/** * {@inheritdoc} */ protected function doRunCommand(Command $command, InputInterface $input, OutputInterface $output) { $helperSet = $command->getHelperSet(); foreach ($helperSet as $helper) { if ($helper instanceof OutputAwareInterface) { $helper->setOutput($output); } if ($helper instanceof InputAwareInterface) { $helper->setInput($input); } } $event = new ConsoleCommandEvent($command, $input, $output); $this->dispatcher->dispatch(ConsoleEvents::COMMAND, $event); try { $exitCode = $command->run($input, $output); } catch (\Exception $e) { $event = new ConsoleTerminateEvent($command, $input, $output, $e->getCode()); $this->dispatcher->dispatch(ConsoleEvents::TERMINATE, $event); $event = new ConsoleExceptionEvent($command, $input, $output, $e, $event->getExitCode()); $this->dispatcher->dispatch(ConsoleEvents::EXCEPTION, $event); if ($e instanceof UserException) { $this->getHelperSet()->get('gush_style')->error($e->getMessages()); if (OutputInterface::VERBOSITY_VERBOSE <= $output->getVerbosity()) { throw $e; } return $event->getExitCode(); } else { throw $event->getException(); } } $event = new ConsoleTerminateEvent($command, $input, $output, $exitCode); $this->dispatcher->dispatch(ConsoleEvents::TERMINATE, $event); return $event->getExitCode(); }
public function getHelperSet() { return $this->innerCommand->getHelperSet(); }
/** * @param Command $command */ private function mockDialogHelper(Command $command) { $mockBuilder = $this->getMockBuilder(\Symfony\Component\Console\Helper\QuestionHelper::class); $mockBuilder->setMethods(['ask']); $helperMock = $mockBuilder->getMock(); $helperMock->method('ask')->willReturn(true); $command->getHelperSet()->set($helperMock, 'question'); }
protected function doRunCommand(Command $command, InputInterface $input, OutputInterface $output) { foreach ($command->getHelperSet() as $helper) { if ($helper instanceof InputAwareInterface) { $helper->setInput($input); } } if (null === $this->dispatcher) { return $command->run($input, $output); } try { $command->mergeApplicationDefinition(); $input->bind($command->getDefinition()); } catch (ExceptionInterface $e) { } $event = new ConsoleCommandEvent($command, $input, $output); $this->dispatcher->dispatch(ConsoleEvents::COMMAND, $event); if ($event->commandShouldRun()) { try { $exitCode = $command->run($input, $output); } catch (\Exception $e) { $event = new ConsoleExceptionEvent($command, $input, $output, $e, $e->getCode()); $this->dispatcher->dispatch(ConsoleEvents::EXCEPTION, $event); $e = $event->getException(); $event = new ConsoleTerminateEvent($command, $input, $output, $e->getCode()); $this->dispatcher->dispatch(ConsoleEvents::TERMINATE, $event); throw $e; } } else { $exitCode = ConsoleCommandEvent::RETURN_CODE_DISABLED; } $event = new ConsoleTerminateEvent($command, $input, $output, $exitCode); $this->dispatcher->dispatch(ConsoleEvents::TERMINATE, $event); return $event->getExitCode(); }
/** * Render the table and pass the output back. * This is done this way because the table * helper dumps everything to the output and * there is no way to catch so have to override * with a special output. * * @param Command $command * @return void */ public function __construct(Command $command) { $this->table = $command->getHelperSet()->get('table'); $this->table->setCellHeaderFormat('<pop>%s</pop>'); $this->output = new CatchOutput(); }
/** * @param Command $cmd Symfony question to ask * @param callable $mockQuestionHandler anonymous function to handle the asked questions * * @since 0.1.0 2015-05-20 */ function mockQuestionHelper(Command $cmd, callable $mockQuestionHandler) { $helper = new QuestionHelperMock(); $helper->setMockQuestionHandler($mockQuestionHandler); $cmd->getHelperSet()->set($helper, 'question'); }