public function interact(InputInterface $input, OutputInterface $output) { $io = new SymfonyStyle($input, $output); $bundle = $input->getArgument('bundle'); $name = $input->getArgument('name'); $container = $this->getContainer(); if (null !== $bundle && null !== $name) { return; } $io->title('Generate new RPC method'); // Bundle name $bundle = $io->ask('Bundle name', null, function ($answer) use($container) { try { $container->get('kernel')->getBundle($answer); } catch (\Exception $e) { throw new \RuntimeException(sprintf('Bundle "%s" does not exist.', $answer)); } return $answer; }); $input->setArgument('bundle', $bundle); // Method name $name = $io->ask('Method name', null, function ($answer) use($container, $bundle) { if (empty($answer)) { throw new \RuntimeException('Method name can`t be empty.'); } $answer = str_replace(' ', ':', $answer); if ($this->isMethodExist($container->get('kernel')->getBundle($bundle), $answer)) { throw new \RuntimeException(sprintf('Method "%s" already exist.', $answer)); } return $answer; }); $input->setArgument('name', $name); }
/** * {@inheritdoc} */ protected function interact(InputInterface $input, OutputInterface $output) { foreach ($input->getOptions() as $option => $value) { if ($value === null) { $input->setOption($option, $this->io->ask(sprintf('%s', ucfirst($option)))); } } }
/** * {@inheritdoc} */ protected function interact(InputInterface $input, OutputInterface $output) { $io = new SymfonyStyle($input, $output); // --title option if (!$input->getOption('title')) { $input->setOption('title', $io->ask('Enter the title of the post')); } // --fieldname option if (!$input->getOption('filename')) { $date = \DateTime::createFromFormat('U', time())->format('Y-m-d'); $input->setOption('filename', $io->ask('Enter the name of the file', $date . self::FILENAME_SEPARATOR . str_replace(' ', self::FILENAME_SEPARATOR, strtolower($input->getOption('title'))) . '.' . self::FILETYPE)); } }
/** * Loads the local configuration from "~/.localhook/config.json" file. */ protected function loadConfiguration() { try { $configuration = $this->configurationStorage->loadFromFile()->get(); } catch (NoConfigurationException $e) { $this->io->comment($e->getMessage()); if (!$this->secret) { $this->secret = $this->io->ask('Secret'); } $configuration = $this->parseConfigurationKey(); } $this->serverUrl = $configuration['socket_url']; $this->secret = $configuration['secret']; $this->configurationStorage->merge($configuration)->save(); }
/** * @return bool */ private function askToInstall() { if (array_key_exists($this->workingLocale, $this->installedLocale)) { $reinstallLocale = $this->formatter->confirm('The locale is already installed, would you like to reinstall and overwrite the current translations?', false); if (!$reinstallLocale) { return true; } $this->installWorkingLocale(true); return true; } $install = $this->formatter->confirm('Would you like to install this locale?'); if (!$install) { return false; } $this->formatter->writeln('<info>Before you can enable a new locale you need to authenticate to be able to create the pages</info>'); while (!Authentication::loginUser($this->formatter->ask('Login'), $this->formatter->askHidden('Password'))) { $this->formatter->error('Failed to login, please try again'); } if (!Authentication::isAllowedAction('Copy', 'Pages')) { $this->formatter->error('Your profile doesn\'t have the permission to execute the action Copy of the Pages module'); return false; } $this->installWorkingLocale(); $this->formatter->writeln('<info>Copying pages from the default locale to the current locale</info>'); BackendPagesModel::copy($this->defaultEnabledLocale, $this->workingLocale); return true; }
/** * {@inheritdoc} */ protected function interact(InputInterface $input, OutputInterface $output) { $io = new SymfonyStyle($input, $output); // --title option if (!$input->getOption('title')) { $input->setOption('title', $io->ask('Enter the title of the page')); } // --extension option if (!$input->getOption('extension')) { $input->setOption('extension', $io->choice('Which file extension?', ['md', 'html.twig', 'twig', 'html'])); } // --fieldname option if (!$input->getOption('filename')) { $input->setOption('filename', $io->ask('Enter the name of the file', str_replace(' ', self::FILENAME_SEPARATOR, strtolower($input->getOption('title'))) . '.' . $input->getOption('extension'))); } }
/** * {@inheritdoc} */ protected function execute(InputInterface $input, OutputInterface $output) { $io = new SymfonyStyle($input, $output); $io->title('Pre-commit install'); $git = new GitVersionControl(); $projectBase = $git->getProjectBase(); $phpunit = $io->confirm('Enable PhpUnit ?', true); $source = realpath($projectBase); $hookDir = $source . '/.git/hooks'; $defaultPhpUnitConfFile = $source . '/' . self::PHPUNIT_DEFAULT_CONF_FILENAME; $precommitCommand = sprintf('precommit check%s', $phpunit ? ' --phpunit true' : ''); if ($phpunit) { $phpunitPath = $io->ask('Specify Phpunit bin path [example: vendor/bin/phpunit] ? : ', 'phpunit'); $phpunitConfFile = $io->ask('Specify Phpunit config file path ? : ', $defaultPhpUnitConfFile); if ($phpunitPath != '') { if (strpos($phpunitPath, '/') !== false) { $phpunitPath = $source . '/' . $phpunitPath; if (!is_file($phpunitPath)) { $io->error(sprintf('No phpunit bin found "%s"', $phpunitPath)); exit(1); } } } if (!is_file($phpunitConfFile)) { $io->error(sprintf('No phpunit conf file found "%s"', $phpunitConfFile)); exit(1); } $precommitCommand .= $phpunitPath != 'phpunit' ? ' --phpunit-bin-path ' . $phpunitPath : ''; $precommitCommand .= $phpunitConfFile != $defaultPhpUnitConfFile ? ' --phpunit-conf ' . $phpunitConfFile : ''; } if (!is_dir($hookDir)) { $io->error(sprintf('The git hook directory does not exist (%s)', $hookDir)); exit(1); } $target = $hookDir . '/pre-commit'; $fs = new Filesystem(); if (!is_file($target)) { $fileContent = sprintf("#!/bin/sh\n%s", $precommitCommand); $fs->dumpFile($target, $fileContent); chmod($target, 0755); $io->success('pre-commit file correctly updated'); } else { $io->note(sprintf('A pre-commit file is already exist. Please add "%s" at the end !', $precommitCommand)); } exit(0); }
protected function execute(InputInterface $input, OutputInterface $output) { $io = new SymfonyStyle($input, $output); $jwt = $this->getContainer()->get('jwt_coder'); $username = $input->getArgument('username'); if (!$username) { $username = $io->ask('Username'); } $io->text('Token: ' . $jwt->encode(['username' => $username])); $io->success('JWT created'); }
protected function askAppBasePath($message, $default) { $appBasePath = $this->io->ask($message, $default); if ($appBasePath[0] === '~') { if (!function_exists('posix_getuid')) { throw new \InvalidArgumentException('cannot use tilde(~) character in your php enviroment.'); } $info = posix_getpwuid(posix_getuid()); $appBasePath = str_replace('~', $info['dir'], $appBasePath); } if ($appBasePath[0] !== '/') { $appBasePath = $this->basePath . "/{$appBasePath}"; } return rtrim($appBasePath, '/'); }
/** * Password Replace action * * @param SymfonyStyle $helper * * @return null */ private function actionPasswordReplace(SymfonyStyle $helper) { if (false === $helper->confirm('Are you sure? Only use this for development purposes!', false)) { return null; } $userClass = $this->getUserEntityClass(); if (null === $userClass) { throw new \RuntimeException('User entity not found'); } $password = $helper->ask('New password', 'login123'); $userManager = $this->getContainer()->get('fos_user.user_manager'); /** @var User $user */ foreach ($this->getDoctrine()->getRepository($userClass)->findAll() as $user) { $user->setPlainPassword($password); $userManager->updateUser($user); } return null; }
/** * Prompt the user for input. * * @param string $question * @param string $default * @return string */ public function ask($question, $default = null) { return $this->output->ask($question, $default); }
public function testSymfonyStyleCommandWithInputs() { $questions = array('What\'s your name?', 'How are you?', 'Where do you come from?'); $command = new Command('foo'); $command->setCode(function ($input, $output) use($questions, $command) { $io = new SymfonyStyle($input, $output); $io->ask($questions[0]); $io->ask($questions[1]); $io->ask($questions[2]); }); $tester = new CommandTester($command); $tester->setInputs(array('Bobby', 'Fine', 'France')); $tester->execute(array()); $this->assertEquals(0, $tester->getStatusCode()); }
/** * {@inheritdoc} */ protected function execute(InputInterface $input, OutputInterface $output) { $output = new SymfonyStyle($input, $output); if (!extension_loaded('pcntl')) { $output->error(array('This command needs the pcntl extension to run.', 'You can either install it or use the "server:run" command instead to run the built-in web server.')); if ($output->ask('Do you want to execute <info>server:run</info> immediately? [Yn] ', true)) { $command = $this->getApplication()->find('server:run'); return $command->run($input, $output); } return 1; } $documentRoot = $input->getOption('docroot'); if (null === $documentRoot) { $documentRoot = $this->getContainer()->getParameter('kernel.root_dir') . '/../web'; } if (!is_dir($documentRoot)) { $output->error(sprintf('The given document root directory "%s" does not exist.', $documentRoot)); return 1; } $env = $this->getContainer()->getParameter('kernel.environment'); if (false === ($router = $this->determineRouterScript($input->getOption('router'), $env, $output))) { return 1; } $address = $input->getArgument('address'); if (false === strpos($address, ':')) { $address = $address . ':' . $input->getOption('port'); } if (!$input->getOption('force') && $this->isOtherServerProcessRunning($address)) { $output->error(array(sprintf('A process is already listening on http://%s.', $address), 'Use the --force option if the server process terminated unexpectedly to start a new web server process.')); return 1; } if ('prod' === $env) { $output->error('Running PHP built-in server in production environment is NOT recommended!'); } $pid = pcntl_fork(); if ($pid < 0) { $output->error('Unable to start the server process.'); return 1; } if ($pid > 0) { $output->success(sprintf('Web server listening on http://%s', $address)); return; } if (posix_setsid() < 0) { $output->error('Unable to set the child process as session leader'); return 1; } if (null === ($process = $this->createServerProcess($output, $address, $documentRoot, $router))) { return 1; } $process->disableOutput(); $process->start(); $lockFile = $this->getLockFile($address); touch($lockFile); if (!$process->isRunning()) { $output->error('Unable to start the server process'); unlink($lockFile); return 1; } // stop the web server when the lock file is removed while ($process->isRunning()) { if (!file_exists($lockFile)) { $process->stop(); } sleep(1); } }
/** * Database Import * * @param SymfonyStyle $helper * * @return null */ private function actionDatabaseImport(SymfonyStyle $helper) { $timestamp = date('ymdHi'); /* * Select Host */ $remoteHost = $helper->ask('Remote host'); /* * Select external database (default: database_name) */ $remoteDatabase = $helper->ask('Remote database', $this->getContainer()->getParameter('database_name')); /* * Select local database (default: database_name) */ $localDatabase = $helper->ask('New local database', sprintf('%s_%s', $this->getContainer()->getParameter('database_name'), $timestamp)); /* * mysqldump */ $fileName = sprintf('%s_%s_%s.sql', $remoteDatabase, $timestamp, uniqid()); $command = sprintf('ssh %s "mysqldump %s > %s"', $remoteHost, $remoteDatabase, $fileName); $helper->comment($command); $process = new Process($command); $process->setTimeout(600); $process->run(function ($type, $buffer) use($helper) { if (Process::ERR === $type) { $helper->error($buffer); if (false !== strpos($buffer, 'Access denied')) { $helper->note('Missing .my.cnf for mysql access?'); } } else { $helper->comment($buffer); } }); /* * scp */ $command = sprintf('scp %s:%s %s/%s', $remoteHost, $fileName, sys_get_temp_dir(), $fileName); $helper->comment($command); $process = new Process($command); $process->setTimeout(300); $process->run(function ($type, $buffer) use($helper) { if (Process::ERR === $type) { $helper->error($buffer); } else { $helper->comment($buffer); } }); /* * create database database_name_ymdHis * if exists: ask for drop */ $command = sprintf('mysql -e "CREATE DATABASE %s;"', $localDatabase); $helper->comment($command); $process = new Process($command); $process->run(function ($type, $buffer) use($helper) { if (Process::ERR === $type) { $helper->error($buffer); if (false !== strpos($buffer, 'Access denied')) { $helper->note('Missing .my.cnf for mysql access?'); } } else { $helper->comment($buffer); } }); /* * mysql -D database_name_ymdhis < /backup/y-m-d_H-i-s_unique.sql */ $command = sprintf('mysql -D %s < %s/%s', $localDatabase, sys_get_temp_dir(), $fileName); $helper->comment($command); $process = new Process($command); $process->setTimeout(3600); $process->run(function ($type, $buffer) use($helper) { if (Process::ERR === $type) { $helper->error($buffer); } else { $helper->comment($buffer); } }); /** * Remove */ $command = sprintf('ssh %s "rm %s"', $remoteHost, $fileName); $helper->comment($command); $process = new Process($command); $process->run(function ($type, $buffer) use($helper) { if (Process::ERR === $type) { $helper->error($buffer); } else { $helper->comment($buffer); } }); $command = sprintf('%s/%s', sys_get_temp_dir(), $fileName); $helper->comment(sprintf('unlink %s', $command)); unlink($command); /** * Success! New database name: */ $helper->success(['Created new Database', $localDatabase]); return null; }
/** * {@inheritdoc} */ protected function execute(InputInterface $input, OutputInterface $output) { $io = new SymfonyStyle($input, $output); if (!extension_loaded('pcntl')) { $io->error(array('This command needs the pcntl extension to run.', 'You can either install it or use the "mongodb:run" command instead to run the MongoDB server.')); if ($io->ask('Do you want to execute <info>mongodb:run</info> immediately? [Yn] ', true)) { $command = $this->getApplication()->find('mongodb:run'); return $command->run($input, $output); } return 1; } $dbPath = $input->getOption('dbpath'); $fs = new Filesystem(); try { $fs->mkdir('data/mongodb/'); } catch (IOException $e) { $io->warning('Could not create "data/mongodb/" directory'); } // $env = $this->getContainer()->getParameter('kernel.environment'); $env = null; $address = $input->getArgument('address'); if (false === strpos($address, ':')) { $address = $address . ':' . $input->getOption('port'); } if (!$input->getOption('force') && $this->isOtherServerProcessRunning($address)) { $io->error(array(sprintf('A process is already listening on mongodb://%s.', $address), 'Use the --force option if the server process terminated unexpectedly to start a new MongoDB server process.')); return 1; } $pid = pcntl_fork(); if ($pid < 0) { $io->error('Unable to start the server process.'); return 1; } if ($pid > 0) { $io->success(sprintf('MongoDB server listening on mongodb://%s', $address)); return; } if (posix_setsid() < 0) { $io->error('Unable to set the child process as session leader'); return 1; } if (null === ($process = $this->createServerProcess($io, $input->getArgument('address'), $input->getOption('port'), $dbPath))) { return 1; } $process->disableOutput(); $process->start(); $lockFile = $this->getLockFile($address); touch($lockFile); if (!$process->isRunning()) { $io->error('Unable to start the server process'); unlink($lockFile); return 1; } // stop the web server when the lock file is removed while ($process->isRunning()) { if (!file_exists($lockFile)) { $process->stop(); } sleep(1); } }
/** * @codeCoverageIgnore */ public function ask($question, $default = null) { return $this->interactive->ask($question, $default); }
<?php use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Style\SymfonyStyle; //Ensure that questions have the expected outputs return function (InputInterface $input, OutputInterface $output) { $output = new SymfonyStyle($input, $output); $stream = fopen('php://memory', 'r+', false); fputs($stream, "Foo\nBar\nBaz"); rewind($stream); $input->setStream($stream); $output->ask('What\'s your name?'); $output->ask('How are you?'); $output->ask('Where do you come from?'); };