Jump to content

EventLoader


ukyo
 Share

Recommended Posts

Hey, @ukyo! I read the module description, but I can't say I fully grasp what it does) Could you please write why this module, what problems it solves, what are typical use cases. Looking forward to learn more about this one, as I already use and love other modules of yours))

  • Like 3
Link to comment
Share on other sites

On 7/23/2022 at 2:44 PM, Ivan Gretsky said:

Hey, @ukyo! I read the module description, but I can't say I fully grasp what it does) Could you please write why this module, what problems it solves, what are typical use cases. Looking forward to learn more about this one, as I already use and love other modules of yours))

This module help you to organize your events in files. Module using CORE hook system. After i wrote many events inside my module, it was confused to find what is where. After that i think to write a EventLoader module for separate events in files and load them by using a prefix like `ready.` or `init.`.

Think, you have api, page method, page property etc. events on your /site/ready.php file or module. If you write all of these events on a single file, it could be hard to find what is where. At this point you can separate your events inside event files.

I will write 2 example file, may you have 5 - 15 or more event files and all file may have events more than 1.

/site/templates/configs/events/ready.api.php

<?php

namespace ProcessWire;

/**
 * Api calls
 **/
return [
    'events' => [
        '/hello/world' => [
            'fn' => function (HookEvent $e) {
                return 'Hello';
            }
        ],
        '/hola/world' => function (HookEvent $e) {
			return 'Hello';
        },
        '/hello/(earth|mars|jupiter)' => [
            'fn' => function (HookEvent $e) {
                return "Hello " . $event->arguments(1);
            }
        ],
		'/hola/(earth|mars|jupiter)' => function (HookEvent $e) {
			return "Hello " . $event->arguments(1);
		}
    ]
];

/site/templates/configs/events/ready.page.php

<?php

namespace ProcessWire;

return [
    'events' => [
        'Page::hello' => [
            'type' => 'method',
            'fn' => function (HookEvent $e) {
                $message = is_string($e->arguments(0)) ? $e->arguments(0) : '';
                $e->return = $message;
            }
        ]
    ]
];

Loading /site/ready.php events from event files

<?php namespace ProcessWire;

if(!defined("PROCESSWIRE")) die();

// this will load all event files, starts with `ready.` prefix
// __DIR__ . '/templates' => /processwire-root/site/templates/configs/events/
EventLoader::load(__DIR__ . '/templates', 'ready.');
  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...