Merge "Move up devunt's name to Developers"
[lhc/web/wiklou.git] / includes / EventRelayerGroup.php
1 <?php
2 use MediaWiki\MediaWikiServices;
3
4 /**
5 * Factory class for spawning EventRelayer objects using configuration
6 *
7 * @author Aaron Schulz
8 * @since 1.27
9 */
10 class EventRelayerGroup {
11 /** @var array[] */
12 protected $configByChannel = [];
13
14 /** @var EventRelayer[] */
15 protected $relayers = [];
16
17 /**
18 * @param array[] $config Channel configuration
19 */
20 public function __construct( array $config ) {
21 $this->configByChannel = $config;
22 }
23
24 /**
25 * @deprecated since 1.27 Use MediaWikiServices::getInstance()->getEventRelayerGroup()
26 * @return EventRelayerGroup
27 */
28 public static function singleton() {
29 return MediaWikiServices::getInstance()->getEventRelayerGroup();
30 }
31
32 /**
33 * @param string $channel
34 * @return EventRelayer Relayer instance that handles the given channel
35 */
36 public function getRelayer( $channel ) {
37 $channelKey = isset( $this->configByChannel[$channel] )
38 ? $channel
39 : 'default';
40
41 if ( !isset( $this->relayers[$channelKey] ) ) {
42 if ( !isset( $this->configByChannel[$channelKey] ) ) {
43 throw new UnexpectedValueException( "No config for '$channelKey'" );
44 }
45
46 $config = $this->configByChannel[$channelKey];
47 $class = $config['class'];
48
49 $this->relayers[$channelKey] = new $class( $config );
50 }
51
52 return $this->relayers[$channelKey];
53 }
54 }