Skip to content

Commit 1f72f7e

Browse files
committed
trigger 方法返回 bool 类型:是否执行了当前事件的监听器
1 parent f194a0a commit 1f72f7e

File tree

9 files changed

+176
-107
lines changed

9 files changed

+176
-107
lines changed

.coveralls.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
service_name: travis-ci
2+
coverage_clover: build/logs/clover.xml
3+
json_path: build/logs/coveralls-upload.json

.travis.yml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
dist: trusty
2+
sudo: false
3+
4+
language: php
5+
6+
env:
7+
global:
8+
- COMPOSER_ARGS="--ansi --prefer-dist --no-interaction --no-suggest --no-progress --optimize-autoloader"
9+
- DEPS="phpunit/phpunit squizlabs/php_codesniffer php-coveralls/php-coveralls"
10+
11+
git:
12+
depth: 3
13+
14+
cache:
15+
directories:
16+
- $HOME/.composer/cache
17+
18+
matrix:
19+
fast_finish: true
20+
allow_failures:
21+
- php: hhvm
22+
- php: nightly
23+
include:
24+
- php: 5.5
25+
- php: 5.6
26+
- php: 7.0
27+
- php: 7.1
28+
env: ANALYSIS="true"
29+
- php: 7.2
30+
- php: hhvm
31+
- php: nightly
32+
33+
before_script:
34+
- composer require --dev $COMPOSER_ARGS $DEPS
35+
36+
script:
37+
- php vendor/bin/phpunit
38+
- if [[ "$ANALYSIS" == "true" ]]; then php vendor/bin/phpcs ; fi
39+
40+
after_script:
41+
- if [[ "$ANALYSIS" == "true" && -f ./build/logs/clover.xml ]]; then php vendor/bin/php-coveralls -v; fi

README.md

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,18 @@
11
Soli Event Manager
22
------------------
33

4-
当前项目参考 [Phalcon 框架的事件管理器]实现
4+
事件管理器,管理事件的注册、删除和调度(触发)
55

6-
事件管理器的目的是为了通过创建"钩子"拦截框架或应用中的部分组件操作。
7-
8-
这些钩子允许开发者获得状态信息,操纵数据或者改变某个组件进程中的执行流向。
9-
10-
以上介绍摘自[Phalcon 框架的事件管理器]官方文档。
6+
[![Build Status](https://travis-ci.org/soliphp/events.svg?branch=master)](https://travis-ci.org/soliphp/events)
7+
[![Coverage Status](https://coveralls.io/repos/github/soliphp/events/badge.svg?branch=master)](https://coveralls.io/github/soliphp/events?branch=master)
8+
[![License](https://poser.pugx.org/soliphp/events/license)](https://packagist.org/packages/soliphp/events)
119

1210
## 安装
1311

1412
使用 `composer` 安装到你的项目:
1513

1614
composer require soliphp/events
1715

18-
## 命名约定
19-
20-
当前事件管理器的命名规则采用分组的方式,目的是为了避免不同组件间的事件名称重名,产生碰撞,
21-
同时也便于对项目中同一维度的事件进行聚合整理;事件命名格式为 "component.event",
22-
类比类的命名空间,我们暂且将这种对事件的命名方式称之为「事件命名空间」。
23-
24-
如,我们有一个 `\Soli\Application` 类,它的事件命名空间可以定义为 "application",
25-
对于此类 "boot" 事件的全名为 "application.boot"。
26-
2716
## 使用
2817

2918
### 针对具体的某个事件设置监听器
@@ -33,25 +22,25 @@ Soli Event Manager
3322

3423
$eventManager = new EventManager();
3524

36-
$eventManager->attach('application.boot', function (Event $event, $application) {
25+
$eventManager->attach('app.boot', function (Event $event, $app) {
3726
echo "应用已启动\n";
3827
});
3928

4029
`监听器的格式`,可以是 `匿名函数或对象实例`
4130

42-
如,我们这里定义一个 `AppEvents` 类用于处理针对 `Application` 类的事件:
31+
如,我们这里定义一个 `AppEvents` 类用于处理针对 `App` 类的事件:
4332

4433
class AppEvents
4534
{
46-
public function boot(Event $event, $application)
35+
public function boot(Event $event, $app)
4736
{
4837
// 导出内部(事件)数据或状态给外部(监听器)调用者
4938
$data = $event->getData();
5039

5140
echo "应用已启动\n";
5241
}
5342

54-
public function finish(Event $event, $application, $extraData)
43+
public function finish(Event $event, $app, $data)
5544
{
5645
echo "应用执行结束\n";
5746
}
@@ -60,21 +49,21 @@ Soli Event Manager
6049
// 注册事件监听
6150

6251
// 匿名函数
63-
$eventManager->attach('application.boot', function (Event $event, $application) {
64-
$ver = $application::VERSION;
52+
$eventManager->attach('app.boot', function (Event $event, $app) {
53+
$ver = $app::VERSION;
6554
echo "应用已启动 $ver\n";
6655
});
6756

6857
// 对象实例
69-
$eventManager->attach('application.boot', new AppEvents);
58+
$eventManager->attach('app.boot', new AppEvents);
7059

7160
### 聚合事件监听器到专门的事件类中进行处理
7261

7362
上面我们定义了 `AppEvents` 类,其中有两个方法 `boot``finish`
74-
这两个方法可以直接用来监听 `application.boot` 事件和 `application.finish` 事件,
63+
这两个方法可以直接用来监听 `app.boot` 事件和 `app.finish` 事件,
7564
注册方法很简单,如下:
7665

77-
$eventManager->attach('application', new AppEvents);
66+
$eventManager->attach('app', new AppEvents);
7867

7968
这样我们便可以很方便的注册和整理不同维度的不同事件。
8069

@@ -83,11 +72,11 @@ Soli Event Manager
8372
触发事件调用 `trigger` 方法,其参数为具体的某个事件名称,事件源(当前类),
8473
也可以传入更多整合后的数据,供监听器使用。
8574

86-
$eventManager->trigger('application.boot', $this, $extraData);
75+
$eventManager->trigger('app.boot', $this, $data);
8776

8877
### 事件传播
8978

90-
$eventManager->attach('application.boot', function (Event $event, $application) {
79+
$eventManager->attach('app.boot', function (Event $event, $app) {
9180
// 终止事件传播,这样其他的侦听器就不会再收到此事件通知
9281
$event->stopPropagation();
9382
});

phpunit.xml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
convertWarningsToExceptions="true"
88
processIsolation="false"
99
stopOnFailure="false"
10-
syntaxCheck="false"
1110
verbose="true"
1211
bootstrap="vendor/autoload.php"
1312
>
@@ -19,20 +18,20 @@
1918

2019
<testsuites>
2120
<testsuite name="Soli Test Suite">
22-
<directory suffix="Test.php">./tests/</directory>
21+
<directory suffix="Test.php">tests</directory>
2322
</testsuite>
2423
</testsuites>
2524

2625
<filter>
2726
<whitelist processUncoveredFilesFromWhitelist="true">
28-
<directory suffix=".php">./src/</directory>
27+
<directory suffix=".php">src</directory>
2928
</whitelist>
3029
</filter>
3130

3231
<logging>
3332
<log type="coverage-html" target="build/coverage" lowUpperBound="35" highLowerBound="70"/>
3433
<log type="coverage-clover" target="build/logs/clover.xml"/>
3534
<log type="coverage-crap4j" target="build/logs/crap4j.xml"/>
36-
<log type="junit" target="build/logs/junit.xml" logIncompleteSkipped="false"/>
35+
<log type="junit" target="build/logs/junit.xml"/>
3736
</logging>
3837
</phpunit>

src/Events/Event.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,7 @@
1212
class Event implements EventInterface
1313
{
1414
/**
15-
* 事件名称分隔符
16-
*/
17-
const DELIMITER = '.';
18-
19-
/**
20-
* 完整的事件名称,格式为 "事件空间.事件名称"
15+
* 事件名称
2116
*
2217
* @var string
2318
*/
@@ -75,7 +70,7 @@ public function getData()
7570

7671
public function setName($name)
7772
{
78-
if (!is_string($name) || !strpos($name, Event::DELIMITER)) {
73+
if (!is_string($name)) {
7974
throw new \InvalidArgumentException('Invalid event type ' . $name);
8075
}
8176
$this->name = $name;

0 commit comments

Comments
 (0)