Events
- Registering Event Listeners
\MVC\Event::run()
\MVC\Event::bind()
\MVC\Event::delete()
- myMVC Standard Events
Registering Event Listeners
Location where to write Event Listeners
module/{module}/etc/config/event/
Listeners written here are executed right after all Configurations have been loaded. No essential classes of myMVC have been loaded or processed at this point yet.
So this is the perfect place to influence the behavior of the starting application.
Example: log current request object to debug.log when on develop environment
<?php
\MVC\Event::processBindConfigStack([
'mvc.request.getCurrentRequest.after' => [ // Event
function (\MVC\DataType\DTArrayObject $oDTArrayObject) { // Closure
// get request object
$oDTRequestCurrent = $oDTArrayObject->getDTKeyValueByKey('oDTRequestCurrent')->get_sValue();
if ('develop' === \MVC\Config::get_MVC_ENV())
{
\MVC\Log::write($oDTRequestCurrent, 'debug.log');
}
},
],
]);
You can write your own event bondings into an already existing file.
And you may add as many events as your application requires.
You can also create your own php file in that folder and note your event bondings, no problem.
⚠ but consider the loading order of the files, which is a-z.
\MVC\Event::run()
with run
you initialize an event; bonded Closures to the event name get executed in order.
Syntax
\MVC\Event::run('eventName', {mixed} );
Example: Run a simple Event
\MVC\Event::run('foo');
Example: pass a value to the event
\MVC\Event::run('foo', 123);
Example: pass an DTArrayObject
Object with Infos
\MVC\Event::run('foo', DTArrayObject::create()
->add_aKeyValue(
DTKeyValue::create()->set_sKey('foo')->set_sValue('bar')
)
);
\MVC\Event::bind()
with bind
you create a Listener to an expected event;
you bind a closure to the expected event.
Syntax
\MVC\Event::bind('eventName', {closure} );
Example: bind a closure to the Event "foo"
\MVC\Event::bind('foo', function(\MVC\DataType\DTArrayObject $oDTArrayObject) {
\MVC\Debug:info($oDTArrayObject);
});
- If the event 'foo' is triggered, the closure will be executed: the content of
$oDTArrayObject
will be displayed on screen
Example: bind a closure to a concrete Controller::method
\MVC\Event::bind('\{module}\Controller\Index::foo', function (\MVC\DataType\DTArrayObject $oDTArrayObject) {
\MVC\Debug:info($oDTArrayObject);
});
- If the method
\{module}\Controller\Index::foo
is being called, the closure will be executed: the content of$oDTArrayObject
will be displayed on screen - ⚠ Make sure to write the event name as the method was a static one, even it is not.
\MVC\Event::processBindConfigStack()
- Bind to Events via config Stack
Instead of writing one bind
command expressure after the other you can make use of array notation and use Event::processBindConfigStack
.
Syntax
\MVC\Event::processBindConfigStack([ '{EventName}' => [ {Closure}, ], ]);
This way you can note bondings to multiple Events at once and you can even note multiple closures for each event.
This reduces complexity and improves readability.
Example: using config Stack to bind closures to Events
<?php
\MVC\Event::processBindConfigStack([ // Bondings to 2 Events
'\Foo\Controller\Index::foo' => [ // 2 Closures bonded to this Event
function (\MVC\DataType\DTArrayObject $oDTArrayObject) {
\MVC\Debug:info($oDTArrayObject);
},
function (\MVC\DataType\DTArrayObject $oDTArrayObject) {
\MVC\Log:write($oDTArrayObject, 'debug.log');
}
],
'\Foo\Controller\Index::bar' => [ // 1 Closure bonded to this Event
function (\MVC\DataType\DTArrayObject $oDTArrayObject) {
\MVC\Log:write($oDTArrayObject, 'debug.log');
}
],
]);
\MVC\Event::delete()
delete one or all events.
⚠ If this parameter not is set, all events are going to be deleted.
Example: delete the certain Event named foo.bar
\MVC\Event::delete('foo.bar');
Example: delete all Events
\MVC\Event::delete();
myMVC Standard Events
Event Name | Event::bind perforemd in |
Event::run located in |
value passed |
---|---|---|---|
mvc.event.init.after | \MVC\Event::init |
||
policy.index.requestMethodHasToMatchRouteMethod.after | modules/{module}/etc/event/policy.php | \{module}\Policy\Index::requestMethodHasToMatchRouteMethod |
\MVC\DataType\DTArrayObject $oDTArrayObject |
mvc.application.setSession.before | modules/{module}/etc/event/default.php | \MVC\Application::initSession |
|
mvc.application.setSession.after | \MVC\Application::initSession |
\MVC\DataType\DTArrayObject $oDTArrayObject |
|
mvc.application.construct.after | \MVC\Application::__construct |
||
mvc.application.destruct.before | \MVC\Application::__destruct |
\MVC\DataType\DTArrayObject $oDTArrayObject |
|
mvc.controller.init.before | \MVC\Request::getCurrentRequest |
\MVC\Controller::init |
\MVC\DataType\DTArrayObject $oDTArrayObject |
mvc.controller.init.after | \MVC\Controller::init |
\MVC\DataType\DTArrayObject $oDTArrayObject |
|
mvc.controller.runTargetClassPreconstruct.after | \MVC\Controller::runTargetClassPreconstruct |
\MVC\DataType\DTArrayObject $oDTArrayObject |
|
mvc.controller.destruct.before | \MVC\Controller::__destruct |
\MVC\DataType\DTArrayObject $oDTArrayObject |
|
mvc.error | \MVC\Error::init modules/{module}/etc/event/default.php |
\MVC\Controller::runTargetClassPreconstruct \MVC\Request::getUriProtocol \MVC\Policy::apply |
\MVC\DataType\DTArrayObject $oDTArrayObject |
mvc.policy.init.before | \MVC\Policy::init |
||
mvc.policy.init.after | \MVC\Policy::init |
||
mvc.policy.set.before | \MVC\Policy::set |
array $aPolicy all declared policies |
|
mvc.policy.set.after | \MVC\Policy::set |
array $aPolicy all declared policies |
|
mvc.policy.unset.before | \MVC\Policy::unset |
array $aPolicy all declared policies |
|
mvc.policy.unset.after | \MVC\Policy::unset |
array $aPolicy all declared policies |
|
mvc.policy.apply.before | \MVC\Policy::apply |
array $aPolicy matching policy rules on the current request |
|
mvc.policy.apply.execute | \MVC\Policy::apply |
\MVC\DataType\DTArrayObject $oDTArrayObject |
|
mvc.reflex.reflect.before | \MVC\Reflex::reflect |
\MVC\DataType\DTArrayObject $oDTArrayObject |
|
mvc.reflex.reflect.targetObject.before | modules/{module}/etc/event/default.php | \MVC\Reflex::reflect |
\MVC\DataType\DTArrayObject $oDTArrayObject |
mvc.reflex.reflect.targetObject.after | modules/{module}/etc/event/default.php | \MVC\Reflex::reflect |
\MVC\DataType\DTArrayObject $oDTArrayObject |
mvc.reflex.destruct.before | \MVC\Reflex::__destruct |
\MVC\DataType\DTArrayObject $oDTArrayObject |
|
mvc.route.init.before | \MVC\Route::init |
||
mvc.route.init.after | \MVC\Route::init |
||
$sControllerClassName :: $sMethod |
\MVC\Reflex::reflect |
||
mvc.request.getCurrentRequest.after | \MVC\Request::getCurrentRequest |
\MVC\DataType\DTArrayObject $oDTArrayObject |
|
mvc.request.redirect | \MVC\Request::redirect |
\MVC\DataType\DTArrayObject $oDTArrayObject |
|
mvc.view.render.before | \MVC\InfoTool::__construct |
\MVC\View::render |
\MVC\DataType\DTArrayObject $oDTArrayObject |
mvc.view.renderString.before | \MVC\View::renderString |
\MVC\DataType\DTArrayObject $oDTArrayObject |
|
mvc.view.renderString.after | \MVC\View::renderString |
\MVC\DataType\DTArrayObject $oDTArrayObject |
|
mvc.view.render.after | \MVC\View::render |
\MVC\DataType\DTArrayObject $oDTArrayObject |
|
mvc.debug.stop.after | modules/{module}/etc/event/default.php | \MVC\Debug::stop |
\MVC\DataType\DTArrayObject $oDTArrayObject |
mvc.lock.create | \MVC\Lock::create |
\MVC\DataType\DTArrayObject $oDTArrayObject |
|
mvc.view.echoOut.off | \MVC\View::__construct |
||
mvc.view.echoOut.on | \MVC\View::__construct |
||
mvc.view.render.off | \MVC\View::__construct |
||
mvc.view.render.on | \MVC\View::__construct |