Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to execute code synchronously #15

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions src/ConsoleRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ class ConsoleRunner extends Component
*/
public $file;

/**
* @var bool whether it'll run in background
*/
public $runInBackground = true;

/**
* @var string The PHP binary path.
*/
Expand All @@ -68,17 +73,18 @@ public function init()
* Running console command on background.
*
* @param string $cmd Argument that will be passed to console application.
* @param integer $exitCode Return valid exit code only in sync mode ($runInBackground = false).
*
* @return boolean
*/
public function run($cmd)
public function run($cmd, &$exitCode = null)
{
$cmd = "{$this->phpBinaryPath} {$this->file} $cmd";
$cmd = $this->isWindows() === true
? $cmd = "start /b {$cmd}"
: $cmd = "{$cmd} > /dev/null 2>&1 &";
? $cmd = "start ". ($this->runInBackground ? '\b' : '') ." {$cmd}"
: $cmd = "{$cmd} > /dev/null 2>&1" . ($this->runInBackground ? ' &' : '');

pclose(popen($cmd, 'r'));
$exitCode = pclose(popen($cmd, 'r'));

return true;
}
Expand Down