mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/Echo
synced 2024-12-04 20:28:49 +00:00
cfa424f16b
?? is an older PHP 7.0 feature. ??= was added in PHP 7.4, which we can finally use. Change-Id: If4145c48eb374aa8e5deeb38aecb27c6c8905382
79 lines
1.6 KiB
PHP
79 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace MediaWiki\Extension\Notifications\Mapper;
|
|
|
|
use MWEchoDbFactory;
|
|
use MWException;
|
|
|
|
/**
|
|
* Abstract mapper for model
|
|
*/
|
|
abstract class AbstractMapper {
|
|
|
|
/**
|
|
* Echo database factory
|
|
* @var MWEchoDbFactory
|
|
*/
|
|
protected $dbFactory;
|
|
|
|
/**
|
|
* Event listeners for method like insert/delete
|
|
* @var array[]
|
|
*/
|
|
protected $listeners;
|
|
|
|
/**
|
|
* @param MWEchoDbFactory|null $dbFactory
|
|
*/
|
|
public function __construct( MWEchoDbFactory $dbFactory = null ) {
|
|
$this->dbFactory = $dbFactory ?? MWEchoDbFactory::newFromDefault();
|
|
}
|
|
|
|
/**
|
|
* Attach a listener
|
|
*
|
|
* @param string $method Method name
|
|
* @param string $key Identification of the callable
|
|
* @param callable $callable
|
|
* @throws MWException
|
|
*/
|
|
public function attachListener( $method, $key, $callable ) {
|
|
if ( !method_exists( $this, $method ) ) {
|
|
throw new MWException( $method . ' does not exist in ' . get_class( $this ) );
|
|
}
|
|
if ( !isset( $this->listeners[$method] ) ) {
|
|
$this->listeners[$method] = [];
|
|
}
|
|
|
|
$this->listeners[$method][$key] = $callable;
|
|
}
|
|
|
|
/**
|
|
* Detach a listener
|
|
*
|
|
* @param string $method Method name
|
|
* @param string $key identification of the callable
|
|
*/
|
|
public function detachListener( $method, $key ) {
|
|
if ( isset( $this->listeners[$method] ) ) {
|
|
unset( $this->listeners[$method][$key] );
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get the listener for a method
|
|
*
|
|
* @param string $method
|
|
* @return callable[]
|
|
* @throws MWException
|
|
*/
|
|
public function getMethodListeners( $method ) {
|
|
if ( !method_exists( $this, $method ) ) {
|
|
throw new MWException( $method . ' does not exist in ' . get_class( $this ) );
|
|
}
|
|
|
|
return $this->listeners[$method] ?? [];
|
|
}
|
|
|
|
}
|