HomeDevCentral

New feature: Events
1d4b0ae081a7Unpublished

Unpublished Commit · Learn More

Repository Importing: This repository is still importing.

Description

New feature: Events

Inspired from .Net world, and more precisely from the C# delegates,
these events allows to register application callbacks.

[ Add an event to a class ]

  1. Add an array as public property. By convention, add Events as suffix. This array will contain the functions to call when the event is fired.

    public $queryErrorEvents = [];
  1. Create a small protected method called by your class when the event should be triggered. The goal of this method is to initialize resources for the events callbacks parameters. By convention, add on as prefix.

    private function onQueryError ($query) { ... }
  1. From your on<Event> method, call Event::call, with two arrays as parameters : the array created in step 1, and the parameters.

    Events::call($this->queryErrorEvents, [$this, $query, $ex]);

[ Attach to an event ]

  1. Create a callable (anonymous function or name to an existing method for example) and add it to the initialized class instance's event array:

    $sqlErrorHandler = function (Database $db, $query, Exception $ex) { ... }; $db->queryErrorEvents[] = $sqlErrorHandler;
  1. When the eventis fired, Events:call do something like:

    yourFunction($this, $query, $ex);

[ Events::callOrThrow ]

This method acts like Events::call, but if no callable has been recordeed,
throsws an exception, submitted as paramater or detected in the parameters for
the callable.

Documentation ]

Here an example of the docblock for an event:

/**
 * @var Array
 * @event QueryError Functions to call when a query fails.# Please

enter the commit message for your changes. Lines starting

  • @eventparam Database $db The current database instance# with '#'

will be ignored, and an empty message aborts the commit.

  • @eventparam string $query The failed query# Explicit paths

specified without -i nor -o; assuming --only paths...

  • @eventparam DatabaseException $ex The exception describing the

query error# On branch feature/MySQLDatabase

 */# Changes to be committed:
public $queryErrorEvents = [];

@event <name> <description>
@eventparam <type> <name> <desription>

Details

Provenance
derecksonAuthored on
Parents
rOBSIDIANd2b88031d7e5: Refactoring: removing $smarty global variable
Branches
Unknown
Tags
Unknown