phpvms/app/Console/Command.php
Nabeel S 23eb9dcbda
384 Laravel 6 changes (#385)
* 384 Laravel 6 changes

* Library versions

* Update package versions

* Add keyType to models

* Remove unused dependencies

* StyleCI fixes

* Fix models for test

* Fix tests output and update test runner

* Unused imports

* Update exceptions handler

* Fix login page
2019-09-13 08:05:02 -04:00

114 lines
2.7 KiB
PHP

<?php
namespace App\Console;
use Illuminate\Support\Facades\Log;
use Symfony\Component\Process\Process;
/**
* Class BaseCommand
*/
abstract class Command extends \Illuminate\Console\Command
{
public function __construct()
{
parent::__construct();
// Running in the console but not in the tests
if (app()->runningInConsole() && env('APP_ENV') !== 'testing') {
$this->redirectLoggingToFile('stdout');
}
}
/**
* @return mixed
*/
abstract public function handle();
/**
* Splice the logger and replace the active handlers with the handlers from the
* a stack in config/logging.php
*
* @param string $channel_name Channel name from config/logging.php
*/
public function redirectLoggingToFile($channel_name): void
{
$logger = app(\Illuminate\Log\Logger::class);
// Close the existing loggers
try {
$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()
);
} catch (\Exception $e) {
$this->error('Couldn\'t splice the logger: '.$e->getMessage());
}
}
/**
* Streaming file reader
*
* @param $filename
*
* @return \Generator
*/
public function readFile($filename): ?\Generator
{
$fp = fopen($filename, 'rb');
while (($line = fgets($fp)) !== false) {
$line = rtrim($line, "\r\n");
if ($line[0] === ';') {
continue;
}
yield $line;
}
fclose($fp);
}
/**
* @param $cmd
* @param bool $return
* @param mixed $verbose
*
* @throws \Symfony\Component\Process\Exception\RuntimeException
* @throws \Symfony\Component\Process\Exception\LogicException
*
* @return string
*/
public function runCommand($cmd, $return = false, $verbose = true): string
{
if (\is_array($cmd)) {
$cmd = implode(' ', $cmd);
}
if ($verbose) {
$this->info('Running "'.$cmd.'"');
}
$val = '';
$process = new Process($cmd);
$process->run(function ($type, $buffer) use ($return, &$val) {
if ($return) {
$val .= $buffer;
} else {
echo $buffer;
}
});
return $val;
}
}