Просмотр файла vendor/robmorgan/phinx/src/Phinx/Console/Command/Init.php

Размер файла: 5.08Kb
  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 InvalidArgumentException;
  11. use RuntimeException;
  12. use Symfony\Component\Console\Command\Command;
  13. use Symfony\Component\Console\Input\InputArgument;
  14. use Symfony\Component\Console\Input\InputInterface;
  15. use Symfony\Component\Console\Output\OutputInterface;
  16.  
  17. class Init extends Command
  18. {
  19. protected const FILE_NAME = 'phinx';
  20.  
  21. /**
  22. * @var string[]
  23. */
  24. protected static $supportedFormats = [
  25. AbstractCommand::FORMAT_JSON,
  26. AbstractCommand::FORMAT_YML_ALIAS,
  27. AbstractCommand::FORMAT_YML,
  28. AbstractCommand::FORMAT_PHP,
  29. ];
  30.  
  31. /**
  32. * @var string
  33. */
  34. protected static $defaultName = 'init';
  35.  
  36. /**
  37. * {@inheritDoc}
  38. *
  39. * @return void
  40. */
  41. protected function configure()
  42. {
  43. $this->setDescription('Initialize the application for Phinx')
  44. ->addOption(
  45. '--format',
  46. '-f',
  47. InputArgument::OPTIONAL,
  48. 'What format should we use to initialize?',
  49. AbstractCommand::FORMAT_DEFAULT
  50. )
  51. ->addArgument('path', InputArgument::OPTIONAL, 'Which path should we initialize for Phinx?')
  52. ->setHelp(sprintf(
  53. '%sInitializes the application for Phinx%s',
  54. PHP_EOL,
  55. PHP_EOL
  56. ));
  57. }
  58.  
  59. /**
  60. * Initializes the application.
  61. *
  62. * @param \Symfony\Component\Console\Input\InputInterface $input Interface implemented by all input classes.
  63. * @param \Symfony\Component\Console\Output\OutputInterface $output Interface implemented by all output classes.
  64. *
  65. * @return int 0 on success
  66. */
  67. protected function execute(InputInterface $input, OutputInterface $output)
  68. {
  69. $format = strtolower($input->getOption('format'));
  70. $path = $this->resolvePath($input, $format);
  71. $this->writeConfig($path, $format);
  72.  
  73. $output->writeln("<info>created</info> {$path}");
  74.  
  75. return AbstractCommand::CODE_SUCCESS;
  76. }
  77.  
  78. /**
  79. * Return valid $path for Phinx's config file.
  80. *
  81. * @param \Symfony\Component\Console\Input\InputInterface $input Interface implemented by all input classes.
  82. * @param string $format Format to resolve for
  83. *
  84. * @throws \InvalidArgumentException
  85. *
  86. * @return string
  87. */
  88. protected function resolvePath(InputInterface $input, $format)
  89. {
  90. // get the migration path from the config
  91. $path = (string)$input->getArgument('path');
  92.  
  93. if (!in_array($format, static::$supportedFormats, true)) {
  94. throw new InvalidArgumentException(sprintf(
  95. 'Invalid format "%s". Format must be either ' . implode(', ', static::$supportedFormats) . '.',
  96. $format
  97. ));
  98. }
  99.  
  100. // Fallback
  101. if (!$path) {
  102. $path = getcwd() . DIRECTORY_SEPARATOR . self::FILE_NAME . '.' . $format;
  103. }
  104.  
  105. // Adding file name if necessary
  106. if (is_dir($path)) {
  107. $path .= DIRECTORY_SEPARATOR . self::FILE_NAME . '.' . $format;
  108. }
  109.  
  110. // Check if path is available
  111. $dirname = dirname($path);
  112. if (is_dir($dirname) && !is_file($path)) {
  113. return $path;
  114. }
  115.  
  116. // Path is valid, but file already exists
  117. if (is_file($path)) {
  118. throw new InvalidArgumentException(sprintf(
  119. 'Config file "%s" already exists.',
  120. $path
  121. ));
  122. }
  123.  
  124. // Dir is invalid
  125. throw new InvalidArgumentException(sprintf(
  126. 'Invalid path "%s" for config file.',
  127. $path
  128. ));
  129. }
  130.  
  131. /**
  132. * Writes Phinx's config in provided $path
  133. *
  134. * @param string $path Config file's path.
  135. * @param string $format Format to use for config file
  136. *
  137. * @throws \InvalidArgumentException
  138. * @throws \RuntimeException
  139. *
  140. * @return void
  141. */
  142. protected function writeConfig($path, $format = AbstractCommand::FORMAT_DEFAULT)
  143. {
  144. // Check if dir is writable
  145. $dirname = dirname($path);
  146. if (!is_writable($dirname)) {
  147. throw new InvalidArgumentException(sprintf(
  148. 'The directory "%s" is not writable',
  149. $dirname
  150. ));
  151. }
  152.  
  153. if ($format === AbstractCommand::FORMAT_YML_ALIAS) {
  154. $format = AbstractCommand::FORMAT_YML;
  155. }
  156.  
  157. // load the config template
  158. if (is_dir(__DIR__ . '/../../../../data')) {
  159. $contents = file_get_contents(__DIR__ . '/../../../../data/' . self::FILE_NAME . '.' . $format . '.dist');
  160. } else {
  161. throw new RuntimeException(sprintf(
  162. 'Could not find template for format "%s".',
  163. $format
  164. ));
  165. }
  166.  
  167. if (file_put_contents($path, $contents) === false) {
  168. throw new RuntimeException(sprintf(
  169. 'The file "%s" could not be written to',
  170. $path
  171. ));
  172. }
  173. }
  174. }