summaryrefslogtreecommitdiff
path: root/plugins/Irc/extlib/phergie/Phergie/Process/Abstract.php
blob: 68d45289e8587a9f897a4aaa65111d9c64b877f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
/**
 * Phergie 
 *
 * PHP version 5
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.
 * It is also available through the world-wide-web at this URL:
 * http://phergie.org/license
 *
 * @category  Phergie 
 * @package   Phergie
 * @author    Phergie Development Team <team@phergie.org>
 * @copyright 2008-2010 Phergie Development Team (http://phergie.org)
 * @license   http://phergie.org/license New BSD License
 * @link      http://pear.phergie.org/package/Phergie
 */

/**
 * Base class for obtaining and processing incoming events.
 *
 * @category Phergie 
 * @package  Phergie
 * @author   Phergie Development Team <team@phergie.org>
 * @license  http://phergie.org/license New BSD License
 * @link     http://pear.phergie.org/package/Phergie
 */
abstract class Phergie_Process_Abstract
{
    /**
     * Current driver instance
     *
     * @var Phergie_Driver_Abstract
     */
    protected $driver;

    /**
     * Current connection handler instance 
     *
     * @var Phergie_Connection_Handler 
     */
    protected $connections;

    /**
     * Current plugin handler instance
     *
     * @var Phergie_Plugin_Handler
     */
    protected $plugins;

    /**
     * Current event handler instance
     *
     * @var Phergie_Event_Handler
     */
    protected $events;

    /**
     * Current end-user interface instance
     *
     * @var Phergie_Ui_Abstract
     */
    protected $ui;

    /**
     * List of arguments for use within the instance
     *
     * @var array
     */
    protected $options = array();

    /**
     * Gets the required class refences from Phergie_Bot.
     *
     * @param Phergie_Bot $bot     Current bot instance in use 
     * @param array       $options Optional processor arguments
     *      
     * @return void
     */
    public function __construct(Phergie_Bot $bot, array $options = array())
    {
        $this->driver = $bot->getDriver();
        $this->plugins = $bot->getPluginHandler();
        $this->connections = $bot->getConnectionHandler();
        $this->events = $bot->getEventHandler();
        $this->ui = $bot->getUi();
        $this->options = $options;
    }

    /**
     * Sends resulting outgoing events from ealier processing in handleEvents.
     *
     * @param Phergie_Connection $connection Active connection
     *
     * @return void
     */
    protected function processEvents(Phergie_Connection $connection)
    {
        if (count($this->events)) {
            $this->plugins->preDispatch();
            foreach ($this->events as $event) {
                $this->ui->onCommand($event, $connection);

                $method = 'do' . ucfirst(strtolower($event->getType()));
                call_user_func_array(
                    array($this->driver, $method),
                    $event->getArguments()
                );
            }
            $this->plugins->postDispatch();

            if ($this->events->hasEventOfType(Phergie_Event_Request::TYPE_QUIT)) {
                $this->ui->onQuit($connection);
                $this->connections->removeConnection($connection);
            }

            $this->events->clearEvents();
        }
    }

    /**
     * Obtains and processes incoming events.
     *
     * @return void
     */
    public abstract function handleEvents();
}