2017-12-30 00:23:42 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Console;
|
|
|
|
|
|
|
|
use Illuminate\Console\Command;
|
|
|
|
use Symfony\Component\Process\Process;
|
2018-03-18 07:05:51 +08:00
|
|
|
use Log;
|
2017-12-30 00:23:42 +08:00
|
|
|
|
|
|
|
class BaseCommand extends Command
|
|
|
|
{
|
2018-03-17 09:12:56 +08:00
|
|
|
/**
|
2018-03-18 07:05:51 +08:00
|
|
|
* Splice the logger and replace the active handlers with
|
|
|
|
* the handlers from the "cron" stack in config/logging.php
|
|
|
|
*
|
|
|
|
* Close out any of the existing handlers so we don't leave
|
|
|
|
* file descriptors leaking around
|
|
|
|
*
|
|
|
|
* @param string $channel_name Channel name to grab the handlers from
|
2018-03-17 09:12:56 +08:00
|
|
|
*/
|
2018-03-18 07:05:51 +08:00
|
|
|
public function redirectLoggingToStdout($channel_name)
|
2018-03-17 09:12:56 +08:00
|
|
|
{
|
|
|
|
$logger = app(\Illuminate\Log\Logger::class);
|
|
|
|
|
2018-03-18 07:05:51 +08:00
|
|
|
// Close the existing loggers
|
2018-03-17 09:12:56 +08:00
|
|
|
try {
|
2018-03-18 07:05:51 +08:00
|
|
|
$handlers = $logger->getHandlers();
|
|
|
|
foreach ($handlers as $handler) {
|
|
|
|
$handler->close();
|
|
|
|
}
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
$this->error('Error closing handlers: ' . $e->getMessage());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open the handlers for the channel name,
|
|
|
|
// and then set them to the main logger
|
|
|
|
try {
|
|
|
|
$logger->setHandlers(
|
|
|
|
Log::channel($channel_name)->getHandlers()
|
|
|
|
);
|
2018-03-17 09:12:56 +08:00
|
|
|
} catch (\Exception $e) {
|
2018-03-18 07:05:51 +08:00
|
|
|
$this->error('Couldn\'t splice the logger: ' . $e->getMessage());
|
2018-03-17 09:12:56 +08:00
|
|
|
}
|
|
|
|
}
|
2017-12-30 00:23:42 +08:00
|
|
|
|
|
|
|
/**
|
2018-03-17 09:12:56 +08:00
|
|
|
* Streaming file reader
|
2017-12-30 00:23:42 +08:00
|
|
|
* @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
|
2018-03-17 09:12:56 +08:00
|
|
|
* @throws \Symfony\Component\Process\Exception\RuntimeException
|
|
|
|
* @throws \Symfony\Component\Process\Exception\LogicException
|
2017-12-30 00:23:42 +08:00
|
|
|
*/
|
2018-03-18 07:05:51 +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-03-18 07:05:51 +08:00
|
|
|
if ($verbose) {
|
2018-01-07 07:00:47 +08:00
|
|
|
$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-03-17 09:12:56 +08:00
|
|
|
$process->run(function ($type, $buffer) use ($return, &$val) {
|
2018-01-07 07:00:47 +08:00
|
|
|
if ($return) {
|
|
|
|
$val .= $buffer;
|
2017-12-30 00:23:42 +08:00
|
|
|
} else {
|
|
|
|
echo $buffer;
|
|
|
|
}
|
|
|
|
});
|
2018-01-07 07:00:47 +08:00
|
|
|
|
|
|
|
return $val;
|
2017-12-30 00:23:42 +08:00
|
|
|
}
|
|
|
|
}
|