Skip to content

Commit

Permalink
add lifecycle (#5)
Browse files Browse the repository at this point in the history
* add lifecycle

* Apply fixes from StyleCI

* format

* Apply fixes from StyleCI

---------

Co-authored-by: StyleCI Bot <[email protected]>
  • Loading branch information
imorland and StyleCIBot authored Nov 25, 2024
1 parent af1b93d commit 27c5462
Show file tree
Hide file tree
Showing 6 changed files with 213 additions and 6 deletions.
23 changes: 18 additions & 5 deletions extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
namespace FoF\S3Assets;

use Flarum\Extend;
use Flarum\Foundation\Event\ClearingCache;
use Flarum\Settings\Event\Saving as SettingsSaving;

return [
(new Extend\Frontend('admin'))
Expand All @@ -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),
]),
];
58 changes: 58 additions & 0 deletions src/ConditionalCheck.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

/*
* This file is part of fof/s3-assets.
*
* Copyright (c) FriendsOfFlarum
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*/

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

/*
* This file is part of fof/s3-assets.
*
* Copyright (c) FriendsOfFlarum
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*/

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

/*
* This file is part of fof/s3-assets.
*
* Copyright (c) FriendsOfFlarum
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*/

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);
}
}
34 changes: 33 additions & 1 deletion src/Repository/S3Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,52 @@

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

/*
* This file is part of fof/s3-assets.
*
* Copyright (c) FriendsOfFlarum
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*/

namespace FoF\S3Assets;

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

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 27c5462

Please sign in to comment.