From beade1eb3f94e09ea7408c25439773ff13b7c102 Mon Sep 17 00:00:00 2001 From: Mats Mikkel Rummelhoff Date: Sat, 6 Jan 2024 01:13:27 +0100 Subject: [PATCH] Clean up some code --- src/CpClearCache.php | 54 ++++++++++++++------------------------ src/CpClearCacheBundle.php | 4 +-- 2 files changed, 21 insertions(+), 37 deletions(-) diff --git a/src/CpClearCache.php b/src/CpClearCache.php index 6ce2b12..f6eddd0 100644 --- a/src/CpClearCache.php +++ b/src/CpClearCache.php @@ -1,6 +1,6 @@ getRequest(); - if (!$request->getIsCpRequest() || $request->getIsConsoleRequest()) { + if (!$request->getIsCpRequest() || !$request->getIsGet() || $request->getIsLoginRequest() || $request->getIsConsoleRequest()) { return; } - Event::on( - Application::class, - Application::EVENT_INIT, - function () { + Craft::$app->onInit(function () { + try { $this->doIt(); - } - ); + } catch (\Throwable $e) { + Craft::error($e, __METHOD__); + }; + }); } /** - * + * @return void + * @throws \Throwable */ - protected function doIt() + protected function doIt(): void { - $user = Craft::$app->getUser(); - if (!$user->id) { + if (!Craft::$app->getUser()->getIdentity()) { return; } + // Get the Clear Caches utility and check that the current user has access to it $utilitiesService = Craft::$app->getUtilities(); - - /** @var UtilityInterface $clearCachesUtility */ $clearCachesUtility = $utilitiesService->getUtilityTypeById('clear-caches'); if ($clearCachesUtility === null || $utilitiesService->checkAuthorization($clearCachesUtility) === false) { return; @@ -97,10 +81,10 @@ protected function doIt() // Register asset bundle Event::on( View::class, - View::EVENT_BEGIN_BODY, + ViewAlias::EVENT_BEGIN_BODY, function () { try { - $html = $this->getClearCachesUtilityHtml(); + $html = $this->_getClearCachesUtilityHtml(); $html = "
$html
"; $view = Craft::$app->getView(); $view->registerAssetBundle(UtilitiesAsset::class); @@ -132,7 +116,7 @@ function () { * @throws \Twig\Error\SyntaxError * @throws \yii\base\Exception */ - protected function getClearCachesUtilityHtml(): string + private function _getClearCachesUtilityHtml(): string { $cacheOptions = []; $tagOptions = []; diff --git a/src/CpClearCacheBundle.php b/src/CpClearCacheBundle.php index 94d215f..c75a9ef 100644 --- a/src/CpClearCacheBundle.php +++ b/src/CpClearCacheBundle.php @@ -1,6 +1,6 @@ sourcePath = '@mmikkel/cpclearcache/resources';