mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/Echo
synced 2024-12-01 02:46:46 +00:00
61fea56641
Change-Id: I71039eb03b4b7e617ce03d515a6d51c4f3666ab8
77 lines
1.6 KiB
PHP
77 lines
1.6 KiB
PHP
<?php
|
|
|
|
/**
|
|
* Abstract mapper for model
|
|
*/
|
|
abstract class EchoAbstractMapper {
|
|
|
|
/**
|
|
* Echo database factory
|
|
* @var MWEchoDbFactory
|
|
*/
|
|
protected $dbFactory;
|
|
|
|
/**
|
|
* Event listeners for method like insert/delete
|
|
* @var array
|
|
*/
|
|
protected $listeners;
|
|
|
|
/**
|
|
* @param MWEchoDbFactory|null
|
|
*/
|
|
public function __construct( MWEchoDbFactory $dbFactory = null ) {
|
|
if ( $dbFactory === null ) {
|
|
$dbFactory = MWEchoDbFactory::newFromDefault();
|
|
}
|
|
$this->dbFactory = $dbFactory;
|
|
}
|
|
|
|
/**
|
|
* 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 ) ) {
|
|
throw new MWException( $method . ' does not exist in ' . get_class( $this ) );
|
|
}
|
|
if ( !isset( $this->listeners[$method] ) ) {
|
|
$this->listeners[$method] = array();
|
|
}
|
|
|
|
$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
|
|
*
|
|
* @return array
|
|
*/
|
|
public function getMethodListeners( $method ) {
|
|
if ( !method_exists( $this, $method ) ) {
|
|
throw new MWException( $method . ' does not exist in ' . get_class( $this ) );
|
|
}
|
|
if ( isset( $this->listeners[$method] ) ) {
|
|
return $this->listeners[$method];
|
|
} else {
|
|
return array();
|
|
}
|
|
}
|
|
|
|
}
|