verbb / patrol

Easy Maintenance Mode and Smart HTTPS Routing for Craft CMS
MIT License
29 stars 8 forks source link

Patrol causing element query before Craft is finished initializing #20

Closed brandonkelly closed 2 years ago

brandonkelly commented 4 years ago

This line in Patrol::init():

https://github.com/selvinortiz/craft-plugin-patrol/blob/d443741770fbaca6adbd38236fb0faffab25be7a/src/Patrol.php#L47

Brings us to:

https://github.com/selvinortiz/craft-plugin-patrol/blob/d443741770fbaca6adbd38236fb0faffab25be7a/src/services/PatrolService.php#L66

Then:

https://github.com/selvinortiz/craft-plugin-patrol/blob/d443741770fbaca6adbd38236fb0faffab25be7a/src/services/PatrolService.php#L229

And finally:

https://github.com/selvinortiz/craft-plugin-patrol/blob/d443741770fbaca6adbd38236fb0faffab25be7a/src/services/PatrolService.php#L278

Which causes an element query to fetch the currently logged-in user.

Problem is, element queries shouldn’t get fired until the application is fully done initializing, so all the plugin-supplied custom fields, event listeners, etc., have had a chance to be registered/initialized. Otherwise you end up getting some weird plugin conflicts, such as https://github.com/craftcms/cms/issues/2860#issuecomment-564956191.

You can fix this by holding off until craft\web\Application::EVENT_INIT before calling $this->defaultService->watch().

use craft\web\Application;

Craft::$app->on(Application::EVENT_INIT, function() {
    $this->defaultService->watch();
});
selvinortiz commented 4 years ago

Thanks, Brandon.

I'll get this addressed and cut a new release.