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.
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.
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.
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.