Skip to content

Commit ad52cd9

Browse files
authored
Merge pull request #1 from inhere/dev
Dev
2 parents 73c0ed0 + f231764 commit ad52cd9

21 files changed

+876
-520
lines changed

README.md

+238-73
Large diffs are not rendered by default.

examples/App.php

+6-9
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
namespace Inhere\Event\Examples;
1010

1111
use Inhere\Event\Event;
12-
use Inhere\Event\EventAwareTrait;
12+
use Inhere\Event\EventManagerAwareTrait;
1313
use Inhere\Event\EventManager;
1414

1515
/**
@@ -23,7 +23,7 @@ class App
2323
const ON_BEFORE_REQUEST = 'app.beforeRequest';
2424
const ON_AFTER_REQUEST = 'app.afterRequest';
2525

26-
use EventAwareTrait;
26+
use EventManagerAwareTrait;
2727

2828
public function __construct(EventManager $em)
2929
{
@@ -36,18 +36,15 @@ public function __construct(EventManager $em)
3636

3737
public function run()
3838
{
39-
$this->eventManager->trigger(self::ON_BEFORE_REQUEST, new Event('beforeRequest'));
40-
4139
$sleep = 0;
40+
$this->eventManager->trigger(self::ON_BEFORE_REQUEST, new Event('beforeRequest'));
4241

43-
echo 'handling ';
44-
45-
while ($sleep <= 5) {
42+
echo 'request handling ';
43+
while ($sleep <= 3) {
4644
$sleep++;
4745
echo '.';
4846
sleep(1);
4947
}
50-
5148
echo "\n";
5249

5350
$this->eventManager->trigger(self::ON_AFTER_REQUEST, new Event('afterRequest'));
@@ -59,4 +56,4 @@ public function __destruct()
5956
'key1' => 'val1'
6057
]));
6158
}
62-
}
59+
}

examples/AppListener.php

+10-4
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,30 @@ class AppListener
1919
public function start(EventInterface $event)
2020
{
2121
$pos = __METHOD__;
22-
echo "handle the event {$event->getName()} on the: $pos\n";
22+
echo "handle the event '{$event->getName()}' on the: $pos\n";
2323
}
2424

2525
public function beforeRequest(EventInterface $event)
2626
{
2727
$pos = __METHOD__;
28-
echo "handle the event {$event->getName()} on the: $pos\n";
28+
echo "handle the event '{$event->getName()}' on the: $pos\n";
2929
}
3030

3131
public function afterRequest(EventInterface $event)
3232
{
3333
$pos = __METHOD__;
34-
echo "handle the event {$event->getName()} on the: $pos\n";
34+
echo "handle the event '{$event->getName()}' on the: $pos\n";
3535
}
3636

3737
public function stop(EventInterface $event)
3838
{
3939
$pos = __METHOD__;
40-
echo "handle the event {$event->getName()} on the: $pos\n";
40+
echo "handle the event '{$event->getName()}' on the: $pos\n";
41+
}
42+
43+
public function allEvent(EventInterface $event)
44+
{
45+
$pos = __METHOD__;
46+
echo "handle the event '{$event->getName()}' on the: $pos\n";
4147
}
4248
}

examples/EnumGroupListener.php

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: Inhere
5+
* Date: 2017/12/14 0014
6+
* Time: 22:25
7+
*/
8+
9+
namespace Inhere\Event\Examples;
10+
11+
12+
use Inhere\Event\EventInterface;
13+
use Inhere\Event\EventSubscriberInterface;
14+
use Inhere\Event\ListenerPriority;
15+
16+
/**
17+
* Class EnumGroupListener
18+
* @package Inhere\Event\Examples
19+
*/
20+
class EnumGroupListener implements EventSubscriberInterface
21+
{
22+
const TEST_EVENT = 'test';
23+
const POST_EVENT = 'post';
24+
25+
/**
26+
* 配置事件与对应的处理方法
27+
* @return array
28+
*/
29+
public static function getSubscribedEvents(): array
30+
{
31+
return [
32+
self::TEST_EVENT => 'onTest',
33+
self::POST_EVENT => ['onPost', ListenerPriority::LOW],
34+
];
35+
}
36+
37+
public function onTest(EventInterface $event)
38+
{
39+
$pos = __METHOD__;
40+
echo "handle the event {$event->getName()} on the: $pos\n";
41+
}
42+
43+
public function onPost(EventInterface $event)
44+
{
45+
$pos = __METHOD__;
46+
echo "handle the event {$event->getName()} on the: $pos\n";
47+
}
48+
}

examples/ExamHandler.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@
99
namespace Inhere\Event\Examples;
1010

1111
use Inhere\Event\EventInterface;
12-
use Inhere\Event\HandlerInterface;
12+
use Inhere\Event\EventHandlerInterface;
1313

1414
/**
1515
* Class SingleListener
1616
* @package Inhere\Event
1717
*/
18-
class ExamHandler implements HandlerInterface
18+
class ExamHandler implements EventHandlerInterface
1919
{
2020
/**
2121
* @param EventInterface $event
@@ -24,7 +24,7 @@ class ExamHandler implements HandlerInterface
2424
public function handle(EventInterface $event)
2525
{
2626
$pos = __METHOD__;
27-
echo "handle the event {$event->getName()} on the: $pos\n";
27+
echo "handle the event '{$event->getName()}' on the: $pos\n";
2828

2929
return true;
3030
}

examples/exam.php examples/demo.php

+17-12
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33

44
use Inhere\Event\Event;
5-
use Inhere\Event\EventAwareTrait;
5+
use Inhere\Event\EventManagerAwareTrait;
66
use Inhere\Event\EventInterface;
77
use Inhere\Event\EventManager;
88
use Inhere\Event\Examples\ExamHandler;
@@ -12,7 +12,7 @@
1212
function exam_handler(EventInterface $event)
1313
{
1414
$pos = __METHOD__;
15-
echo "handle the event {$event->getName()} on the: $pos \n";
15+
echo "handle the event '{$event->getName()}' on the: $pos \n";
1616
}
1717

1818
class ExamListener1
@@ -21,60 +21,65 @@ public function messageSent(EventInterface $event)
2121
{
2222
$pos = __METHOD__;
2323

24-
echo "handle the event {$event->getName()} on the: $pos \n";
24+
echo "handle the event '{$event->getName()}' on the: $pos \n";
2525
}
2626
}
2727

2828
class ExamListener2
2929
{
3030
public function __invoke(EventInterface $event)
3131
{
32+
// $event->stopPropagation(true);
3233
$pos = __METHOD__;
33-
echo "handle the event {$event->getName()} on the: $pos\n";
34+
echo "handle the event '{$event->getName()}' on the: $pos\n";
3435
}
3536
}
3637

3738
// create event class
3839
class MessageEvent extends Event
3940
{
40-
protected $name = 'messageSent';
41-
4241
// append property ...
4342
public $message = 'oo a text';
4443
}
4544

4645
class Mailer
4746
{
48-
use EventAwareTrait;
47+
use EventManagerAwareTrait;
4948

5049
const EVENT_MESSAGE_SENT = 'messageSent';
5150

5251
public function send($message)
5352
{
5453
// ...发送 $message 的逻辑...
5554

56-
$event = new MessageEvent;
55+
$event = new MessageEvent(self::EVENT_MESSAGE_SENT);
5756
$event->message = $message;
5857

5958
// trigger event
60-
$this->eventManager->trigger(self::EVENT_MESSAGE_SENT, $event);
59+
$this->eventManager->trigger($event);
6160

6261
// var_dump($event);
6362
}
6463
}
6564

6665
$em = new EventManager();
66+
6767
$em->attach(Mailer::EVENT_MESSAGE_SENT, 'exam_handler');
6868
$em->attach(Mailer::EVENT_MESSAGE_SENT, function (EventInterface $event)
6969
{
7070
$pos = __METHOD__;
71-
echo "handle the event {$event->getName()} on the: $pos\n";
71+
echo "handle the event '{$event->getName()}' on the: $pos\n";
7272
});
73-
$em->attach(Mailer::EVENT_MESSAGE_SENT, new ExamListener1());
73+
$em->attach(Mailer::EVENT_MESSAGE_SENT, new ExamListener1(), 10);
7474
$em->attach(Mailer::EVENT_MESSAGE_SENT, new ExamListener2());
7575
$em->attach(Mailer::EVENT_MESSAGE_SENT, new ExamHandler());
7676

77+
$em->attach('*', function (EventInterface $event)
78+
{
79+
echo "handle the event '{$event->getName()}' on the global listener.\n";
80+
});
81+
7782
$mailer = new Mailer();
7883
$mailer->setEventManager($em);
7984

80-
$mailer->send('hello, world!');
85+
$mailer->send('hello, world!');

examples/enum-group.php

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: inhere
5+
* Date: 2017-12-07
6+
* Time: 14:07
7+
*/
8+
9+
use Inhere\Event\EventManager;
10+
use Inhere\Event\Examples\EnumGroupListener;
11+
12+
require dirname(__DIR__) . '/tests/boot.php';
13+
14+
$em = new EventManager();
15+
16+
// register a group listener
17+
$em->addListener(new EnumGroupListener());
18+
19+
$demo = new class
20+
{
21+
use \Inhere\Event\EventManagerAwareTrait;
22+
23+
public function run()
24+
{
25+
$this->eventManager->trigger(EnumGroupListener::TEST_EVENT);
26+
27+
echo '.';
28+
sleep(1);
29+
echo ".\n";
30+
31+
$this->eventManager->trigger(EnumGroupListener::POST_EVENT);
32+
}
33+
};
34+
35+
$demo->setEventManager($em);
36+
$demo->run();

examples/group.php examples/named-group.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,13 @@
1414

1515
$em = new EventManager();
1616

17+
$groupListener = new AppListener();
18+
1719
// register a group listener
18-
$em->attach('app', new AppListener());
20+
$em->attach('app', $groupListener);
21+
22+
// all `app.` prefix events will be handled by `AppListener::allEvent()`
23+
$em->attach('app.*', [$groupListener, 'allEvent']);
1924

2025
// create app
2126
$app = new App($em);

src/ClassEvent.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public static function on($class, $event, callable $handler)
4444

4545
/**
4646
* trigger event
47-
* @param $event
47+
* @param string $event
4848
* @param array $args
4949
* @return bool
5050
*/
@@ -57,7 +57,7 @@ public static function fire($event, array $args = [])
5757
// call event handlers of the event.
5858
foreach ((array)self::$events[$event] as $cb) {
5959
// return FALSE to stop go on handle.
60-
if (false === \call_user_func_array($cb, $args)) {
60+
if (false === $cb(...$args)) {
6161
break;
6262
}
6363
}

src/Event.php

+11-20
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,21 @@
44
* User: inhere
55
* Date: 16/8/27
66
* Time: 下午12:34
7-
* reference windwalker https://github.com/ventoviro/windwalker
7+
* @link windwalker https://github.com/ventoviro/windwalker
88
*/
99

1010
namespace Inhere\Event;
1111

12-
//use Inhere\Library\StdObject;
13-
1412
/**
1513
* Class Event
16-
* @package Inhere\LibraryPlus\Event
14+
* @package Inhere\Event
1715
*/
1816
class Event implements EventInterface, \ArrayAccess, \Serializable
1917
{
20-
/**
21-
* @var string 当前的事件名称
22-
*/
18+
/** @var string Event name */
2319
protected $name;
2420

25-
/**
26-
* 参数
27-
* @var array
28-
*/
21+
/** @var array Event params */
2922
protected $params = [];
3023

3124
/**
@@ -50,7 +43,9 @@ public function __construct($name = null, array $params = [])
5043
$this->setName($name);
5144
}
5245

53-
$this->params = $params;
46+
if ($params) {
47+
$this->params = $params;
48+
}
5449
}
5550

5651
/**
@@ -60,14 +55,10 @@ public function __construct($name = null, array $params = [])
6055
*/
6156
public static function checkName(string $name)
6257
{
63-
$name = trim($name);
64-
65-
if (!$name || \strlen($name) > 50) {
66-
throw new \InvalidArgumentException('Set up the name can be a not empty string of not more than 50 characters!');
67-
}
58+
$name = trim($name, '. ');
6859

69-
if (!preg_match('/^\w[\w-.]{1,56}$/i', $name)) {
70-
throw new \InvalidArgumentException("The service Id[$name] is invalid string");
60+
if (!$name || \strlen($name) > 64) {
61+
throw new \InvalidArgumentException('Set up the name can be a not empty string of not more than 64 characters!');
7162
}
7263

7364
return $name;
@@ -91,7 +82,7 @@ public function setName($name)
9182
}
9283

9384
/**
94-
* set all param
85+
* set all params
9586
* @param array $params
9687
*/
9788
public function setParams(array $params)

0 commit comments

Comments
 (0)