InitController.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. namespace wdmg\turbo\commands;
  3. use Yii;
  4. use yii\console\Controller;
  5. use yii\console\ExitCode;
  6. use yii\helpers\Console;
  7. class InitController extends Controller
  8. {
  9. /**
  10. * @inheritdoc
  11. */
  12. public $choice = null;
  13. /**
  14. * @inheritdoc
  15. */
  16. public $defaultAction = 'index';
  17. public function options($actionID)
  18. {
  19. return ['choice', 'color', 'interactive', 'help'];
  20. }
  21. public function actionIndex($params = null)
  22. {
  23. $module = Yii::$app->controller->module;
  24. $version = $module->version;
  25. $welcome =
  26. '╔════════════════════════════════════════════════╗'. "\n" .
  27. '║ ║'. "\n" .
  28. '║ YANDEX.TURBO MODULE, v.'.$version.' ║'. "\n" .
  29. '║ by Alexsander Vyshnyvetskyy ║'. "\n" .
  30. '║ (c) 2019-2020 W.D.M.Group, Ukraine ║'. "\n" .
  31. '║ ║'. "\n" .
  32. '╚════════════════════════════════════════════════╝';
  33. echo $name = $this->ansiFormat($welcome . "\n\n", Console::FG_GREEN);
  34. echo "Select the operation you want to perform:\n";
  35. echo " 1) Apply all module migrations\n";
  36. echo " 2) Revert all module migrations\n";
  37. echo " 3) Flush Yandex turbo-pages cache\n";
  38. echo "Your choice: ";
  39. if(!is_null($this->choice))
  40. $selected = $this->choice;
  41. else
  42. $selected = trim(fgets(STDIN));
  43. if ($selected == "1") {
  44. Yii::$app->runAction('migrate/up', ['migrationPath' => '@vendor/wdmg/yii2-turbo/migrations', 'interactive' => true]);
  45. } else if($selected == "2") {
  46. Yii::$app->runAction('migrate/down', ['migrationPath' => '@vendor/wdmg/yii2-turbo/migrations', 'interactive' => true]);
  47. } else if($selected == "3") {
  48. if ($cache = Yii::$app->getCache()) {
  49. if ($cache->delete(md5('yandex-turbo'))) {
  50. echo $this->ansiFormat("OK! Yandex turbo-pages cache successfully cleaned.\n\n", Console::FG_GREEN);
  51. } else {
  52. echo $this->ansiFormat("An error occurred while cleaning a Yandex turbo-pages cache.\n\n", Console::FG_RED);
  53. }
  54. } else {
  55. echo $this->ansiFormat("Error! Cache component not configured in application.\n\n", Console::FG_RED);
  56. }
  57. } else {
  58. echo $this->ansiFormat("Error! Your selection has not been recognized.\n\n", Console::FG_RED);
  59. return ExitCode::UNSPECIFIED_ERROR;
  60. }
  61. echo "\n";
  62. return ExitCode::OK;
  63. }
  64. }