From 2defb17cc2f3f98fd017c74ba6db35c9dbee0a45 Mon Sep 17 00:00:00 2001 From: David Wheatley Date: Tue, 26 Jul 2022 13:48:04 +0100 Subject: [PATCH] feat: publish assets on admin dashboard cache clear (#3564) Co-authored-by: Sami Mazouz --- .../js/src/admin/components/StatusWidget.js | 10 ++++++- framework/core/locale/core.yml | 1 + .../Api/Controller/ClearCacheController.php | 26 +++++++++++++++++-- .../src/Foundation/ErrorServiceProvider.php | 3 +++ framework/core/src/Foundation/IOException.php | 20 ++++++++++++++ 5 files changed, 57 insertions(+), 3 deletions(-) create mode 100644 framework/core/src/Foundation/IOException.php diff --git a/framework/core/js/src/admin/components/StatusWidget.js b/framework/core/js/src/admin/components/StatusWidget.js index 7ced35b47..34ef12579 100644 --- a/framework/core/js/src/admin/components/StatusWidget.js +++ b/framework/core/js/src/admin/components/StatusWidget.js @@ -56,6 +56,14 @@ export default class StatusWidget extends DashboardWidget { method: 'DELETE', url: app.forum.attribute('apiUrl') + '/cache', }) - .then(() => window.location.reload()); + .then(() => window.location.reload()) + .catch((e) => { + if (e.status === 409) { + app.alerts.clear(); + app.alerts.show({ type: 'error' }, app.translator.trans('core.admin.dashboard.io_error_message')); + } + + app.modal.close(); + }); } } diff --git a/framework/core/locale/core.yml b/framework/core/locale/core.yml index 29d407a38..010bb9cfb 100644 --- a/framework/core/locale/core.yml +++ b/framework/core/locale/core.yml @@ -53,6 +53,7 @@ core: dashboard: clear_cache_button: Clear Cache description: Your forum at a glance. + io_error_message: "Could not write to filesystem. Check your filesystem permissions an try again. Or try running from the command line." title: Dashboard tools_button: Tools diff --git a/framework/core/src/Api/Controller/ClearCacheController.php b/framework/core/src/Api/Controller/ClearCacheController.php index d1ea5efcc..f498cd590 100644 --- a/framework/core/src/Api/Controller/ClearCacheController.php +++ b/framework/core/src/Api/Controller/ClearCacheController.php @@ -9,7 +9,9 @@ namespace Flarum\Api\Controller; +use Flarum\Foundation\Console\AssetsPublishCommand; use Flarum\Foundation\Console\CacheClearCommand; +use Flarum\Foundation\IOException; use Flarum\Http\RequestUtil; use Laminas\Diactoros\Response\EmptyResponse; use Psr\Http\Message\ServerRequestInterface; @@ -23,26 +25,46 @@ class ClearCacheController extends AbstractDeleteController */ protected $command; + /** + * @var AssetsPublishCommand + */ + protected $assetsPublishCommand; + /** * @param CacheClearCommand $command */ - public function __construct(CacheClearCommand $command) + public function __construct(CacheClearCommand $command, AssetsPublishCommand $assetsPublishCommand) { $this->command = $command; + $this->assetsPublishCommand = $assetsPublishCommand; } /** * {@inheritdoc} + * @throws IOException|\Flarum\User\Exception\PermissionDeniedException */ protected function delete(ServerRequestInterface $request) { RequestUtil::getActor($request)->assertAdmin(); - $this->command->run( + $exitCode = $this->command->run( new ArrayInput([]), new NullOutput() ); + if ($exitCode !== 0) { + throw new IOException(); + } + + $exitCode = $this->assetsPublishCommand->run( + new ArrayInput([]), + new NullOutput() + ); + + if ($exitCode !== 0) { + throw new IOException(); + } + return new EmptyResponse(204); } } diff --git a/framework/core/src/Foundation/ErrorServiceProvider.php b/framework/core/src/Foundation/ErrorServiceProvider.php index 0ecf3a58e..495303b61 100644 --- a/framework/core/src/Foundation/ErrorServiceProvider.php +++ b/framework/core/src/Foundation/ErrorServiceProvider.php @@ -39,6 +39,9 @@ class ErrorServiceProvider extends AbstractServiceProvider // 405 Method Not Allowed 'method_not_allowed' => 405, + // 409 Conflict + 'io_error' => 409, + // 429 Too Many Requests 'too_many_requests' => 429, ]; diff --git a/framework/core/src/Foundation/IOException.php b/framework/core/src/Foundation/IOException.php new file mode 100644 index 000000000..19417b8c4 --- /dev/null +++ b/framework/core/src/Foundation/IOException.php @@ -0,0 +1,20 @@ +