Skip to content

Commit

Permalink
Clean up some code
Browse files Browse the repository at this point in the history
  • Loading branch information
mmikkel committed Jan 6, 2024
1 parent 34792f6 commit beade1e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 37 deletions.
54 changes: 19 additions & 35 deletions src/CpClearCache.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* CP Cache QuickMenu plugin for Craft CMS 3.x
* CP Cache QuickMenu plugin for Craft CMS 5.x
*
* Fewer clicks to get clearin'
*
Expand All @@ -12,19 +12,15 @@

use Craft;
use craft\base\Plugin;
use craft\base\UtilityInterface;
use craft\events\RegisterCpNavItemsEvent;
use craft\helpers\ArrayHelper;
use craft\utilities\ClearCaches;
use craft\web\Application;
use craft\web\assets\utilities\UtilitiesAsset;
use craft\web\twig\variables\Cp;
use craft\web\View;

use yii\base\Event;
use yii\base\InvalidConfigException;

use mmikkel\cpclearcache\CpClearCacheBundle;
use yii\web\View as ViewAlias;

/**
* Class CpClearCache
Expand All @@ -36,59 +32,47 @@
*/
class CpClearCache extends Plugin
{
// Static Properties
// =========================================================================

/**
* @var CpClearCache
*/
public static $plugin;

// Public Methods
// =========================================================================

/**
* @inheritdoc
* @return void
*/
public function init()
public function init(): void
{

parent::init();
self::$plugin = $this;

// Register alias
Craft::setAlias('@mmikkel/cpclearcache', __DIR__);

$request = Craft::$app->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;
Expand All @@ -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 = "<div>$html</div>";
$view = Craft::$app->getView();
$view->registerAssetBundle(UtilitiesAsset::class);
Expand Down Expand Up @@ -132,7 +116,7 @@ function () {
* @throws \Twig\Error\SyntaxError
* @throws \yii\base\Exception
*/
protected function getClearCachesUtilityHtml(): string
private function _getClearCachesUtilityHtml(): string
{
$cacheOptions = [];
$tagOptions = [];
Expand Down
4 changes: 2 additions & 2 deletions src/CpClearCacheBundle.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* CP Cache QuickMenu plugin for Craft CMS 3.x
* CP Cache QuickMenu plugin for Craft CMS 5.x
*
* Fewer clicks to get clearin'
*
Expand All @@ -15,7 +15,7 @@

class CpClearCacheBundle extends AssetBundle
{
public function init()
public function init(): void
{
// define the path that your publishable resources live
$this->sourcePath = '@mmikkel/cpclearcache/resources';
Expand Down

0 comments on commit beade1e

Please sign in to comment.