environment = $kernelEnvironment; parent::__construct(); } /** * {@inheritdoc} */ protected function configure() { $this ->setName('kimai:reset-dev') ->setDescription('Resets the dev environment') ->setHelp( <<-n switch to skip the question. EOT ) ->addOption('no-cache', null, InputOption::VALUE_NONE, 'Skip cache flushing') ; } /** * Make sure that this command CANNOT be executed in production. * It can't work, as the fixtures bundle is not available in production. * * @return bool */ public function isEnabled() { return $this->environment !== 'prod'; } /** * @param InputInterface $input * @param OutputInterface $output * @return int|null */ protected function execute(InputInterface $input, OutputInterface $output) { $io = new SymfonyStyle($input, $output); if ($this->askConfirmation($input, $output, 'Do you want to create the database y/N ?')) { try { $command = $this->getApplication()->find('doctrine:database:create'); $command->run(new ArrayInput([]), $output); } catch (Exception $ex) { $io->error('Failed to create database: ' . $ex->getMessage()); return 1; } } if ($this->askConfirmation($input, $output, 'Do you want to drop and re-create the schema y/N ?')) { try { $command = $this->getApplication()->find('doctrine:schema:drop'); $command->run(new ArrayInput(['--force' => true]), $output); } catch (Exception $ex) { $io->error('Failed to drop database schema: ' . $ex->getMessage()); return 2; } try { $command = $this->getApplication()->find('doctrine:schema:create'); $command->run(new ArrayInput([]), $output); } catch (Exception $ex) { $io->error('Failed to create database schema: ' . $ex->getMessage()); return 3; } } try { $command = $this->getApplication()->find('doctrine:fixtures:load'); $cmdInput = new ArrayInput([]); $cmdInput->setInteractive(false); $command->run($cmdInput, $output); } catch (Exception $ex) { $io->error('Failed to import fixtures: ' . $ex->getMessage()); return 4; } try { $command = $this->getApplication()->find('doctrine:migrations:version'); $cmdInput = new ArrayInput(['--add' => true, '--all' => true]); $cmdInput->setInteractive(false); $command->run($cmdInput, $output); } catch (Exception $ex) { $io->error('Failed to set migration status: ' . $ex->getMessage()); return 5; } if (!$input->getOption('no-cache')) { $command = $this->getApplication()->find('cache:clear'); try { $command->run(new ArrayInput([]), $output); } catch (Exception $ex) { $io->error('Failed to clear cache: ' . $ex->getMessage()); return 6; } } return 0; } /** * @param InputInterface $input * @param OutputInterface $output * @param string $question * @param bool $default * @return bool */ private function askConfirmation(InputInterface $input, OutputInterface $output, $question, $default = false) { if (!$input->isInteractive()) { return true; } /** @var QuestionHelper $questionHelper */ $questionHelper = $this->getHelperSet()->get('question'); $question = new ConfirmationQuestion('' . $question . '', $default); return $questionHelper->ask($input, $output, $question); } }