You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

114 lines
4.0KB

  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\gii\console;
  8. use yii\helpers\Console;
  9. /**
  10. * @author Qiang Xue <qiang.xue@gmail.com>
  11. * @since 2.0
  12. */
  13. class GenerateAction extends \yii\base\Action
  14. {
  15. /**
  16. * @var \yii\gii\Generator
  17. */
  18. public $generator;
  19. /**
  20. * @var GenerateController
  21. */
  22. public $controller;
  23. /**
  24. * @inheritdoc
  25. */
  26. public function run()
  27. {
  28. echo "Running '{$this->generator->getName()}'...\n\n";
  29. if ($this->generator->validate()) {
  30. $this->generateCode();
  31. } else {
  32. $this->displayValidationErrors();
  33. }
  34. }
  35. protected function displayValidationErrors()
  36. {
  37. $this->controller->stdout("Code not generated. Please fix the following errors:\n\n", Console::FG_RED);
  38. foreach ($this->generator->errors as $attribute => $errors) {
  39. echo ' - ' . $this->controller->ansiFormat($attribute, Console::FG_CYAN) . ': ' . implode('; ', $errors) . "\n";
  40. }
  41. echo "\n";
  42. }
  43. protected function generateCode()
  44. {
  45. $files = $this->generator->generate();
  46. $n = count($files);
  47. if ($n === 0) {
  48. echo "No code to be generated.\n";
  49. return;
  50. }
  51. echo "The following files will be generated:\n";
  52. $skipAll = $this->controller->interactive ? null : !$this->controller->overwrite;
  53. $answers = [];
  54. foreach ($files as $file) {
  55. $path = $file->getRelativePath();
  56. if (is_file($file->path)) {
  57. if (file_get_contents($file->path) === $file->content) {
  58. echo ' ' . $this->controller->ansiFormat('[unchanged]', Console::FG_GREY);
  59. echo $this->controller->ansiFormat(" $path\n", Console::FG_CYAN);
  60. $answers[$file->id] = false;
  61. } else {
  62. echo ' ' . $this->controller->ansiFormat('[changed]', Console::FG_RED);
  63. echo $this->controller->ansiFormat(" $path\n", Console::FG_CYAN);
  64. if ($skipAll !== null) {
  65. $answers[$file->id] = !$skipAll;
  66. } else {
  67. $answer = $this->controller->select("Do you want to overwrite this file?", [
  68. 'y' => 'Overwrite this file.',
  69. 'n' => 'Skip this file.',
  70. 'ya' => 'Overwrite this and the rest of the changed files.',
  71. 'na' => 'Skip this and the rest of the changed files.',
  72. ]);
  73. $answers[$file->id] = $answer === 'y' || $answer === 'ya';
  74. if ($answer === 'ya') {
  75. $skipAll = false;
  76. } elseif ($answer === 'na') {
  77. $skipAll = true;
  78. }
  79. }
  80. }
  81. } else {
  82. echo ' ' . $this->controller->ansiFormat('[new]', Console::FG_GREEN);
  83. echo $this->controller->ansiFormat(" $path\n", Console::FG_CYAN);
  84. $answers[$file->id] = true;
  85. }
  86. }
  87. if (!array_sum($answers)) {
  88. $this->controller->stdout("\nNo files were chosen to be generated.\n", Console::FG_CYAN);
  89. return;
  90. }
  91. if (!$this->controller->confirm("\nReady to generate the selected files?", true)) {
  92. $this->controller->stdout("\nNo file was generated.\n", Console::FG_CYAN);
  93. return;
  94. }
  95. if ($this->generator->save($files, (array) $answers, $results)) {
  96. $this->controller->stdout("\nFiles were generated successfully!\n", Console::FG_GREEN);
  97. } else {
  98. $this->controller->stdout("\nSome errors occurred while generating the files.", Console::FG_RED);
  99. }
  100. echo preg_replace('%<span class="error">(.*?)</span>%', '\1', $results) . "\n";
  101. }
  102. }