2017-12-30 00:23:42 +08:00
|
|
|
<?php
|
|
|
|
|
2019-09-17 01:08:26 +08:00
|
|
|
namespace App\Contracts;
|
2017-12-30 00:23:42 +08:00
|
|
|
|
2019-09-13 20:05:02 +08:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2018-03-20 09:50:40 +08:00
|
|
|
use Symfony\Component\Process\Process;
|
2017-12-30 00:23:42 +08:00
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
/**
|
|
|
|
* Class BaseCommand
|
|
|
|
*/
|
|
|
|
abstract class Command extends \Illuminate\Console\Command
|
2017-12-30 00:23:42 +08:00
|
|
|
{
|
2019-09-17 01:08:26 +08:00
|
|
|
/**
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
abstract public function handle();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adjust the logging depending on where we're running from
|
|
|
|
*/
|
2019-09-13 20:05:02 +08:00
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
|
|
|
|
// Running in the console but not in the tests
|
|
|
|
if (app()->runningInConsole() && env('APP_ENV') !== 'testing') {
|
|
|
|
$this->redirectLoggingToFile('stdout');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-17 09:12:56 +08:00
|
|
|
/**
|
2019-08-11 08:42:35 +08:00
|
|
|
* Splice the logger and replace the active handlers with the handlers from the
|
|
|
|
* a stack in config/logging.php
|
2018-03-18 07:05:51 +08:00
|
|
|
*
|
2019-08-11 08:42:35 +08:00
|
|
|
* @param string $channel_name Channel name from config/logging.php
|
2018-03-17 09:12:56 +08:00
|
|
|
*/
|
2019-08-11 08:42:35 +08:00
|
|
|
public function redirectLoggingToFile($channel_name): void
|
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) {
|
2018-03-20 09:50:40 +08:00
|
|
|
$this->error('Error closing handlers: '.$e->getMessage());
|
2018-03-18 07:05:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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-20 09:50:40 +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
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2017-12-30 00:23:42 +08:00
|
|
|
* @param $filename
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2017-12-30 00:23:42 +08:00
|
|
|
* @return \Generator
|
|
|
|
*/
|
2018-03-20 09:50:40 +08:00
|
|
|
public function readFile($filename): ?\Generator
|
2017-12-30 00:23:42 +08:00
|
|
|
{
|
|
|
|
$fp = fopen($filename, 'rb');
|
|
|
|
while (($line = fgets($fp)) !== false) {
|
|
|
|
$line = rtrim($line, "\r\n");
|
|
|
|
if ($line[0] === ';') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
yield $line;
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose($fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-27 00:40:04 +08:00
|
|
|
* @param $cmd
|
|
|
|
* @param bool $return
|
|
|
|
* @param mixed $verbose
|
|
|
|
*
|
2018-03-17 09:12:56 +08:00
|
|
|
* @throws \Symfony\Component\Process\Exception\RuntimeException
|
|
|
|
* @throws \Symfony\Component\Process\Exception\LogicException
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
|
|
|
* @return string
|
2017-12-30 00:23:42 +08:00
|
|
|
*/
|
2018-03-20 09:50:40 +08:00
|
|
|
public function runCommand($cmd, $return = false, $verbose = true): string
|
2017-12-30 00:23:42 +08:00
|
|
|
{
|
2018-01-07 07:00:47 +08:00
|
|
|
if (\is_array($cmd)) {
|
2018-08-27 00:40:04 +08:00
|
|
|
$cmd = implode(' ', $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-03-20 09:50:40 +08:00
|
|
|
$this->info('Running "'.$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
|
|
|
$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
|
|
|
}
|
|
|
|
}
|