12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- <?php
- namespace wdmg\turbo\commands;
- use Yii;
- use yii\console\Controller;
- use yii\console\ExitCode;
- use yii\helpers\Console;
- class InitController extends Controller
- {
- /**
- * @inheritdoc
- */
- public $choice = null;
- /**
- * @inheritdoc
- */
- public $defaultAction = 'index';
- public function options($actionID)
- {
- return ['choice', 'color', 'interactive', 'help'];
- }
- public function actionIndex($params = null)
- {
- $module = Yii::$app->controller->module;
- $version = $module->version;
- $welcome =
- '╔════════════════════════════════════════════════╗'. "\n" .
- '║ ║'. "\n" .
- '║ YANDEX.TURBO MODULE, v.'.$version.' ║'. "\n" .
- '║ by Alexsander Vyshnyvetskyy ║'. "\n" .
- '║ (c) 2019-2020 W.D.M.Group, Ukraine ║'. "\n" .
- '║ ║'. "\n" .
- '╚════════════════════════════════════════════════╝';
- echo $name = $this->ansiFormat($welcome . "\n\n", Console::FG_GREEN);
- echo "Select the operation you want to perform:\n";
- echo " 1) Apply all module migrations\n";
- echo " 2) Revert all module migrations\n";
- echo " 3) Flush Yandex turbo-pages cache\n";
- echo "Your choice: ";
- if(!is_null($this->choice))
- $selected = $this->choice;
- else
- $selected = trim(fgets(STDIN));
- if ($selected == "1") {
- Yii::$app->runAction('migrate/up', ['migrationPath' => '@vendor/wdmg/yii2-turbo/migrations', 'interactive' => true]);
- } else if($selected == "2") {
- Yii::$app->runAction('migrate/down', ['migrationPath' => '@vendor/wdmg/yii2-turbo/migrations', 'interactive' => true]);
- } else if($selected == "3") {
- if ($cache = Yii::$app->getCache()) {
- if ($cache->delete(md5('yandex-turbo'))) {
- echo $this->ansiFormat("OK! Yandex turbo-pages cache successfully cleaned.\n\n", Console::FG_GREEN);
- } else {
- echo $this->ansiFormat("An error occurred while cleaning a Yandex turbo-pages cache.\n\n", Console::FG_RED);
- }
- } else {
- echo $this->ansiFormat("Error! Cache component not configured in application.\n\n", Console::FG_RED);
- }
- } else {
- echo $this->ansiFormat("Error! Your selection has not been recognized.\n\n", Console::FG_RED);
- return ExitCode::UNSPECIFIED_ERROR;
- }
- echo "\n";
- return ExitCode::OK;
- }
- }
|