From bb7ffd89a58690c20df9be76f8ff4f318c693a6c Mon Sep 17 00:00:00 2001 From: IanM Date: Mon, 25 Nov 2024 14:08:05 +0000 Subject: [PATCH] add lifecycle --- extend.php | 29 ++++++++++++++------ src/ConditionalCheck.php | 47 ++++++++++++++++++++++++++++++++ src/Listener/RepublishAssets.php | 18 ++++++++++++ src/Listener/SettingsChanged.php | 22 +++++++++++++++ src/Repository/S3Repository.php | 35 +++++++++++++++++++++++- src/S3Lifecycle.php | 35 ++++++++++++++++++++++++ 6 files changed, 177 insertions(+), 9 deletions(-) create mode 100644 src/ConditionalCheck.php create mode 100644 src/Listener/RepublishAssets.php create mode 100644 src/Listener/SettingsChanged.php create mode 100644 src/S3Lifecycle.php diff --git a/extend.php b/extend.php index ffffd8b..2c9311b 100644 --- a/extend.php +++ b/extend.php @@ -12,14 +12,16 @@ namespace FoF\S3Assets; use Flarum\Extend; +use Flarum\Foundation\Event\ClearingCache; +use Flarum\Settings\Event\Saving as SettingsSaving; return [ (new Extend\Frontend('admin')) - ->js(__DIR__.'/js/dist/admin.js') - ->css(__DIR__.'/less/admin.less') + ->js(__DIR__ . '/js/dist/admin.js') + ->css(__DIR__ . '/less/admin.less') ->content(Content\AdminPayload::class), - new Extend\Locales(__DIR__.'/locale'), + new Extend\Locales(__DIR__ . '/locale'), (new Extend\Settings()) ->default('fof-s3-assets.share_s3_config_with_fof_upload', false), @@ -27,10 +29,21 @@ (new Extend\ServiceProvider()) ->register(Provider\S3DiskProvider::class), - (new Extend\Console()) - ->command(Console\CopyAssetsCommand::class), + (new Extend\Event()) + ->listen(SettingsSaving::class, Listener\SettingsChanged::class) - (new Extend\Filesystem()) - ->driver('s3', Driver\S3Driver::class) - ->driver('local', Driver\S3Driver::class), + (new S3Lifecycle()), + + (new Extend\Conditional()) + ->when((resolve(ConditionalCheck::class))->validConfig(), fn() => [ + (new Extend\Console()) + ->command(Console\CopyAssetsCommand::class), + + (new Extend\Filesystem()) + ->driver('s3', Driver\S3Driver::class) + ->driver('local', Driver\S3Driver::class), + + (new Extend\Event()) + ->listen(ClearingCache::class, Listener\RepublishAssets::class) + ]) ]; diff --git a/src/ConditionalCheck.php b/src/ConditionalCheck.php new file mode 100644 index 0000000..6250101 --- /dev/null +++ b/src/ConditionalCheck.php @@ -0,0 +1,47 @@ +cache->get(self::CACHE_KEY)) && $cacheValue !== null) { + return $cacheValue; + } + + // If we don't have a cached value, check the config + $config = $this->config->config(); + + if (empty($config)) { + return false; + } + + try { + $this->validator->assertValid($config); + $this->cache->forever(self::CACHE_KEY, true); + } catch (Exception $e) { + $this->cache->forever(self::CACHE_KEY, false); + return false; + } + + return true; + } +} diff --git a/src/Listener/RepublishAssets.php b/src/Listener/RepublishAssets.php new file mode 100644 index 0000000..8944049 --- /dev/null +++ b/src/Listener/RepublishAssets.php @@ -0,0 +1,18 @@ +s3->publishAssets(); + } +} diff --git a/src/Listener/SettingsChanged.php b/src/Listener/SettingsChanged.php new file mode 100644 index 0000000..1e79743 --- /dev/null +++ b/src/Listener/SettingsChanged.php @@ -0,0 +1,22 @@ +cache->forget(ConditionalCheck::CACHE_KEY); + } +} diff --git a/src/Repository/S3Repository.php b/src/Repository/S3Repository.php index 65a06d2..c08e56b 100644 --- a/src/Repository/S3Repository.php +++ b/src/Repository/S3Repository.php @@ -11,15 +11,22 @@ namespace FoF\S3Assets\Repository; +use Flarum\Foundation\Console\AssetsPublishCommand; +use Flarum\Foundation\Console\CacheClearCommand; use FoF\S3Assets\Driver\Config as DriverConfig; use Illuminate\Support\Arr; use Psr\Log\LoggerInterface; +use Symfony\Component\Console\Input\ArrayInput; +use Symfony\Component\Console\Output\NullOutput; class S3Repository { public function __construct( protected LoggerInterface $logger, - protected DriverConfig $config + protected DriverConfig $config, + protected CacheClearCommand $cache, + protected AssetsPublishCommand $publish, + ) { } @@ -27,4 +34,30 @@ public function cdnHost(): string { return Arr::get($this->config->config(), 'url'); } + + /** + * Just a helper method to call the Flarum clear cache command + * + * @return void + */ + public function clearCache(): void + { + $this->cache->run( + new ArrayInput([]), + new NullOutput() + ); + } + + /** + * Just a helper method to call the Flarum publish assets command + * + * @return void + */ + public function publishAssets(): void + { + $this->publish->run( + new ArrayInput([]), + new NullOutput() + ); + } } diff --git a/src/S3Lifecycle.php b/src/S3Lifecycle.php new file mode 100644 index 0000000..18e9797 --- /dev/null +++ b/src/S3Lifecycle.php @@ -0,0 +1,35 @@ +make(ConditionalCheck::class)->validConfig(); + + if ($configured) { + /** @var S3Repository $s3 */ + $s3 = $container->make(S3Repository::class); + $s3->publishAssets(); + } + } + + public function onDisable(Container $container, Extension $extension): void + { + /** @var S3Repository $s3 */ + $s3 = $container->make(S3Repository::class); + $s3->publishAssets(); + } + + public function extend(Container $container, Extension $extension): void + { + // Not used here. + } +}