Log composer command outputs (Fixes #5)

This commit is contained in:
SychO9 2021-09-29 13:00:36 +01:00
parent d878eeb92a
commit 1b65c6862e
8 changed files with 101 additions and 17 deletions

View File

@ -11,6 +11,7 @@ use Composer\Console\Application;
use Flarum\Settings\SettingsRepositoryInterface;
use SychO\PackageManager\Exception\ComposerCommandFailedException;
use SychO\PackageManager\LastUpdateCheck;
use SychO\PackageManager\OutputLogger;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\BufferedOutput;
@ -27,13 +28,15 @@ class CheckForUpdatesHandler
protected $lastUpdateCheck;
/**
* @param Application $composer
* @param LastUpdateCheck $lastUpdateCheck
* @var OutputLogger
*/
public function __construct(Application $composer, LastUpdateCheck $lastUpdateCheck)
protected $logger;
public function __construct(Application $composer, LastUpdateCheck $lastUpdateCheck, OutputLogger $logger)
{
$this->composer = $composer;
$this->lastUpdateCheck = $lastUpdateCheck;
$this->logger = $logger;
}
/**
@ -55,6 +58,8 @@ class CheckForUpdatesHandler
$exitCode = $this->composer->run($input, $output);
$this->logger->log($output->fetch(), $exitCode);
if ($exitCode !== 0) {
throw new ComposerCommandFailedException('', $output->fetch());
}

View File

@ -11,6 +11,7 @@ use Flarum\Bus\Dispatcher as FlarumDispatcher;
use Illuminate\Contracts\Events\Dispatcher;
use SychO\PackageManager\Event\FlarumUpdated;
use SychO\PackageManager\Exception\ComposerUpdateFailedException;
use SychO\PackageManager\OutputLogger;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\BufferedOutput;
@ -32,15 +33,16 @@ class GlobalUpdateHandler
protected $commandDispatcher;
/**
* @param Application $composer
* @param Dispatcher $events
* @param FlarumDispatcher $commandDispatcher
* @var OutputLogger
*/
public function __construct(Application $composer, Dispatcher $events, FlarumDispatcher $commandDispatcher)
protected $logger;
public function __construct(Application $composer, Dispatcher $events, FlarumDispatcher $commandDispatcher, OutputLogger $logger)
{
$this->composer = $composer;
$this->events = $events;
$this->commandDispatcher = $commandDispatcher;
$this->logger = $logger;
}
/**
@ -61,6 +63,8 @@ class GlobalUpdateHandler
$exitCode = $this->composer->run($input, $output);
$this->logger->log($output->fetch(), $exitCode);
if ($exitCode !== 0) {
throw new ComposerUpdateFailedException('*', $output->fetch());
}

View File

@ -11,6 +11,7 @@ use Illuminate\Contracts\Events\Dispatcher;
use SychO\PackageManager\Event\FlarumUpdated;
use SychO\PackageManager\Exception\ComposerUpdateFailedException;
use SychO\PackageManager\LastUpdateCheck;
use SychO\PackageManager\OutputLogger;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\BufferedOutput;
@ -32,15 +33,16 @@ class MinorFlarumUpdateHandler
protected $events;
/**
* @param Application $composer
* @param LastUpdateCheck $lastUpdateCheck
* @param Dispatcher $events
* @var OutputLogger
*/
public function __construct(Application $composer, LastUpdateCheck $lastUpdateCheck, Dispatcher $events)
protected $logger;
public function __construct(Application $composer, LastUpdateCheck $lastUpdateCheck, Dispatcher $events, OutputLogger $logger)
{
$this->composer = $composer;
$this->lastUpdateCheck = $lastUpdateCheck;
$this->events = $events;
$this->logger = $logger;
}
/**
@ -63,6 +65,8 @@ class MinorFlarumUpdateHandler
$exitCode = $this->composer->run($input, $output);
$this->logger->log($output->fetch(), $exitCode);
if ($exitCode !== 0) {
throw new ComposerUpdateFailedException('flarum/*', $output->fetch());
}

View File

@ -11,6 +11,7 @@ use Flarum\Extension\ExtensionManager;
use Illuminate\Contracts\Events\Dispatcher;
use SychO\PackageManager\Exception\ExtensionNotInstalledException;
use SychO\PackageManager\Extension\Event\Removed;
use SychO\PackageManager\OutputLogger;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\BufferedOutput;
@ -31,11 +32,17 @@ class RemoveExtensionHandler
*/
protected $events;
public function __construct(Application $composer, ExtensionManager $extensions, Dispatcher $events)
/**
* @var OutputLogger
*/
protected $logger;
public function __construct(Application $composer, ExtensionManager $extensions, Dispatcher $events, OutputLogger $logger)
{
$this->composer = $composer;
$this->extensions = $extensions;
$this->events = $events;
$this->logger = $logger;
}
/**
@ -58,7 +65,9 @@ class RemoveExtensionHandler
'packages' => [$extension->name],
]);
$this->composer->run($input, $output);
$exitCode = $this->composer->run($input, $output);
$this->logger->log($output->fetch(), $exitCode);
$this->events->dispatch(
new Removed($extension)

View File

@ -13,6 +13,7 @@ use SychO\PackageManager\Exception\ComposerRequireFailedException;
use SychO\PackageManager\Exception\ExtensionAlreadyInstalledException;
use SychO\PackageManager\Extension\Event\Installed;
use SychO\PackageManager\Extension\ExtensionUtils;
use SychO\PackageManager\OutputLogger;
use SychO\PackageManager\RequirePackageValidator;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\BufferedOutput;
@ -39,12 +40,18 @@ class RequireExtensionHandler
*/
protected $events;
public function __construct(Application $composer, ExtensionManager $extensions, RequirePackageValidator $validator, Dispatcher $events)
/**
* @var OutputLogger
*/
protected $logger;
public function __construct(Application $composer, ExtensionManager $extensions, RequirePackageValidator $validator, Dispatcher $events, OutputLogger $logger)
{
$this->composer = $composer;
$this->extensions = $extensions;
$this->validator = $validator;
$this->events = $events;
$this->logger = $logger;
}
/**
@ -72,6 +79,8 @@ class RequireExtensionHandler
$exitCode = $this->composer->run($input, $output);
$this->logger->log($output->fetch(), $exitCode);
if ($exitCode !== 0) {
throw new ComposerRequireFailedException($command->package, $output->fetch());
}

View File

@ -13,6 +13,7 @@ use Illuminate\Contracts\Events\Dispatcher;
use SychO\PackageManager\Exception\ComposerUpdateFailedException;
use SychO\PackageManager\Exception\ExtensionNotInstalledException;
use SychO\PackageManager\Extension\Event\Updated;
use SychO\PackageManager\OutputLogger;
use SychO\PackageManager\UpdateExtensionValidator;
use SychO\PackageManager\LastUpdateCheck;
use Symfony\Component\Console\Input\ArrayInput;
@ -45,13 +46,25 @@ class UpdateExtensionHandler
*/
protected $events;
public function __construct(Application $composer, ExtensionManager $extensions, UpdateExtensionValidator $validator, LastUpdateCheck $lastUpdateCheck, Dispatcher $events)
/**
* @var OutputLogger
*/
protected $logger;
public function __construct(
Application $composer,
ExtensionManager $extensions,
UpdateExtensionValidator $validator,
LastUpdateCheck $lastUpdateCheck,
Dispatcher $events,
OutputLogger $logger)
{
$this->composer = $composer;
$this->extensions = $extensions;
$this->validator = $validator;
$this->lastUpdateCheck = $lastUpdateCheck;
$this->events = $events;
$this->logger = $logger;
}
/**
@ -78,6 +91,8 @@ class UpdateExtensionHandler
$exitCode = $this->composer->run($input, $output);
$this->logger->log($output->fetch(), $exitCode);
if ($exitCode !== 0) {
throw new ComposerUpdateFailedException($extension->name, $output->fetch());
}

View File

@ -0,0 +1,27 @@
<?php
namespace SychO\PackageManager;
use Psr\Log\LoggerInterface;
class OutputLogger
{
/**
* @var LoggerInterface
*/
protected $logger;
public function __construct(LoggerInterface $logger)
{
$this->logger = $logger;
}
public function log(string $output, int $exitCode): void
{
if ($exitCode === 0) {
$this->logger->info($output);
} else {
$this->logger->error($output);
}
}
}

View File

@ -8,7 +8,6 @@ namespace SychO\PackageManager;
use Composer\Config;
use Composer\Console\Application;
use Flarum\Extension\Extension;
use Flarum\Extension\ExtensionManager;
use Flarum\Foundation\AbstractServiceProvider;
use Flarum\Foundation\Paths;
@ -16,8 +15,10 @@ use Flarum\Frontend\RecompileFrontendAssets;
use Flarum\Locale\LocaleManager;
use Illuminate\Contracts\Container\Container;
use Illuminate\Contracts\Events\Dispatcher;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\RotatingFileHandler;
use Monolog\Logger;
use SychO\PackageManager\Event\FlarumUpdated;
use SychO\PackageManager\Extension\Event\Installed;
use SychO\PackageManager\Extension\Event\Updated;
use SychO\PackageManager\Listener\PostUpdateListener;
@ -47,6 +48,16 @@ class PackageManagerServiceProvider extends AbstractServiceProvider
});
$this->container->alias(Application::class, 'flarum.composer');
$this->container->singleton(OutputLogger::class, function (Container $container) {
$logPath = $container->make(Paths::class)->storage.'/logs/composer/output.log';
$handler = new RotatingFileHandler($logPath, Logger::INFO);
$handler->setFormatter(new LineFormatter(null, null, true, true));
$logger = new Logger('composer', [$handler]);
return new OutputLogger($logger);
});
}
public function boot(Container $container)