1. Go to this page and download the library: Download icanboogie/event library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
icanboogie / event example snippets
namespace ICanBoogie;
/* @var Application $app */
EventCollectionProvider::define(function() use ($app) {
static $collection;
return $collection ??= new EventCollection($app->configs['event']);
});
# Getting the event collection
$events = EventCollectionProvider::provide();
# or
$events = get_events();
namespace ICanBoogie\Operation;
use ICanBoogie\Event;
use ICanBoogie\HTTP\Request;
use ICanBoogie\HTTP\Response;
use ICanBoogie\Operation;
class ProcessEvent extends Event
{
/**
* Reference to the response result property.
*/
public mixed $result;
public function __construct(
Operation $target,
public readonly Request $request,
public readonly Response $response,
mixed &$result
) {
$this->result = &$result;
parent::__construct($target);
}
}
namespace ICanBoogie
$detach = $events->attach(function(Operation\BeforeProcessEvent $event, Operation $target) {
// …
});
# or, if the event doesn't have a target
$detach = $events->attach(function(Operation\BeforeProcessEvent $event) {
// …
});
$detach(); // You can detach if you no longer want to listen.
namespace ICanBoogie
class ValidateOperation
{
private $rules;
public function __construct(array $rules)
{
$this->rules = $rules;
}
public function __invoke(Operation\BeforeProcessEvent $event, Operation $target)
{
// …
}
}
// …
/* @var $events EventCollection */
/* @var $rules array<string, mixed> */
$events->attach(new ValidateOperation($rules));
namespace ICanBoogie;
use ICanBoogie\Routing\Controller;
// …
/* @var $events EventCollection */
$detach = $events->attach_to($controller, function(Controller\ActionEvent $event, Controller $target) {
echo "invoked!";
});
$controller_clone = clone $controller;
emit(new Controller\ActionEvent($controller_clone, …)); // nothing happens, it's a clone
emit(new Controller\ActionEvent($controller, …)); // echo "invoked!"
// …
$detach(); // You can detach if you no longer want to listen.