Created
January 19, 2015 20:28
-
-
Save stepanselyuk/c7b3ca03755eb5788ce5 to your computer and use it in GitHub Desktop.
ConsoleAppHelper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Created by PhpStorm. | |
* Author: Stepan Seliuk <[email protected]> | |
* Date: 04/09/14 | |
* Time: 22:22 | |
*/ | |
namespace app\components; | |
use app\components\utils\SystemUtils; | |
use app\components\yiiExt\ConsoleApplication; | |
use app\models\AppSettingsRecord; | |
use yii\base\Exception; | |
/** | |
* Class ConsoleAppHelper | |
* @package app\components | |
*/ | |
class ConsoleAppHelper | |
{ | |
protected static $output; | |
public static function getOutput() | |
{ | |
return preg_replace( '/\x1B\[[0-9;]*[JKmsu]/', '', self::$output ); | |
} | |
public static function runAction( $route, $opts = [ ], $controllerNamespace = null ) | |
{ | |
self::$output = ''; | |
$basePath = \Yii::$app->basePath; | |
$runner = 'console'; | |
if ($controllerNamespace == 'app\commands\system') { | |
$runner = 'console-system'; | |
} elseif ($controllerNamespace == 'app\commands\developer') { | |
$runner = 'console-developer'; | |
} | |
$params = [ build_path( $basePath, $runner ), $route ]; | |
foreach ($opts as $key => $value) { | |
if (!is_string( $key )) { | |
$params[ ] = $value; | |
} else { | |
$params[ ] = '--' . $key . '=' . $value; | |
} | |
} | |
$params = array_map( | |
function ( $v ) { | |
return escapeshellarg( $v ); | |
}, | |
$params | |
); | |
try { | |
$php = AppSettingsRecord::getValue( 'system.phpInterpreter' ); | |
$cmd = $php . ' ' . implode( ' ', $params ); | |
$outputTail = exec( $fullcmd = sprintf( '(%s)2>&1', $cmd ), $outputLines, $returnValue ); | |
self::$output = implode( PHP_EOL, $outputLines ); | |
if (YII_ENV_DEV) { | |
SystemUtils::writeFileInRuntime( $fullcmd ); | |
} | |
if ($returnValue > 0) { | |
throw new Exception( $outputTail ); | |
} | |
if (YII_ENV_DEV) { | |
SystemUtils::writeFileInRuntime( self::getOutput() ); | |
} | |
} catch ( \Exception $e ) { | |
\Yii::warning( ( self::$output = $e->getMessage() . ' in ' . $e->getFile() . ' on line ' . $e->getLine() ), 'console' ); | |
if (YII_ENV_DEV) { | |
SystemUtils::writeFileInRuntime( $e->getMessage() ); | |
} | |
if (!isset( $returnValue )) { | |
$returnValue = 1; | |
} | |
} | |
return $returnValue; | |
} | |
public static function runActionOld( $route, $opts = [ ], $controllerNamespace = null ) | |
{ | |
self::$output = ''; | |
$webApp = \Yii::$app; | |
$webLogger = \Yii::getLogger(); | |
// fcgi doesn't have STDIN, STDOUT, STDERR defined by default | |
defined( 'STDIN' ) or define( 'STDIN', fopen( 'php://stdin', 'r' ) ); | |
// Set php://output for possibility catch output by ob_* functions | |
defined( 'STDOUT' ) or define( 'STDOUT', fopen( 'php://output', 'w' ) ); | |
defined( 'STDERR' ) or define( 'STDERR', fopen( 'php://output', 'w' ) ); | |
/** @noinspection PhpIncludeInspection */ | |
$config = require( \Yii::getAlias( '@app/config/console.php' ) ); | |
// use current connection to DB | |
$config[ 'components' ][ 'db' ] = $webApp->db; | |
$consoleApp = new ConsoleApplication( $config ); | |
if (!is_null( $controllerNamespace )) { | |
$consoleApp->controllerNamespace = $controllerNamespace; | |
} | |
try { | |
ob_start(); | |
$exitCode = $consoleApp->runAction( | |
$route, | |
array_merge( $opts, [ 'interactive' => false, 'color' => false ] ) | |
); | |
$result = ob_get_clean(); | |
self::$output = $result; | |
\Yii::trace( $result, 'console' ); | |
} catch ( \Exception $e ) { | |
\Yii::warning( ( self::$output = $e->getMessage() . ' in ' . $e->getFile() . ' on line ' . $e->getLine() ), 'console' ); | |
$exitCode = 1; | |
} | |
\Yii::$app = $webApp; | |
\Yii::setLogger( $webLogger ); | |
return $exitCode; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment