Skip to content

Commit

Permalink
add lifecycle
Browse files Browse the repository at this point in the history
  • Loading branch information
imorland committed Nov 25, 2024
1 parent af1b93d commit bb7ffd8
Show file tree
Hide file tree
Showing 6 changed files with 177 additions and 9 deletions.
29 changes: 21 additions & 8 deletions extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,38 @@
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),

(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)
])
];
47 changes: 47 additions & 0 deletions src/ConditionalCheck.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace FoF\S3Assets;

use Exception;
use FoF\S3Assets\Driver\Config as DriverConfig;
use FoF\S3Assets\Validator\S3DiskConfigValidator;
use Illuminate\Contracts\Cache\Store;

class ConditionalCheck
{
const CACHE_KEY = 'fof-s3-assets-config-valid';

public function __construct(
protected DriverConfig $config,
protected S3DiskConfigValidator $validator,
protected Store $cache
) {}

/**
* Checks to see if the supplied config passes the required checks
*/
public function validConfig(): bool
{
// If we have a cached value, return it
if (($cacheValue = $this->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;
}
}
18 changes: 18 additions & 0 deletions src/Listener/RepublishAssets.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace FoF\S3Assets\Listener;

use Flarum\Foundation\Event\ClearingCache;
use FoF\S3Assets\Repository\S3Repository;

class RepublishAssets
{
public function __construct(
protected S3Repository $s3
) {}

public function handle(ClearingCache $event)
{
$this->s3->publishAssets();
}
}
22 changes: 22 additions & 0 deletions src/Listener/SettingsChanged.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace FoF\S3Assets\Listener;

use Flarum\Settings\Event\Saving;
use FoF\S3Assets\ConditionalCheck;
use Illuminate\Contracts\Cache\Store;

class SettingsChanged
{
public function __construct(
protected Store $cache
) {}

public function handle(Saving $event)
{
// TODO: only clear the config cache if any of the S3 settings have changed.
// We should check for both this extension's settings and the fof-upload
// settings, as we might be using the S3 config from there.
$this->cache->forget(ConditionalCheck::CACHE_KEY);
}
}
35 changes: 34 additions & 1 deletion src/Repository/S3Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,53 @@

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,

) {
}

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()
);
}
}
35 changes: 35 additions & 0 deletions src/S3Lifecycle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace FoF\S3Assets;

use Flarum\Extend\LifecycleInterface;
use Illuminate\Contracts\Container\Container;
use Flarum\Extension\Extension;
use FoF\S3Assets\Repository\S3Repository;

class S3Lifecycle implements LifecycleInterface
{
public function onEnable(Container $container, Extension $extension): void
{
/** @var bool $configured */
$configured = $container->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.
}
}

0 comments on commit bb7ffd8

Please sign in to comment.