2017-12-30 00:23:42 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Console;
|
|
|
|
|
|
|
|
use Illuminate\Console\Command;
|
|
|
|
use Symfony\Component\Process\Process;
|
|
|
|
|
|
|
|
class BaseCommand extends Command
|
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Streaming file read
|
|
|
|
* @param $filename
|
|
|
|
* @return \Generator
|
|
|
|
*/
|
|
|
|
public function readFile($filename)
|
|
|
|
{
|
|
|
|
$fp = fopen($filename, 'rb');
|
|
|
|
|
|
|
|
while (($line = fgets($fp)) !== false) {
|
|
|
|
$line = rtrim($line, "\r\n");
|
|
|
|
if ($line[0] === ';') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
yield $line;
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose($fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $cmd
|
2018-01-07 07:00:47 +08:00
|
|
|
* @param bool $return
|
|
|
|
* @return string
|
2017-12-30 00:23:42 +08:00
|
|
|
*/
|
2018-01-07 07:00:47 +08:00
|
|
|
public function runCommand($cmd, $return=false, $verbose=true)
|
2017-12-30 00:23:42 +08:00
|
|
|
{
|
2018-01-07 07:00:47 +08:00
|
|
|
if (\is_array($cmd)) {
|
2017-12-30 00:23:42 +08:00
|
|
|
$cmd = join(' ', $cmd);
|
2018-01-07 07:00:47 +08:00
|
|
|
}
|
2017-12-30 00:23:42 +08:00
|
|
|
|
2018-01-07 07:00:47 +08:00
|
|
|
if($verbose) {
|
|
|
|
$this->info('Running "' . $cmd . '"');
|
|
|
|
}
|
2017-12-30 00:23:42 +08:00
|
|
|
|
2018-01-07 07:00:47 +08:00
|
|
|
$val = '';
|
2017-12-30 00:23:42 +08:00
|
|
|
$process = new Process($cmd);
|
2018-01-07 07:00:47 +08:00
|
|
|
$process->run(function ($type, $buffer) use ($return, $val) {
|
|
|
|
if ($return) {
|
|
|
|
$val .= $buffer;
|
2017-12-30 00:23:42 +08:00
|
|
|
} else {
|
|
|
|
echo $buffer;
|
|
|
|
}
|
2018-01-07 07:00:47 +08:00
|
|
|
|
|
|
|
/*if (Process::ERR === $type) {
|
|
|
|
echo $buffer;
|
|
|
|
} else {
|
|
|
|
echo $buffer;
|
|
|
|
}*/
|
2017-12-30 00:23:42 +08:00
|
|
|
});
|
2018-01-07 07:00:47 +08:00
|
|
|
|
|
|
|
return $val;
|
2017-12-30 00:23:42 +08:00
|
|
|
}
|
|
|
|
}
|