Post Composer Actions (Fixes #3)

This commit is contained in:
SychO9 2021-09-29 10:40:40 +01:00
parent 19da2e9827
commit e19c9ea67c
11 changed files with 172 additions and 10 deletions

View File

@ -7,6 +7,8 @@
namespace SychO\PackageManager\Command; namespace SychO\PackageManager\Command;
use Composer\Console\Application; use Composer\Console\Application;
use Illuminate\Contracts\Events\Dispatcher;
use SychO\PackageManager\Event\FlarumUpdated;
use SychO\PackageManager\Exception\ComposerUpdateFailedException; use SychO\PackageManager\Exception\ComposerUpdateFailedException;
use SychO\PackageManager\LastUpdateCheck; use SychO\PackageManager\LastUpdateCheck;
use Symfony\Component\Console\Input\ArrayInput; use Symfony\Component\Console\Input\ArrayInput;
@ -24,14 +26,21 @@ class MinorFlarumUpdateHandler
*/ */
protected $lastUpdateCheck; protected $lastUpdateCheck;
/**
* @var Dispatcher
*/
protected $events;
/** /**
* @param Application $composer * @param Application $composer
* @param LastUpdateCheck $lastUpdateCheck * @param LastUpdateCheck $lastUpdateCheck
* @param Dispatcher $events
*/ */
public function __construct(Application $composer, LastUpdateCheck $lastUpdateCheck) public function __construct(Application $composer, LastUpdateCheck $lastUpdateCheck, Dispatcher $events)
{ {
$this->composer = $composer; $this->composer = $composer;
$this->lastUpdateCheck = $lastUpdateCheck; $this->lastUpdateCheck = $lastUpdateCheck;
$this->events = $events;
} }
/** /**
@ -60,6 +69,10 @@ class MinorFlarumUpdateHandler
$this->lastUpdateCheck->forget('flarum/*', true); $this->lastUpdateCheck->forget('flarum/*', true);
$this->events->dispatch(
new FlarumUpdated()
);
return true; return true;
} }
} }

View File

@ -1,9 +1,15 @@
<?php <?php
/**
*
*/
namespace SychO\PackageManager\Command; namespace SychO\PackageManager\Command;
use Composer\Console\Application; use Composer\Console\Application;
use Flarum\Extension\ExtensionManager; use Flarum\Extension\ExtensionManager;
use Illuminate\Contracts\Events\Dispatcher;
use SychO\PackageManager\Extension\Event\Removed;
use Symfony\Component\Console\Input\ArrayInput; use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\BufferedOutput; use Symfony\Component\Console\Output\BufferedOutput;
@ -19,10 +25,16 @@ class RemoveExtensionHandler
*/ */
protected $extensions; protected $extensions;
public function __construct(Application $composer, ExtensionManager $extensions,) /**
* @var Dispatcher
*/
protected $events;
public function __construct(Application $composer, ExtensionManager $extensions, Dispatcher $events)
{ {
$this->composer = $composer; $this->composer = $composer;
$this->extensions = $extensions; $this->extensions = $extensions;
$this->events = $events;
} }
/** /**
@ -46,5 +58,9 @@ class RemoveExtensionHandler
]); ]);
$this->composer->run($input, $output); $this->composer->run($input, $output);
$this->events->dispatch(
new Removed($extension)
);
} }
} }

View File

@ -1,10 +1,16 @@
<?php <?php
/**
*
*/
namespace SychO\PackageManager\Command; namespace SychO\PackageManager\Command;
use Composer\Console\Application; use Composer\Console\Application;
use Flarum\Extension\ExtensionManager; use Flarum\Extension\ExtensionManager;
use Illuminate\Contracts\Events\Dispatcher;
use SychO\PackageManager\Exception\ComposerRequireFailedException; use SychO\PackageManager\Exception\ComposerRequireFailedException;
use SychO\PackageManager\Extension\Event\Installed;
use SychO\PackageManager\Extension\ExtensionUtils; use SychO\PackageManager\Extension\ExtensionUtils;
use SychO\PackageManager\RequirePackageValidator; use SychO\PackageManager\RequirePackageValidator;
use Symfony\Component\Console\Input\ArrayInput; use Symfony\Component\Console\Input\ArrayInput;
@ -27,11 +33,17 @@ class RequireExtensionHandler
*/ */
protected $validator; protected $validator;
public function __construct(Application $composer, ExtensionManager $extensions, RequirePackageValidator $validator) /**
* @var Dispatcher
*/
protected $events;
public function __construct(Application $composer, ExtensionManager $extensions, RequirePackageValidator $validator, Dispatcher $events)
{ {
$this->composer = $composer; $this->composer = $composer;
$this->extensions = $extensions; $this->extensions = $extensions;
$this->validator = $validator; $this->validator = $validator;
$this->events = $events;
} }
/** /**
@ -63,6 +75,10 @@ class RequireExtensionHandler
throw new ComposerRequireFailedException($command->package, $output->fetch()); throw new ComposerRequireFailedException($command->package, $output->fetch());
} }
$this->events->dispatch(
new Installed($extensionId)
);
return ['id' => $extensionId]; return ['id' => $extensionId];
} }
} }

View File

@ -9,7 +9,9 @@ namespace SychO\PackageManager\Command;
use Composer\Console\Application; use Composer\Console\Application;
use Flarum\Extension\ExtensionManager; use Flarum\Extension\ExtensionManager;
use Flarum\Settings\SettingsRepositoryInterface; use Flarum\Settings\SettingsRepositoryInterface;
use Illuminate\Contracts\Events\Dispatcher;
use SychO\PackageManager\Exception\ComposerUpdateFailedException; use SychO\PackageManager\Exception\ComposerUpdateFailedException;
use SychO\PackageManager\Extension\Event\Updated;
use SychO\PackageManager\UpdateExtensionValidator; use SychO\PackageManager\UpdateExtensionValidator;
use SychO\PackageManager\LastUpdateCheck; use SychO\PackageManager\LastUpdateCheck;
use Symfony\Component\Console\Input\ArrayInput; use Symfony\Component\Console\Input\ArrayInput;
@ -37,12 +39,18 @@ class UpdateExtensionHandler
*/ */
protected $lastUpdateCheck; protected $lastUpdateCheck;
public function __construct(Application $composer, ExtensionManager $extensions, UpdateExtensionValidator $validator, LastUpdateCheck $lastUpdateCheck) /**
* @var Dispatcher
*/
protected $events;
public function __construct(Application $composer, ExtensionManager $extensions, UpdateExtensionValidator $validator, LastUpdateCheck $lastUpdateCheck, Dispatcher $events)
{ {
$this->composer = $composer; $this->composer = $composer;
$this->extensions = $extensions; $this->extensions = $extensions;
$this->validator = $validator; $this->validator = $validator;
$this->lastUpdateCheck = $lastUpdateCheck; $this->lastUpdateCheck = $lastUpdateCheck;
$this->events = $events;
} }
/** /**
@ -75,6 +83,10 @@ class UpdateExtensionHandler
$this->lastUpdateCheck->forget($extension->name); $this->lastUpdateCheck->forget($extension->name);
$this->events->dispatch(
new Updated($extension)
);
return true; return true;
} }
} }

View File

@ -0,0 +1,8 @@
<?php
namespace SychO\PackageManager\Event;
class FlarumUpdated
{
}

View File

@ -1,18 +1,20 @@
<?php <?php
namespace SychO\PackageManager\Extension\Event; /**
*
*/
use Flarum\Extension\Extension; namespace SychO\PackageManager\Extension\Event;
class Installed class Installed
{ {
/** /**
* @var Extension * @var string
*/ */
public $extension; public $extensionId;
public function __construct(Extension $extension) public function __construct(string $extensionId)
{ {
$this->extension = $extension; $this->extensionId = $extensionId;
} }
} }

View File

@ -1,5 +1,9 @@
<?php <?php
/**
*
*/
namespace SychO\PackageManager\Extension\Event; namespace SychO\PackageManager\Extension\Event;
use Flarum\Extension\Extension; use Flarum\Extension\Extension;

View File

@ -1,5 +1,9 @@
<?php <?php
/**
*
*/
namespace SychO\PackageManager\Extension\Event; namespace SychO\PackageManager\Extension\Event;
use Flarum\Extension\Extension; use Flarum\Extension\Extension;

View File

@ -1,5 +1,9 @@
<?php <?php
/**
*
*/
namespace SychO\PackageManager\Extension; namespace SychO\PackageManager\Extension;
class ExtensionUtils class ExtensionUtils

View File

@ -0,0 +1,48 @@
<?php
/**
*
*/
namespace SychO\PackageManager\Listener;
use Composer\Command\ClearCacheCommand;
use Flarum\Database\Console\MigrateCommand;
use Flarum\Foundation\Console\AssetsPublishCommand;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\NullOutput;
class PostUpdateListener
{
/**
* @var ClearCacheCommand
*/
private $clearCache;
/**
* @var AssetsPublishCommand
*/
private $publishAssets;
/**
* @var MigrateCommand
*/
private $migrate;
public function __construct(ClearCacheCommand $clearCache, AssetsPublishCommand $publishAssets, MigrateCommand $migrate)
{
$this->clearCache = $clearCache;
$this->publishAssets = $publishAssets;
$this->migrate = $migrate;
}
/**
* @throws \Exception
*/
public function handle($event)
{
$this->clearCache->run(new ArrayInput([]), new NullOutput());
$this->migrate->run(new ArrayInput([]), new NullOutput());
$this->publishAssets->run(new ArrayInput([]), new NullOutput());
}
}

View File

@ -1,12 +1,25 @@
<?php <?php
/**
*
*/
namespace SychO\PackageManager; namespace SychO\PackageManager;
use Composer\Config; use Composer\Config;
use Composer\Console\Application; use Composer\Console\Application;
use Flarum\Extension\Extension;
use Flarum\Extension\ExtensionManager;
use Flarum\Foundation\AbstractServiceProvider; use Flarum\Foundation\AbstractServiceProvider;
use Flarum\Foundation\Paths; use Flarum\Foundation\Paths;
use Flarum\Frontend\RecompileFrontendAssets;
use Flarum\Locale\LocaleManager;
use Illuminate\Contracts\Container\Container; use Illuminate\Contracts\Container\Container;
use Illuminate\Contracts\Events\Dispatcher;
use SychO\PackageManager\Event\FlarumUpdated;
use SychO\PackageManager\Extension\Event\Installed;
use SychO\PackageManager\Extension\Event\Updated;
use SychO\PackageManager\Listener\PostUpdateListener;
class PackageManagerServiceProvider extends AbstractServiceProvider class PackageManagerServiceProvider extends AbstractServiceProvider
{ {
@ -35,4 +48,26 @@ class PackageManagerServiceProvider extends AbstractServiceProvider
$this->container->alias(Application::class, 'flarum.composer'); $this->container->alias(Application::class, 'flarum.composer');
} }
public function boot(Container $container)
{
/** @var Dispatcher $events */
$events = $container->make('events');
$events->listen(
[Updated::class],
function (Updated $event) use ($container) {
$recompile = new RecompileFrontendAssets(
$container->make('flarum.assets.forum'),
$container->make(LocaleManager::class)
);
$recompile->flush();
$container->make(ExtensionManager::class)->migrate($event->extension);
$event->extension->copyAssetsTo($container->make('filesystem')->disk('flarum-assets'));
}
);
$events->listen(FlarumUpdated::class, PostUpdateListener::class);
}
} }