parent
cac7055c9d
commit
94105d829a
@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
namespace jrosset\CliProgram;
|
||||
|
||||
use jrosset\CliProgram\Monolog\ConsoleOutputWithMonolog;
|
||||
use jrosset\ExtendedMonolog\ExceptionLogger;
|
||||
use jrosset\ExtendedMonolog\LogDirectoryHandler;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
/**
|
||||
* An application with a {@see LogDirectoryHandler Monolog log directory} for each command
|
||||
*/
|
||||
class ApplicationWithCommandMonolog extends ApplicationWithCommandOutputInterface {
|
||||
/**
|
||||
* @var string The main log directory for Monolog: on subdirectory by command
|
||||
*/
|
||||
private string $logMainDirectory;
|
||||
|
||||
/**
|
||||
* Initialization
|
||||
*
|
||||
* @param string $logMainDirectory The main log directory for Monolog: on subdirectory by command
|
||||
* @param string $name The application name
|
||||
* @param string $version The application version
|
||||
*/
|
||||
public function __construct (string $logMainDirectory, string $name = 'UNKNOWN', string $version = 'UNKNOWN') {
|
||||
parent::__construct($name, $version);
|
||||
$this->setLogMainDirectory($logMainDirectory);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
protected function getOutputInterfaceForCommand (Command $command, InputInterface $input, OutputInterface $output): OutputInterface {
|
||||
return new ConsoleOutputWithMonolog(
|
||||
new ExceptionLogger(
|
||||
$command->getName(),
|
||||
[
|
||||
new LogDirectoryHandler(
|
||||
$this->getLogMainDirectory() . DIRECTORY_SEPARATOR
|
||||
. str_replace(':', DIRECTORY_SEPARATOR, $command->getName())
|
||||
),
|
||||
]
|
||||
),
|
||||
$output->getVerbosity(),
|
||||
$output->isDecorated(),
|
||||
$output->getFormatter()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* The main log directory for Monolog: on subdirectory by command
|
||||
*
|
||||
* @return string The main log directory for Monolog: on subdirectory by command
|
||||
*/
|
||||
public function getLogMainDirectory (): string {
|
||||
return $this->logMainDirectory;
|
||||
}
|
||||
/**
|
||||
* Set the main log directory for Monolog: on subdirectory by command
|
||||
*
|
||||
* @param string $logMainDirectory The main log directory for Monolog: on subdirectory by command
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setLogMainDirectory (string $logMainDirectory): self {
|
||||
$this->logMainDirectory = $logMainDirectory;
|
||||
return $this;
|
||||
}
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace jrosset\CliProgram;
|
||||
|
||||
use Symfony\Component\Console\Application;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* An application with a by-command {@see OutputInterface}
|
||||
*/
|
||||
class ApplicationWithCommandOutputInterface extends Application {
|
||||
/**
|
||||
* Get the {@see OutputInterface} for a command
|
||||
*
|
||||
* @param Command $command The command
|
||||
* @param InputInterface $input The input
|
||||
* @param OutputInterface $output The existing output
|
||||
*
|
||||
* @return OutputInterface The output for the command
|
||||
*
|
||||
* @throws Throwable On error
|
||||
*
|
||||
* @noinspection PhpUnusedParameterInspection
|
||||
*/
|
||||
protected function getOutputInterfaceForCommand (Command $command, InputInterface $input, OutputInterface $output): OutputInterface {
|
||||
return $output;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
* @throws Throwable
|
||||
*/
|
||||
protected function doRunCommand (Command $command, InputInterface $input, OutputInterface $output): int {
|
||||
return parent::doRunCommand($command, $input, $this->getOutputInterfaceForCommand($command, $input, $output));
|
||||
}
|
||||
}
|
@ -1 +0,0 @@
|
||||
run.log
|
@ -0,0 +1,2 @@
|
||||
*
|
||||
!.gitignore
|
Loading…
Reference in New Issue