9 * This source file is subject to the new BSD license that is bundled
10 * with this package in the file LICENSE.
11 * It is also available through the world-wide-web at this URL:
12 * http://phergie.org/license
16 * @author Phergie Development Team <team@phergie.org>
17 * @copyright 2008-2010 Phergie Development Team (http://phergie.org)
18 * @license http://phergie.org/license New BSD License
19 * @link http://pear.phergie.org/package/Phergie
23 * Composite class for other components to represent the bot.
27 * @author Phergie Development Team <team@phergie.org>
28 * @license http://phergie.org/license New BSD License
29 * @link http://pear.phergie.org/package/Phergie
34 * Current version of Phergie
36 const VERSION = '2.0.1';
39 * Current driver instance
41 * @var Phergie_Driver_Abstract
46 * Current configuration instance
53 * Current connection handler instance
55 * @var Phergie_Connection_Handler
57 protected $connections;
60 * Current plugin handler instance
62 * @var Phergie_Plugin_Handler
67 * Current event handler instance
69 * @var Phergie_Event_Handler
74 * Current end-user interface instance
76 * @var Phergie_Ui_Abstract
81 * Current processor instance
83 * @var Phergie_Process_Abstract
88 * Returns a driver instance, creating one of the default class if
91 * @return Phergie_Driver_Abstract
93 public function getDriver()
95 if (empty($this->driver)) {
96 // Check if a driver has been defined in the configuration to use
98 $config = $this->getConfig();
99 if (isset($config['driver'])) {
100 $class = 'Phergie_Driver_' . ucfirst($config['driver']);
102 // Otherwise default to the Streams driver.
103 $class = 'Phergie_Driver_Streams';
106 $this->driver = new $class;
108 return $this->driver;
112 * Sets the driver instance to use.
114 * @param Phergie_Driver_Abstract $driver Driver instance
116 * @return Phergie_Bot Provides a fluent interface
118 public function setDriver(Phergie_Driver_Abstract $driver)
120 $this->driver = $driver;
125 * Sets the configuration to use.
127 * @param Phergie_Config $config Configuration instance
129 * @return Phergie_Runner_Abstract Provides a fluent interface
131 public function setConfig(Phergie_Config $config)
133 $this->config = $config;
138 * Returns the entire configuration in use or the value of a specific
139 * configuration setting.
141 * @param string $index Optional index of a specific configuration
142 * setting for which the corresponding value should be returned
143 * @param mixed $default Value to return if no match is found for $index
145 * @return mixed Value corresponding to $index or the entire
146 * configuration if $index is not specified
148 public function getConfig($index = null, $default = null)
150 if (empty($this->config)) {
151 $this->config = new Phergie_Config;
152 $this->config->read('Settings.php');
154 if ($index !== null) {
155 if (isset($this->config[$index])) {
156 return $this->config[$index];
161 return $this->config;
165 * Returns a plugin handler instance, creating it if it does not already
166 * exist and using a default class if none has been set.
168 * @return Phergie_Plugin_Handler
170 public function getPluginHandler()
172 if (empty($this->plugins)) {
173 $this->plugins = new Phergie_Plugin_Handler(
175 $this->getEventHandler()
178 return $this->plugins;
182 * Sets the plugin handler instance to use.
184 * @param Phergie_Plugin_Handler $handler Plugin handler instance
186 * @return Phergie_Bot Provides a fluent interface
188 public function setPluginHandler(Phergie_Plugin_Handler $handler)
190 $this->plugins = $handler;
195 * Returns an event handler instance, creating it if it does not already
196 * exist and using a default class if none has been set.
198 * @return Phergie_Event_Handler
200 public function getEventHandler()
202 if (empty($this->events)) {
203 $this->events = new Phergie_Event_Handler;
205 return $this->events;
209 * Sets the event handler instance to use.
211 * @param Phergie_Event_Handler $handler Event handler instance
213 * @return Phergie_Bot Provides a fluent interface
215 public function setEventHandler(Phergie_Event_Handler $handler)
217 $this->events = $handler;
222 * Returns a connection handler instance, creating it if it does not
223 * already exist and using a default class if none has been set.
225 * @return Phergie_Connection_Handler
227 public function getConnectionHandler()
229 if (empty($this->connections)) {
230 $this->connections = new Phergie_Connection_Handler;
232 return $this->connections;
236 * Sets the connection handler instance to use.
238 * @param Phergie_Connection_Handler $handler Connection handler instance
240 * @return Phergie_Bot Provides a fluent interface
242 public function setConnectionHandler(Phergie_Connection_Handler $handler)
244 $this->connections = $handler;
249 * Returns an end-user interface instance, creating it if it does not
250 * already exist and using a default class if none has been set.
252 * @return Phergie_Ui_Abstract
254 public function getUi()
256 if (empty($this->ui)) {
257 $this->ui = new Phergie_Ui_Console;
263 * Sets the end-user interface instance to use.
265 * @param Phergie_Ui_Abstract $ui End-user interface instance
267 * @return Phergie_Bot Provides a fluent interface
269 public function setUi(Phergie_Ui_Abstract $ui)
276 * Returns a processer instance, creating one if none exists.
278 * @return Phergie_Process_Abstract
280 public function getProcessor()
282 if (empty($this->processor)) {
283 $class = 'Phergie_Process_Standard';
285 $type = $this->getConfig('processor');
287 $class = 'Phergie_Process_' . ucfirst($type);
290 $this->processor = new $class(
292 $this->getConfig('processor.options', array())
295 return $this->processor;
299 * Sets the processer instance to use.
301 * @param Phergie_Process_Abstract $processor Processer instance
303 * @return Phergie_Bot Provides a fluent interface
305 public function setProcessor(Phergie_Process_Abstract $processor)
307 $this->processor = $processor;
312 * Loads plugins into the plugin handler.
316 protected function loadPlugins()
318 $config = $this->getConfig();
319 $plugins = $this->getPluginHandler();
320 $ui = $this->getUi();
322 $plugins->setAutoload($config['plugins.autoload']);
323 foreach ($config['plugins'] as $name) {
325 $plugin = $plugins->addPlugin($name);
326 $ui->onPluginLoad($name);
327 } catch (Phergie_Plugin_Exception $e) {
328 $ui->onPluginFailure($name, $e->getMessage());
329 if (!empty($plugin)) {
330 $plugins->removePlugin($plugin);
337 * Configures and establishes connections to IRC servers.
341 protected function loadConnections()
343 $config = $this->getConfig();
344 $driver = $this->getDriver();
345 $connections = $this->getConnectionHandler();
346 $plugins = $this->getPluginHandler();
347 $ui = $this->getUi();
349 foreach ($config['connections'] as $data) {
350 $connection = new Phergie_Connection($data);
351 $connections->addConnection($connection);
353 $ui->onConnect($data['host']);
354 $driver->setConnection($connection)->doConnect();
355 $plugins->setConnection($connection);
356 $plugins->onConnect();
361 * Establishes server connections and initiates an execution loop to
362 * continuously receive and process events.
364 * @return Phergie_Bot Provides a fluent interface
366 public function run()
370 $timezone = $this->getConfig('timezone', 'UTC');
371 date_default_timezone_set($timezone);
373 $ui = $this->getUi();
374 $ui->setEnabled($this->getConfig('ui.enabled'));
376 $this->loadPlugins();
377 $this->loadConnections();
379 $processor = $this->getProcessor();
381 $connections = $this->getConnectionHandler();
382 while (count($connections)) {
383 $processor->handleEvents();