V2: Organizing event listeners in Plugin.php boot() by moving to another file

Greetings.

I’m looking to better organize the ton of event listeners in the Plugin boot() method. I inherited a project and there’s a lot of them. For future maintenance and code organization I’d like to move the event listeners related to Rainlab Blog into a separate file.

As a test I created a file, /plugins/acme/myplugin/classes/extend/RainlabBlogExtend.php, and moved all extend type event listeners out of the Plugins.php file’s boot() method and into this file. I then call a static run() method on that file which instantiates itself and runs all sub methods that set up the event listeners (so in the Plugins.php boot() method I have this line : RainlabBlogExtend::run():wink:

So my question is if this seems like it’s a good practice, and if there might be any issues setting up event listeners outside of the Plugin.php file itself.

Thanks!

Hey @Jinjo !

Sure, the Translate plugin extends so many areas that it has two “Event Registry” classes, similar to the pattern you’ve described:

And then called via the registration class:

public function register()
{
    EventCoreRegistry::instance()->registerEvents();
    EventPluginRegistry::instance()->registerEvents();
}

public function boot()
{
    EventCoreRegistry::instance()->bootEvents();
    EventPluginRegistry::instance()->bootEvents();
}