Erebot  latest
A modular IRC bot for PHP 7.0+
Network.php
1 <?php
2 /*
3  This file is part of Erebot, a modular IRC bot written in PHP.
4 
5  Copyright © 2010 François Poirotte
6 
7  Erebot is free software: you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation, either version 3 of the License, or
10  (at your option) any later version.
11 
12  Erebot is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with Erebot. If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 namespace Erebot\Config;
22 
33 class Network extends \Erebot\Config\Proxy implements \Erebot\Interfaces\Config\Network
34 {
36  protected $maincfg;
37 
39  protected $name;
40 
42  protected $servers;
43 
45  protected $channels;
46 
57  public function __construct(
58  \Erebot\Interfaces\Config\Main $mainCfg,
59  \SimpleXMLElement $xml
60  ) {
61  parent::__construct($mainCfg, $xml);
62  $this->maincfg = $mainCfg;
63  $this->servers = array();
64  $this->channels = array();
65  $this->name = (string) $xml['name'];
66 
67  foreach ($xml->servers->server as $serverCfg) {
69  $newConfig = new \Erebot\Config\Server($this, $serverCfg);
70  $uris = $newConfig->getConnectionURI();
71  $uri = new \Erebot\URI($uris[count($uris) - 1]);
72  $this->servers[(string) $uri] = $newConfig;
73  unset($newConfig);
74  }
75 
76  if (isset($xml->channels->channel)) {
77  foreach ($xml->channels->channel as $channelCfg) {
79  $newConfig = new \Erebot\Config\Channel($this, $channelCfg);
80  $this->channels[$newConfig->getName()] = $newConfig;
81  unset($newConfig);
82  }
83  }
84  }
85 
89  public function __destruct()
90  {
91  unset(
92  $this->servers,
93  $this->maincfg
94  );
95  parent::__destruct();
96  }
97 
99  public function getName()
100  {
101  return $this->name;
102  }
103 
105  public function getServerCfg($server)
106  {
107  if (!isset($this->servers[$server])) {
108  throw new \Erebot\NotFoundException('No such server');
109  }
110  return $this->servers[$server];
111  }
112 
114  public function getServers()
115  {
116  return $this->servers;
117  }
118 
120  public function getChannelCfg($channel)
121  {
122  if (!isset($this->channels[$channel])) {
123  throw new \Erebot\NotFoundException('No such channel');
124  }
125  return $this->channels[$channel];
126  }
127 
129  public function getChannels()
130  {
131  return $this->channels;
132  }
133 }
$maincfg
Main configuration this object depends on.
Definition: Network.php:36
__construct(\Erebot\Interfaces\Config\Main $mainCfg,\SimpleXMLElement $xml)
Definition: Network.php:57
This class contains the configuration for an IRC network.
Definition: Network.php:33
$name
The name of this IRC network.
Definition: Network.php:39
$channels
A list of channel configurations which apply to this network.
Definition: Network.php:45
getServerCfg($server)
Definition: Network.php:105
getChannelCfg($channel)
Definition: Network.php:120
Contains the main (general) configuration for Erebot.
Definition: Main.php:31
$servers
A list of server configurations which apply to this network.
Definition: Network.php:42
A configuration proxy which cascades settings.
Definition: Proxy.php:37