2014-08-14 18:46:26 +00:00
|
|
|
<?php
|
|
|
|
|
2022-11-02 20:47:04 +00:00
|
|
|
namespace MediaWiki\Extension\Notifications\Mapper;
|
|
|
|
|
2023-06-09 00:21:09 +00:00
|
|
|
use InvalidArgumentException;
|
2022-11-02 20:47:04 +00:00
|
|
|
use MWEchoDbFactory;
|
|
|
|
|
2014-08-14 18:46:26 +00:00
|
|
|
/**
|
|
|
|
* Abstract mapper for model
|
|
|
|
*/
|
2022-11-02 20:47:04 +00:00
|
|
|
abstract class AbstractMapper {
|
2014-08-14 18:46:26 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Echo database factory
|
|
|
|
* @var MWEchoDbFactory
|
|
|
|
*/
|
|
|
|
protected $dbFactory;
|
|
|
|
|
2014-08-07 19:10:37 +00:00
|
|
|
/**
|
|
|
|
* Event listeners for method like insert/delete
|
2018-08-13 07:25:22 +00:00
|
|
|
* @var array[]
|
2014-08-07 19:10:37 +00:00
|
|
|
*/
|
|
|
|
protected $listeners;
|
|
|
|
|
2014-08-14 18:46:26 +00:00
|
|
|
/**
|
2015-11-12 22:10:52 +00:00
|
|
|
* @param MWEchoDbFactory|null $dbFactory
|
2014-08-14 18:46:26 +00:00
|
|
|
*/
|
|
|
|
public function __construct( MWEchoDbFactory $dbFactory = null ) {
|
2022-11-08 19:58:23 +00:00
|
|
|
$this->dbFactory = $dbFactory ?? MWEchoDbFactory::newFromDefault();
|
2014-08-14 18:46:26 +00:00
|
|
|
}
|
|
|
|
|
2014-08-07 19:10:37 +00:00
|
|
|
/**
|
|
|
|
* Attach a listener
|
|
|
|
*
|
|
|
|
* @param string $method Method name
|
|
|
|
* @param string $key Identification of the callable
|
|
|
|
* @param callable $callable
|
|
|
|
*/
|
|
|
|
public function attachListener( $method, $key, $callable ) {
|
|
|
|
if ( !method_exists( $this, $method ) ) {
|
2023-06-09 00:21:09 +00:00
|
|
|
throw new InvalidArgumentException( $method . ' does not exist in ' . get_class( $this ) );
|
2014-08-07 19:10:37 +00:00
|
|
|
}
|
|
|
|
if ( !isset( $this->listeners[$method] ) ) {
|
2016-12-05 18:51:07 +00:00
|
|
|
$this->listeners[$method] = [];
|
2014-08-07 19:10:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$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
|
|
|
|
*
|
2017-10-14 13:45:13 +00:00
|
|
|
* @param string $method
|
2018-08-13 07:25:22 +00:00
|
|
|
* @return callable[]
|
2014-08-07 19:10:37 +00:00
|
|
|
*/
|
|
|
|
public function getMethodListeners( $method ) {
|
|
|
|
if ( !method_exists( $this, $method ) ) {
|
2023-06-09 00:21:09 +00:00
|
|
|
throw new InvalidArgumentException( $method . ' does not exist in ' . get_class( $this ) );
|
2014-08-07 19:10:37 +00:00
|
|
|
}
|
2019-05-07 16:52:01 +00:00
|
|
|
|
|
|
|
return $this->listeners[$method] ?? [];
|
2014-08-07 19:10:37 +00:00
|
|
|
}
|
|
|
|
|
2014-08-14 18:46:26 +00:00
|
|
|
}
|