Skip to content

Commit

Permalink
Defer element queries to Application::EVENT_INIT
Browse files Browse the repository at this point in the history
  • Loading branch information
mmikkel committed May 7, 2022
1 parent 2f73610 commit f998366
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 44 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/).

## 2.0.1 - 2022-05-07
### Changed
- MatrixMate now defers any element queries to the `craft\web\Application::EVENT_INIT` event, avoiding potential issues with element queries being executed before Craft has fully initialised.

## 2.0.0 - 2022-05-04

### Added
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "vaersaagod/matrixmate",
"description": "Welding Matrix into shape, mate!",
"type": "craft-plugin",
"version": "2.0.0",
"version": "2.0.1",
"keywords": [
"craft",
"cms",
Expand Down
32 changes: 16 additions & 16 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

58 changes: 31 additions & 27 deletions src/MatrixMate.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@
use craft\events\TemplateEvent;
use craft\helpers\Json;
use craft\services\Fields;
use craft\services\Plugins;
use craft\web\Application;
use craft\web\Controller;

use craft\web\CpScreenResponseFormatter;
use craft\web\Request;
use craft\web\Response;
use craft\web\View;

use vaersaagod\matrixmate\assetbundles\matrixmate\MatrixMateAsset;
use vaersaagod\matrixmate\services\MatrixMateService;
use vaersaagod\matrixmate\models\Settings;
Expand Down Expand Up @@ -89,10 +90,10 @@ public function init()
// Defer further initialisation to after plugins have loaded, and only for CP web requests
if (Craft::$app->getRequest()->getIsCpRequest() && !Craft::$app->getRequest()->getIsConsoleRequest()) {
Event::on(
Plugins::class,
Plugins::EVENT_AFTER_LOAD_PLUGINS,
Application::class,
Application::EVENT_INIT,
function (): void {
$this->onAfterLoadPlugins();
$this->onAppInit();
}
);
}
Expand All @@ -106,7 +107,32 @@ function (): void {
);
}

public function onAfterLoadPlugins(): void
/**
* @return Settings
*/
public function getSettings(): Settings
{
if ($this->_settings === null) {
$this->_settings = $this->createSettingsModel();
}
return $this->_settings;
}

// Protected Methods
// =========================================================================

/**
* @return Settings
*/
protected function createSettingsModel(): Settings
{
return new Settings();
}

/**
* @return void
*/
protected function onAppInit(): void
{

if (!Craft::$app->getUser()->checkPermission('accessCp')) {
Expand Down Expand Up @@ -140,28 +166,6 @@ function (DefineHtmlEvent $event) {

}

/**
* @return Settings
*/
public function getSettings(): Settings
{
if ($this->_settings === null) {
$this->_settings = $this->createSettingsModel();
}
return $this->_settings;
}

// Protected Methods
// =========================================================================

/**
* @return Settings
*/
protected function createSettingsModel(): Settings
{
return new Settings();
}

/**
* @param Element $element
* @return string
Expand Down

0 comments on commit f998366

Please sign in to comment.