View file vendor/robmorgan/phinx/src/Phinx/Console/Command/SeedRun.php

File size: 3.82Kb
  1. <?php
  2.  
  3. /**
  4. * MIT License
  5. * For full license information, please view the LICENSE file that was distributed with this source code.
  6. */
  7.  
  8. namespace Phinx\Console\Command;
  9.  
  10. use Symfony\Component\Console\Input\InputInterface;
  11. use Symfony\Component\Console\Input\InputOption;
  12. use Symfony\Component\Console\Output\OutputInterface;
  13.  
  14. class SeedRun extends AbstractCommand
  15. {
  16. /**
  17. * @var string
  18. */
  19. protected static $defaultName = 'seed:run';
  20.  
  21. /**
  22. * {@inheritDoc}
  23. *
  24. * @return void
  25. */
  26. protected function configure()
  27. {
  28. parent::configure();
  29.  
  30. $this->addOption('--environment', '-e', InputOption::VALUE_REQUIRED, 'The target environment');
  31.  
  32. $this->setDescription('Run database seeders')
  33. ->addOption('--seed', '-s', InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'What is the name of the seeder?')
  34. ->setHelp(
  35. <<<EOT
  36. The <info>seed:run</info> command runs all available or individual seeders
  37.  
  38. <info>phinx seed:run -e development</info>
  39. <info>phinx seed:run -e development -s UserSeeder</info>
  40. <info>phinx seed:run -e development -s UserSeeder -s PermissionSeeder -s LogSeeder</info>
  41. <info>phinx seed:run -e development -v</info>
  42.  
  43. EOT
  44. );
  45. }
  46.  
  47. /**
  48. * Run database seeders.
  49. *
  50. * @param \Symfony\Component\Console\Input\InputInterface $input Input
  51. * @param \Symfony\Component\Console\Output\OutputInterface $output Output
  52. *
  53. * @return int integer 0 on success, or an error code.
  54. */
  55. protected function execute(InputInterface $input, OutputInterface $output)
  56. {
  57. $this->bootstrap($input, $output);
  58.  
  59. $seedSet = $input->getOption('seed');
  60. $environment = $input->getOption('environment');
  61.  
  62. if ($environment === null) {
  63. $environment = $this->getConfig()->getDefaultEnvironment();
  64. $output->writeln('<comment>warning</comment> no environment specified, defaulting to: ' . $environment);
  65. } else {
  66. $output->writeln('<info>using environment</info> ' . $environment);
  67. }
  68.  
  69. if (!$this->getConfig()->hasEnvironment($environment)) {
  70. $output->writeln(sprintf('<error>The environment "%s" does not exist</error>', $environment));
  71.  
  72. return self::CODE_ERROR;
  73. }
  74.  
  75. $envOptions = $this->getConfig()->getEnvironment($environment);
  76. if (isset($envOptions['adapter'])) {
  77. $output->writeln('<info>using adapter</info> ' . $envOptions['adapter']);
  78. }
  79.  
  80. if (isset($envOptions['wrapper'])) {
  81. $output->writeln('<info>using wrapper</info> ' . $envOptions['wrapper']);
  82. }
  83.  
  84. if (isset($envOptions['name'])) {
  85. $output->writeln('<info>using database</info> ' . $envOptions['name']);
  86. } else {
  87. $output->writeln('<error>Could not determine database name! Please specify a database name in your config file.</error>');
  88.  
  89. return self::CODE_ERROR;
  90. }
  91.  
  92. if (isset($envOptions['table_prefix'])) {
  93. $output->writeln('<info>using table prefix</info> ' . $envOptions['table_prefix']);
  94. }
  95. if (isset($envOptions['table_suffix'])) {
  96. $output->writeln('<info>using table suffix</info> ' . $envOptions['table_suffix']);
  97. }
  98.  
  99. $start = microtime(true);
  100.  
  101. if (empty($seedSet)) {
  102. // run all the seed(ers)
  103. $this->getManager()->seed($environment);
  104. } else {
  105. // run seed(ers) specified in a comma-separated list of classes
  106. foreach ($seedSet as $seed) {
  107. $this->getManager()->seed($environment, trim($seed));
  108. }
  109. }
  110.  
  111. $end = microtime(true);
  112.  
  113. $output->writeln('');
  114. $output->writeln('<comment>All Done. Took ' . sprintf('%.4fs', $end - $start) . '</comment>');
  115.  
  116. return self::CODE_SUCCESS;
  117. }
  118. }