Skip to content

Instantly share code, notes, and snippets.

@icewind1991
Last active December 17, 2015 01:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save icewind1991/1e315e6e75c106406405 to your computer and use it in GitHub Desktop.
Save icewind1991/1e315e6e75c106406405 to your computer and use it in GitHub Desktop.

Hooks

The main goal of the new hook system is to scope listeners and events to an instance of the emitting object (i.e. a filesystem or music scanner) which allows for better seperation of code and automated testing.

Interface

Any class capable of emitting events (i.e. filesystem, music scanner, etc) implements the \OC\Hooks\Emitter interface which provides the following methods.

  • listen(string $scope, string $method, callable $callback) Where callback can be any of the following:
    • an anonymous function.
    • an array containing a classname and static method name.
    • an array containing an instance of a class and a non-static method name.
  • removeListener(string $scope = null, string $method = null, callable $callback = null) where all arguments are optional, any argument left out or set to null will function as a wildcard.

Implementation

A basic implementation is provided by \OC\Hooks\BasicEmitter which implements the listen($scope, $method, $callback) method and provides a protected emit(string $scope, string $method, array $arguments) method. Classes capable of emitting events can inherit from this implementation which handles the logic of keeping track of listeners and transmitting events to the correct listeners when emitted.

Legacy

Additionally a class \OC\Hooks\LegacyEmitter can be used, which besides emitting the event to all registered listeners will also pass the event to the legacy hook system OC_Hooks, only classes which previously emitted legacy hooks should make use of this base-class, no new legacy hooks should be created to allow phasing out of the legacy system.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment