-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add lifecycle * Apply fixes from StyleCI * format * Apply fixes from StyleCI --------- Co-authored-by: StyleCI Bot <[email protected]>
- Loading branch information
1 parent
af1b93d
commit 27c5462
Showing
6 changed files
with
213 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
} | ||
} |