Skip to content

Commit 45726de

Browse files
committed
First move to a Symfony based event system using EventDispatcher
1 parent 6461767 commit 45726de

File tree

9 files changed

+125
-8
lines changed

9 files changed

+125
-8
lines changed

.gitmodules

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "src/library/Symfony/Component/EventDispatcher"]
2+
path = src/library/Symfony/Component/EventDispatcher
3+
url = git://github.com/symfony/EventDispatcher.git

src/Application/Autoloader.php

+5
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ public function autoload($class)
2121
array_unshift($classPath, $this->applicationPath);
2222
if ($package == 'Ircbot') {
2323
require_once implode(DIRECTORY_SEPARATOR, $classPath) . '.php';
24+
} elseif ($package == 'Symfony') {
25+
$classPath = explode('\\', $class);
26+
$package = array_shift($classPath);
27+
require_once $this->applicationPath . '/library/Symfony/'
28+
. implode(DIRECTORY_SEPARATOR, $classPath) . '.php';
2429
}
2530
}
2631
}

src/Event/Irc/Message.php

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
namespace Ircbot\Event\Irc;
3+
4+
use Symfony\Component\EventDispatcher\Event;
5+
6+
class Message extends Event
7+
{
8+
9+
private $sender;
10+
private $target;
11+
private $message;
12+
13+
public function __construct($sender, $target, $message)
14+
{
15+
$this->sender = $sender;
16+
$this->target = $target;
17+
$this->message = $message;
18+
}
19+
20+
public function getSender()
21+
{
22+
return $this->sender;
23+
}
24+
25+
public function getTarget()
26+
{
27+
return $this->target;
28+
}
29+
30+
public function targetIsChannel()
31+
{
32+
return (substr($this->target, 0, 1) == '#');
33+
}
34+
35+
public function getMessage()
36+
{
37+
return $this->message;
38+
}
39+
40+
}

src/Event/Irc/Notice.php

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
namespace Ircbot\Event\Irc;
3+
4+
class Notice extends Message
5+
{
6+
7+
}

src/Event/Irc/Privmsg.php

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
namespace Ircbot\Event\Irc;
3+
4+
class Privmsg extends Message
5+
{
6+
7+
}

src/Event/Loop/Iterated.php

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
namespace Ircbot\Event\Loop;
3+
4+
use Symfony\Component\EventDispatcher\Event;
5+
6+
class Iterated extends Event
7+
{
8+
9+
private $tick;
10+
11+
public function __construct($tick)
12+
{
13+
$this->tick = $tick;
14+
}
15+
16+
public function getTick()
17+
{
18+
return $this->tick;
19+
}
20+
21+
}

src/Event/Loop/Started.php

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
namespace Ircbot\Event\Loop;
3+
4+
use Symfony\Component\EventDispatcher\Event;
5+
6+
class Started extends Event
7+
{
8+
9+
}

src/Handler/Events.php

+32-8
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
11
<?php
2-
/**
3-
* @category IRCBot
4-
* @package IRCBot_Handlers
5-
* @subpackage Events
6-
* @author Marlin Cremers <[email protected]>
7-
*/
8-
92
namespace Ircbot\Handler;
103

114
use \Ircbot\Application\Debug;
5+
use \Symfony\Component\EventDispatcher\Event;
126

137
/**
148
* The IRCBot event handler
@@ -17,7 +11,7 @@
1711
* you can simply raise any event by calling the raiseEvent method
1812
* or register a callback with any event by calling the addEventCallback method
1913
*/
20-
class Events
14+
class Events extends \Symfony\Component\EventDispatcher\EventDispatcher
2115
{
2216
/**
2317
* This variable contains all the callbacks registered with the events
@@ -42,6 +36,11 @@ public function raiseEvent($eventName, $data = null)
4236
\Ircbot\Application::getInstance()->getDebugger()->log(
4337
'Events', 'RaisedEvent', $eventName, Debug::LEVEL_INFO
4438
);
39+
\Ircbot\Application::getInstance()->getDebugger()->log(
40+
'Events', 'Event', 'WARNING - This method is deprecated! '
41+
. 'Use dispatch instead. (' . $eventName . ')',
42+
Debug::LEVEL_WARN
43+
);
4544
}
4645
foreach ($this->_callbacks as $callback) {
4746
if ($callback['eventName'] == $eventName) {
@@ -80,6 +79,31 @@ public function addEventCallback($eventName, $callback)
8079
'Events', 'AddCallback', $eventName . ' => ' . $callbackDisplay,
8180
Debug::LEVEL_DEBUG
8281
);
82+
\Ircbot\Application::getInstance()->getDebugger()->log(
83+
'Events', 'Callback', 'WARNING - This method is deprecated! '
84+
. 'Use addListener instead. (' . $eventName . ')',
85+
Debug::LEVEL_WARN
86+
);
8387
return $this;
8488
}
89+
90+
public function addListener($eventName, $listener, $priority = 0)
91+
{
92+
if (!$this->hasListeners($eventName)) {
93+
$this->dispatch('eventdispatcher.event_used.' . $eventName);
94+
}
95+
parent::addListener($eventName, $listener, $priority);
96+
}
97+
98+
protected function doDispatch($listeners, $eventName, Event $event)
99+
{
100+
if ($eventName != 'loop.iterated') {
101+
\Ircbot\Application::getInstance()->getDebugger()->log(
102+
'Symfony', 'EventDispatcher', 'Dispatched event ' . $eventName,
103+
Debug::LEVEL_INFO
104+
);
105+
}
106+
parent::doDispatch($listeners, $eventName, $event);
107+
}
108+
85109
}
Submodule EventDispatcher added at 0dc437f

0 commit comments

Comments
 (0)