Skip to content

Commit

Permalink
fix: logger aware interface issue
Browse files Browse the repository at this point in the history
  • Loading branch information
alexpozzi committed Jan 7, 2022
1 parent b4db557 commit 2979e00
Showing 1 changed file with 28 additions and 39 deletions.
67 changes: 28 additions & 39 deletions src/Knp/Snappy/AbstractGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@

use Knp\Snappy\Exception\FileAlreadyExistsException;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Psr\Log\LoggerAwareTrait;
use Symfony\Component\Process\Process;
use Exception;
use LogicException;
Expand All @@ -20,6 +19,8 @@
*/
abstract class AbstractGenerator implements GeneratorInterface, LoggerAwareInterface
{
use LoggerAwareTrait;

/**
* @var array
*/
Expand Down Expand Up @@ -55,11 +56,6 @@ abstract class AbstractGenerator implements GeneratorInterface, LoggerAwareInter
*/
private $defaultExtension;

/**
* @var LoggerInterface
*/
private $logger;

/**
* @param null|string $binary
* @param array $options
Expand All @@ -69,7 +65,6 @@ public function __construct($binary, array $options = [], array $env = null)
{
$this->configure();

$this->logger = new NullLogger();
$this->setBinary($binary);
$this->setOptions($options);
$this->env = empty($env) ? null : $env;
Expand All @@ -84,20 +79,6 @@ public function __destruct()
$this->removeTemporaryFiles();
}

/**
* Set the logger to use to log debugging data.
*
* @param LoggerInterface $logger
*
* @return $this
*/
public function setLogger(LoggerInterface $logger)
{
$this->logger = $logger;

return $this;
}

/**
* Sets the default extension.
* Useful when letting Snappy deal with file creation.
Expand Down Expand Up @@ -142,7 +123,9 @@ public function setOption($name, $value)

$this->options[$name] = $value;

$this->logger->debug(\sprintf('Set option "%s".', $name), ['value' => $value]);
if (null !== $this->logger) {
$this->logger->debug(\sprintf('Set option "%s".', $name), ['value' => $value]);
}

return $this;
}
Expand Down Expand Up @@ -198,32 +181,38 @@ public function generate($input, $output, array $options = [], $overwrite = fals

$inputFiles = \is_array($input) ? \implode('", "', $input) : $input;

$this->logger->info(\sprintf('Generate from file(s) "%s" to file "%s".', $inputFiles, $output), [
'command' => $command,
'env' => $this->env,
'timeout' => $this->timeout,
]);
if (null !== $this->logger) {
$this->logger->info(\sprintf('Generate from file(s) "%s" to file "%s".', $inputFiles, $output), [
'command' => $command,
'env' => $this->env,
'timeout' => $this->timeout,
]);
}

try {
list($status, $stdout, $stderr) = $this->executeCommand($command);
$this->checkProcessStatus($status, $stdout, $stderr, $command);
$this->checkOutput($output, $command);
} catch (Exception $e) {
$this->logger->error(\sprintf('An error happened while generating "%s".', $output), [
'command' => $command,
'status' => $status ?? null,
'stdout' => $stdout ?? null,
'stderr' => $stderr ?? null,
]);
if (null !== $this->logger) {
$this->logger->error(\sprintf('An error happened while generating "%s".', $output), [
'command' => $command,
'status' => $status ?? null,
'stdout' => $stdout ?? null,
'stderr' => $stderr ?? null,
]);
}

throw $e;
}

$this->logger->info(\sprintf('File "%s" has been successfully generated.', $output), [
'command' => $command,
'stdout' => $stdout,
'stderr' => $stderr,
]);
if (null !== $this->logger) {
$this->logger->info(\sprintf('File "%s" has been successfully generated.', $output), [
'command' => $command,
'stdout' => $stdout,
'stderr' => $stderr,
]);
}
}

/**
Expand Down

0 comments on commit 2979e00

Please sign in to comment.