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