Commit 19e79ad4 by Paul Klimov

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

parent 70ca76d4
...@@ -144,14 +144,14 @@ class AssetController extends Controller ...@@ -144,14 +144,14 @@ class AssetController extends Controller
$bundles = $this->loadBundles($this->bundles); $bundles = $this->loadBundles($this->bundles);
$targets = $this->loadTargets($this->targets, $bundles); $targets = $this->loadTargets($this->targets, $bundles);
foreach ($targets as $name => $target) { foreach ($targets as $name => $target) {
echo "Creating output bundle '{$name}':\n"; $this->stdout("Creating output bundle '{$name}':\n");
if (!empty($target->js)) { if (!empty($target->js)) {
$this->buildTarget($target, 'js', $bundles); $this->buildTarget($target, 'js', $bundles);
} }
if (!empty($target->css)) { if (!empty($target->css)) {
$this->buildTarget($target, 'css', $bundles); $this->buildTarget($target, 'css', $bundles);
} }
echo "\n"; $this->stdout("\n");
} }
$targets = $this->adjustDependency($targets, $bundles); $targets = $this->adjustDependency($targets, $bundles);
...@@ -165,7 +165,7 @@ class AssetController extends Controller ...@@ -165,7 +165,7 @@ class AssetController extends Controller
*/ */
protected function loadConfiguration($configFile) protected function loadConfiguration($configFile)
{ {
echo "Loading configuration from '{$configFile}'...\n"; $this->stdout("Loading configuration from '{$configFile}'...\n");
foreach (require($configFile) as $name => $value) { foreach (require($configFile) as $name => $value) {
if (property_exists($this, $name) || $this->canSetProperty($name)) { if (property_exists($this, $name) || $this->canSetProperty($name)) {
$this->$name = $value; $this->$name = $value;
...@@ -184,7 +184,7 @@ class AssetController extends Controller ...@@ -184,7 +184,7 @@ class AssetController extends Controller
*/ */
protected function loadBundles($bundles) protected function loadBundles($bundles)
{ {
echo "Collecting source bundles information...\n"; $this->stdout("Collecting source bundles information...\n");
$am = $this->getAssetManager(); $am = $this->getAssetManager();
$result = []; $result = [];
...@@ -324,7 +324,7 @@ class AssetController extends Controller ...@@ -324,7 +324,7 @@ class AssetController extends Controller
*/ */
protected function adjustDependency($targets, $bundles) protected function adjustDependency($targets, $bundles)
{ {
echo "Creating new bundle configuration...\n"; $this->stdout("Creating new bundle configuration...\n");
$map = []; $map = [];
foreach ($targets as $name => $target) { foreach ($targets as $name => $target) {
...@@ -423,7 +423,7 @@ EOD; ...@@ -423,7 +423,7 @@ EOD;
if (!file_put_contents($bundleFile, $bundleFileContent)) { if (!file_put_contents($bundleFile, $bundleFileContent)) {
throw new Exception("Unable to write output bundle configuration at '{$bundleFile}'."); throw new Exception("Unable to write output bundle configuration at '{$bundleFile}'.");
} }
echo "Output bundle configuration created at '{$bundleFile}'.\n"; $this->stdout("Output bundle configuration created at '{$bundleFile}'.\n");
} }
/** /**
...@@ -437,14 +437,14 @@ EOD; ...@@ -437,14 +437,14 @@ EOD;
if (empty($inputFiles)) { if (empty($inputFiles)) {
return; return;
} }
echo " Compressing JavaScript files...\n"; $this->stdout(" Compressing JavaScript files...\n");
if (is_string($this->jsCompressor)) { if (is_string($this->jsCompressor)) {
$tmpFile = $outputFile . '.tmp'; $tmpFile = $outputFile . '.tmp';
$this->combineJsFiles($inputFiles, $tmpFile); $this->combineJsFiles($inputFiles, $tmpFile);
echo shell_exec(strtr($this->jsCompressor, [ $this->stdout(shell_exec(strtr($this->jsCompressor, [
'{from}' => escapeshellarg($tmpFile), '{from}' => escapeshellarg($tmpFile),
'{to}' => escapeshellarg($outputFile), '{to}' => escapeshellarg($outputFile),
])); ])));
@unlink($tmpFile); @unlink($tmpFile);
} else { } else {
call_user_func($this->jsCompressor, $this, $inputFiles, $outputFile); call_user_func($this->jsCompressor, $this, $inputFiles, $outputFile);
...@@ -452,7 +452,7 @@ EOD; ...@@ -452,7 +452,7 @@ EOD;
if (!file_exists($outputFile)) { if (!file_exists($outputFile)) {
throw new Exception("Unable to compress JavaScript files into '{$outputFile}'."); throw new Exception("Unable to compress JavaScript files into '{$outputFile}'.");
} }
echo " JavaScript files compressed into '{$outputFile}'.\n"; $this->stdout(" JavaScript files compressed into '{$outputFile}'.\n");
} }
/** /**
...@@ -466,14 +466,14 @@ EOD; ...@@ -466,14 +466,14 @@ EOD;
if (empty($inputFiles)) { if (empty($inputFiles)) {
return; return;
} }
echo " Compressing CSS files...\n"; $this->stdout(" Compressing CSS files...\n");
if (is_string($this->cssCompressor)) { if (is_string($this->cssCompressor)) {
$tmpFile = $outputFile . '.tmp'; $tmpFile = $outputFile . '.tmp';
$this->combineCssFiles($inputFiles, $tmpFile); $this->combineCssFiles($inputFiles, $tmpFile);
echo shell_exec(strtr($this->cssCompressor, [ $this->stdout(shell_exec(strtr($this->cssCompressor, [
'{from}' => escapeshellarg($tmpFile), '{from}' => escapeshellarg($tmpFile),
'{to}' => escapeshellarg($outputFile), '{to}' => escapeshellarg($outputFile),
])); ])));
@unlink($tmpFile); @unlink($tmpFile);
} else { } else {
call_user_func($this->cssCompressor, $this, $inputFiles, $outputFile); call_user_func($this->cssCompressor, $this, $inputFiles, $outputFile);
...@@ -481,7 +481,7 @@ EOD; ...@@ -481,7 +481,7 @@ EOD;
if (!file_exists($outputFile)) { if (!file_exists($outputFile)) {
throw new Exception("Unable to compress CSS files into '{$outputFile}'."); throw new Exception("Unable to compress CSS files into '{$outputFile}'.");
} }
echo " CSS files compressed into '{$outputFile}'.\n"; $this->stdout(" CSS files compressed into '{$outputFile}'.\n");
} }
/** /**
...@@ -656,7 +656,8 @@ EOD; ...@@ -656,7 +656,8 @@ EOD;
if (!file_put_contents($configFile, $template)) { if (!file_put_contents($configFile, $template)) {
throw new Exception("Unable to write template file '{$configFile}'."); throw new Exception("Unable to write template file '{$configFile}'.");
} else { } else {
echo "Configuration file template created at '{$configFile}'.\n\n"; $this->stdout("Configuration file template created at '{$configFile}'.\n\n");
return self::EXIT_CODE_NORMAL;
} }
} }
......
...@@ -62,12 +62,12 @@ class AssetControllerTest extends TestCase ...@@ -62,12 +62,12 @@ class AssetControllerTest extends TestCase
/** /**
* Creates test asset controller instance. * Creates test asset controller instance.
* @return AssetController * @return AssetControllerMock
*/ */
protected function createAssetController() protected function createAssetController()
{ {
$module = $this->getMock('yii\\base\\Module', ['fake'], ['console']); $module = $this->getMock('yii\\base\\Module', ['fake'], ['console']);
$assetController = new AssetController('asset', $module); $assetController = new AssetControllerMock('asset', $module);
$assetController->interactive = false; $assetController->interactive = false;
$assetController->jsCompressor = 'cp {from} {to}'; $assetController->jsCompressor = 'cp {from} {to}';
$assetController->cssCompressor = 'cp {from} {to}'; $assetController->cssCompressor = 'cp {from} {to}';
...@@ -84,11 +84,8 @@ class AssetControllerTest extends TestCase ...@@ -84,11 +84,8 @@ class AssetControllerTest extends TestCase
protected function runAssetControllerAction($actionID, array $args = []) protected function runAssetControllerAction($actionID, array $args = [])
{ {
$controller = $this->createAssetController(); $controller = $this->createAssetController();
ob_start();
ob_implicit_flush(false);
$controller->run($actionID, $args); $controller->run($actionID, $args);
return $controller->flushStdOutBuffer();
return ob_get_clean();
} }
/** /**
...@@ -457,3 +454,11 @@ EOL; ...@@ -457,3 +454,11 @@ EOL;
$this->assertEquals($expectedRealPath, $realPath); $this->assertEquals($expectedRealPath, $realPath);
} }
} }
/**
* Mock class for [[\yii\console\controllers\AssetController]]
*/
class AssetControllerMock extends AssetController
{
use StdOutBufferControllerTrait;
}
\ No newline at end of file
<?php
namespace yiiunit\framework\console\controllers;
/**
* StdOutBufferControllerTrait is a trait, which can be applied to [[yii\console\Controller]],
* allowing to store all output into internal buffer instead of direct sending it to 'stdout'
*/
trait StdOutBufferControllerTrait
{
/**
* @var string output buffer.
*/
private $stdOutBuffer = '';
public function stdout($string)
{
$this->stdOutBuffer .= $string;
}
public function flushStdOutBuffer()
{
$result = $this->stdOutBuffer;
$this->stdOutBuffer = '';
return $result;
}
}
\ 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