[BC] Upgraded dependencies, dropped support for anything below PHP 8.0. (#849)
* GitHub actions + style fixes + updated packages * Fixed workflows dir * Support for PHP 8.1 (#1) * Update README.md * Revert some changes from upstream
This commit is contained in:
parent
72c946dc7d
commit
111c153428
335 changed files with 4394 additions and 28116 deletions
107
src/Alchemy/BinaryDriver/ProcessRunner.php
Normal file
107
src/Alchemy/BinaryDriver/ProcessRunner.php
Normal file
|
|
@ -0,0 +1,107 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Alchemy\BinaryDriver.
|
||||
*
|
||||
* (c) Alchemy <info@alchemy.fr>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Alchemy\BinaryDriver;
|
||||
|
||||
use Alchemy\BinaryDriver\Exception\ExecutionFailureException;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use SplObjectStorage;
|
||||
use Symfony\Component\Process\Exception\RuntimeException;
|
||||
use Symfony\Component\Process\Process;
|
||||
|
||||
class ProcessRunner implements ProcessRunnerInterface
|
||||
{
|
||||
/** @var LoggerInterface */
|
||||
private $logger;
|
||||
|
||||
/** @var string */
|
||||
private $name;
|
||||
|
||||
public function __construct(LoggerInterface $logger, $name)
|
||||
{
|
||||
$this->logger = $logger;
|
||||
$this->name = $name;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function setLogger(LoggerInterface $logger): void
|
||||
{
|
||||
$this->logger = $logger;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return LoggerInterface
|
||||
*/
|
||||
public function getLogger()
|
||||
{
|
||||
return $this->logger;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function run(Process $process, SplObjectStorage $listeners, $bypassErrors)
|
||||
{
|
||||
$this->logger->info(sprintf(
|
||||
'%s running command %s',
|
||||
$this->name,
|
||||
$process->getCommandLine()
|
||||
));
|
||||
|
||||
try {
|
||||
$process->run($this->buildCallback($listeners));
|
||||
} catch (RuntimeException $e) {
|
||||
if (!$bypassErrors) {
|
||||
$this->doExecutionFailure($process->getCommandLine(), $process->getErrorOutput(), $e);
|
||||
}
|
||||
}
|
||||
|
||||
if (!$bypassErrors && !$process->isSuccessful()) {
|
||||
$this->doExecutionFailure($process->getCommandLine(), $process->getErrorOutput());
|
||||
} elseif (!$process->isSuccessful()) {
|
||||
$this->logger->error($this->createErrorMessage($process->getCommandLine(), $process->getErrorOutput()));
|
||||
return;
|
||||
} else {
|
||||
$this->logger->info(sprintf('%s executed command successfully', $this->name));
|
||||
return $process->getOutput();
|
||||
}
|
||||
}
|
||||
|
||||
private function buildCallback(SplObjectStorage $listeners)
|
||||
{
|
||||
return function ($type, $data) use ($listeners) {
|
||||
foreach ($listeners as $listener) {
|
||||
$listener->handle($type, $data);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private function doExecutionFailure($command, $errorOutput, \Exception $e = null)
|
||||
{
|
||||
$this->logger->error($this->createErrorMessage($command, $errorOutput));
|
||||
throw new ExecutionFailureException(
|
||||
$this->name,
|
||||
$command,
|
||||
$errorOutput,
|
||||
$e ? $e->getCode() : 0,
|
||||
$e ?: null
|
||||
);
|
||||
}
|
||||
|
||||
private function createErrorMessage($command, $errorOutput)
|
||||
{
|
||||
return sprintf('%s failed to execute command %s: %s', $this->name, $command, $errorOutput);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue