Commit 86328823 by Paul Klimov

Plain `echo` replaced by `stdout()` at `yii\console\MessageController`

parent 19e79ad4
...@@ -64,7 +64,8 @@ class MessageController extends Controller ...@@ -64,7 +64,8 @@ class MessageController extends Controller
} }
} }
copy(Yii::getAlias('@yii/views/messageConfig.php'), $filePath); copy(Yii::getAlias('@yii/views/messageConfig.php'), $filePath);
echo "Configuration file template created at '{$filePath}'.\n\n"; $this->stdout("Configuration file template created at '{$filePath}'.\n\n");
return self::EXIT_CODE_NORMAL;
} }
/** /**
...@@ -196,7 +197,7 @@ class MessageController extends Controller ...@@ -196,7 +197,7 @@ class MessageController extends Controller
} }
$obsolete = array_keys($obsolete); $obsolete = array_keys($obsolete);
echo "Inserting new messages..."; $this->stdout("Inserting new messages...");
$savedFlag = false; $savedFlag = false;
foreach ($new as $category => $msgs) { foreach ($new as $category => $msgs) {
...@@ -213,16 +214,16 @@ class MessageController extends Controller ...@@ -213,16 +214,16 @@ class MessageController extends Controller
} }
} }
echo $savedFlag ? "saved.\n" : "Nothing new...skipped.\n"; $this->stdout($savedFlag ? "saved.\n" : "Nothing new...skipped.\n");
echo $removeUnused ? "Deleting obsoleted messages..." : "Updating obsoleted messages..."; $this->stdout($removeUnused ? "Deleting obsoleted messages..." : "Updating obsoleted messages...");
if (empty($obsolete)) { if (empty($obsolete)) {
echo "Nothing obsoleted...skipped.\n"; $this->stdout("Nothing obsoleted...skipped.\n");
} else { } else {
if ($removeUnused) { if ($removeUnused) {
$db->createCommand() $db->createCommand()
->delete($sourceMessageTable, ['in', 'id', $obsolete])->execute(); ->delete($sourceMessageTable, ['in', 'id', $obsolete])->execute();
echo "deleted.\n"; $this->stdout("deleted.\n");
} else { } else {
$db->createCommand() $db->createCommand()
->update( ->update(
...@@ -230,7 +231,7 @@ class MessageController extends Controller ...@@ -230,7 +231,7 @@ class MessageController extends Controller
['message' => new \yii\db\Expression("CONCAT('@@',message,'@@')")], ['message' => new \yii\db\Expression("CONCAT('@@',message,'@@')")],
['in', 'id', $obsolete] ['in', 'id', $obsolete]
)->execute(); )->execute();
echo "updated.\n"; $this->stdout("updated.\n");
} }
} }
} }
...@@ -245,7 +246,7 @@ class MessageController extends Controller ...@@ -245,7 +246,7 @@ class MessageController extends Controller
protected function extractMessages($fileName, $translator) protected function extractMessages($fileName, $translator)
{ {
$coloredFileName = Console::ansiFormat($fileName, [Console::FG_CYAN]); $coloredFileName = Console::ansiFormat($fileName, [Console::FG_CYAN]);
echo "Extracting messages from $coloredFileName...\n"; $this->stdout("Extracting messages from $coloredFileName...\n");
$subject = file_get_contents($fileName); $subject = file_get_contents($fileName);
$messages = []; $messages = [];
if (!is_array($translator)) { if (!is_array($translator)) {
...@@ -288,7 +289,7 @@ class MessageController extends Controller ...@@ -288,7 +289,7 @@ class MessageController extends Controller
$line = Console::ansiFormat($this->getLine($buffer), [Console::FG_CYAN]); $line = Console::ansiFormat($this->getLine($buffer), [Console::FG_CYAN]);
$skipping = Console::ansiFormat('Skipping line', [Console::FG_YELLOW]); $skipping = Console::ansiFormat('Skipping line', [Console::FG_YELLOW]);
echo "$skipping $line. Make sure both category and message are static strings.\n"; $this->stdout("$skipping $line. Make sure both category and message are static strings.\n");
} }
// prepare for the next match // prepare for the next match
...@@ -302,7 +303,7 @@ class MessageController extends Controller ...@@ -302,7 +303,7 @@ class MessageController extends Controller
} }
} }
echo "\n"; $this->stdout("\n");
return $messages; return $messages;
} }
...@@ -359,7 +360,7 @@ class MessageController extends Controller ...@@ -359,7 +360,7 @@ class MessageController extends Controller
FileHelper::createDirectory($path); FileHelper::createDirectory($path);
$msgs = array_values(array_unique($msgs)); $msgs = array_values(array_unique($msgs));
$coloredFileName = Console::ansiFormat($file, [Console::FG_CYAN]); $coloredFileName = Console::ansiFormat($file, [Console::FG_CYAN]);
echo "Saving messages to $coloredFileName...\n"; $this->stdout("Saving messages to $coloredFileName...\n");
$this->saveMessagesCategoryToPHP($msgs, $file, $overwrite, $removeUnused, $sort, $category); $this->saveMessagesCategoryToPHP($msgs, $file, $overwrite, $removeUnused, $sort, $category);
} }
} }
...@@ -381,7 +382,7 @@ class MessageController extends Controller ...@@ -381,7 +382,7 @@ class MessageController extends Controller
sort($messages); sort($messages);
ksort($existingMessages); ksort($existingMessages);
if (array_keys($existingMessages) == $messages) { if (array_keys($existingMessages) == $messages) {
echo "Nothing new in \"$category\" category... Nothing to save.\n\n"; $this->stdout("Nothing new in \"$category\" category... Nothing to save.\n\n");
return; return;
} }
$merged = []; $merged = [];
...@@ -416,7 +417,7 @@ class MessageController extends Controller ...@@ -416,7 +417,7 @@ class MessageController extends Controller
if (false === $overwrite) { if (false === $overwrite) {
$fileName .= '.merged'; $fileName .= '.merged';
} }
echo "Translation merged.\n"; $this->stdout("Translation merged.\n");
} else { } else {
$merged = []; $merged = [];
foreach ($messages as $message) { foreach ($messages as $message) {
...@@ -451,7 +452,7 @@ return $array; ...@@ -451,7 +452,7 @@ return $array;
EOD; EOD;
file_put_contents($fileName, $content); file_put_contents($fileName, $content);
echo "Saved.\n\n"; $this->stdout("Saved.\n\n");
} }
/** /**
...@@ -468,7 +469,7 @@ EOD; ...@@ -468,7 +469,7 @@ EOD;
{ {
$file = str_replace("\\", '/', "$dirName/$catalog.po"); $file = str_replace("\\", '/', "$dirName/$catalog.po");
FileHelper::createDirectory(dirname($file)); FileHelper::createDirectory(dirname($file));
echo "Saving messages to $file...\n"; $this->stdout("Saving messages to $file...\n");
$poFile = new GettextPoFile(); $poFile = new GettextPoFile();
...@@ -487,7 +488,7 @@ EOD; ...@@ -487,7 +488,7 @@ EOD;
sort($msgs); sort($msgs);
ksort($existingMessages); ksort($existingMessages);
if (array_keys($existingMessages) == $msgs) { if (array_keys($existingMessages) == $msgs) {
echo "Nothing new in \"$category\" category...\n"; $this->stdout("Nothing new in \"$category\" category...\n");
sort($msgs); sort($msgs);
foreach ($msgs as $message) { foreach ($msgs as $message) {
...@@ -539,14 +540,14 @@ EOD; ...@@ -539,14 +540,14 @@ EOD;
} }
ksort($merged); ksort($merged);
} }
echo "Category \"$category\" merged.\n"; $this->stdout("Category \"$category\" merged.\n");
$hasSomethingToWrite = true; $hasSomethingToWrite = true;
} }
if ($hasSomethingToWrite) { if ($hasSomethingToWrite) {
$poFile->save($file, $merged); $poFile->save($file, $merged);
echo "Saved.\n"; $this->stdout("Saved.\n");
} else { } else {
echo "Nothing to save.\n"; $this->stdout("Nothing to save.\n");
} }
} }
} }
...@@ -38,12 +38,12 @@ abstract class BaseMessageControllerTest extends TestCase ...@@ -38,12 +38,12 @@ abstract class BaseMessageControllerTest extends TestCase
/** /**
* Creates test message controller instance. * Creates test message controller instance.
* @return MessageController message command instance. * @return MessageControllerMock message command instance.
*/ */
protected function createMessageController() protected function createMessageController()
{ {
$module = $this->getMock('yii\\base\\Module', ['fake'], ['console']); $module = $this->getMock('yii\\base\\Module', ['fake'], ['console']);
$messageController = new MessageController('message', $module); $messageController = new MessageControllerMock('message', $module);
$messageController->interactive = false; $messageController->interactive = false;
return $messageController; return $messageController;
...@@ -58,11 +58,8 @@ abstract class BaseMessageControllerTest extends TestCase ...@@ -58,11 +58,8 @@ abstract class BaseMessageControllerTest extends TestCase
protected function runMessageControllerAction($actionID, array $args = []) protected function runMessageControllerAction($actionID, array $args = [])
{ {
$controller = $this->createMessageController(); $controller = $this->createMessageController();
ob_start();
ob_implicit_flush(false);
$controller->run($actionID, $args); $controller->run($actionID, $args);
return $controller->flushStdOutBuffer();
return ob_get_clean();
} }
/** /**
...@@ -344,3 +341,8 @@ abstract class BaseMessageControllerTest extends TestCase ...@@ -344,3 +341,8 @@ abstract class BaseMessageControllerTest extends TestCase
$this->assertArrayNotHasKey($message2, $messages2, "message2 found in category2. Command output:\n\n" . $out); $this->assertArrayNotHasKey($message2, $messages2, "message2 found in category2. Command output:\n\n" . $out);
} }
} }
class MessageControllerMock extends MessageController
{
use StdOutBufferControllerTrait;
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment