PHPackages                             soliphp/events - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. soliphp/events

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

soliphp/events
==============

Soli Event Manager

v1.3.0(7y ago)31381MITPHPPHP &gt;=5.5.0

Since Mar 22Pushed 1y ago1 watchersCompare

[ Source](https://github.com/soliphp/events)[ Packagist](https://packagist.org/packages/soliphp/events)[ Docs](https://github.com/soliphp/events)[ RSS](/packages/soliphp-events/feed)WikiDiscussions master Synced 4w ago

READMEChangelog (4)DependenciesVersions (5)Used By (1)

Soli Event Manager
------------------

[](#soli-event-manager)

事件管理器，管理事件的注册、删除和调度(触发)。

[![Build Status](https://camo.githubusercontent.com/7f06d1d9736f300ecc5c3647540ffdcf84af070e83ff46c0ac1a9321f5f2ad6b/68747470733a2f2f7472617669732d63692e6f72672f736f6c697068702f6576656e74732e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/soliphp/events)[![Coverage Status](https://camo.githubusercontent.com/a0b9a4449757881ddbceb51d6bf839d6f2607debd5e3420ffd84015c3216ce7f/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f736f6c697068702f6576656e74732f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/soliphp/events?branch=master)[![License](https://camo.githubusercontent.com/ae8a61f166a8483f319385a48363baedf5a26c2ff8e1a7f93a9e84ce3d963556/68747470733a2f2f706f7365722e707567782e6f72672f736f6c697068702f6576656e74732f6c6963656e7365)](https://packagist.org/packages/soliphp/events)

安装
--

[](#安装)

使用 `composer` 安装到你的项目：

```
composer require soliphp/events

```

使用
--

[](#使用)

### 针对具体的某个事件设置监听器

[](#针对具体的某个事件设置监听器)

```
use Soli\Events\EventManager;
use Soli\Events\Event;

$eventManager = new EventManager();

$eventManager->attach('app.boot', function (Event $event, $app) {
    echo "应用已启动\n";
});

```

`监听器的格式`，可以是 `匿名函数或对象实例`。

如，我们这里定义一个 `AppEvents` 类用于处理针对 `App` 类的事件：

```
class AppEvents
{
    public function boot(Event $event, $app)
    {
        // 导出内部（事件）数据或状态给外部（监听器）调用者
        $data = $event->getData();

        echo "应用已启动\n";
    }

    public function finish(Event $event, $app, $data)
    {
        echo "应用执行结束\n";
    }
}

// 注册事件监听

// 匿名函数
$eventManager->attach('app.boot', function (Event $event, $app) {
    $ver = $app::VERSION;
    echo "应用已启动 $ver\n";
});

// 对象实例
$eventManager->attach('app.boot', new AppEvents);

```

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

[](#聚合事件监听器到专门的事件类中进行处理)

上面我们定义了 `AppEvents` 类，其中有两个方法 `boot` 和 `finish`， 这两个方法可以直接用来监听 `app.boot` 事件和 `app.finish` 事件， 注册方法很简单，如下：

```
$eventManager->attach('app', new AppEvents);

```

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

### 触发事件

[](#触发事件)

触发事件调用 `trigger` 方法，其参数为具体的某个事件名称，事件源（当前类）， 也可以传入更多整合后的数据，供监听器使用。

```
$eventManager->trigger('app.boot', $this, $data);

```

### 事件传播

[](#事件传播)

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

```

PSR-14
------

[](#psr-14)

`soliphp/events` 实现了绝大部分的 [PSR-14](https://github.com/php-fig/fig-standards/blob/master/proposed/event-manager.md) 的接口，但是与 [PSR-14](https://github.com/php-fig/fig-standards/blob/master/proposed/event-manager.md) 也有一些不同。

1. EventManagerInterface 的 clearListeners 接口，实现了但未写到 EventManagerInterface 接口文件中。

对比 clearListeners 的命名格式，添加和移除某个监听器的命名应为 addListener、removeListener， 而不应为 attach、detach，但是事实上我们更习惯使用 attach、detach 的命名，于是便搁置了 clearListeners 方法，暂时不进入 EventManagerInterface 接口文件。等待 [PSR-14](https://github.com/php-fig/fig-standards/blob/master/proposed/event-manager.md) 正式通过后，再做调整。

2. EventManagerInterface 的 trigger 接口实现

[PSR-14](https://github.com/php-fig/fig-standards/blob/master/proposed/event-manager.md) 上 trigger 接口方法为：

```
/**
 * Trigger an event
 *
 * Can accept an EventInterface or will create one if not passed
 *
 * @param  string|EventInterface $event
 * @param  object|string $target
 * @param  array|object $argv
 * @return mixed
 */
public function trigger($event, $target = null, $argv = array());

```

第三个参数 $argv 类型为 array|object，事实上这个参数类型可以更广泛 string/int 等。

故这里将 $argv 参数类型改为 mixed，默认值改为 null，且命名为 $data：

```
/**
 * @param mixed $data
 */
public function trigger($event, $target = null, $data = null);

```

3. EventInterface 使用 get/setData 替换 get/setParams、getParam

第2条说到 $data（即 $argv）的参数类型为 mixed，因此我们直接使用 get/setData 替换之；

且假如第2条 $argv 不做参数类型的更改，get/setParams、getParam 三个接口 并不能直接体现 $argv 为 object 时的情况，是选择 getParam() 还是 getParams() 方法获取 $argv 参数，会疑惑开发者 array 时用 getParams()，而 object 时用 getParam()，而事实上 getParam() 仅仅是获取参数类型为 array 时的单个元素， 那 object 是想怎么获取？这样的接口体验远不如直接使用 getData() 一个方法，交给开发者来处理具体的数据类型问题。

测试
--

[](#测试)

```
$ cd /path/to/soliphp/events/
$ composer install
$ phpunit

```

License
-------

[](#license)

MIT Public License

###  Health Score

32

—

LowBetter than 69% of packages

Maintenance34

Infrequent updates — may be unmaintained

Popularity14

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity61

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~197 days

Total

4

Last Release

2795d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/318253?v=4)[ueaner](/maintainers/ueaner)[@ueaner](https://github.com/ueaner)

---

Top Contributors

[![ueaner](https://avatars.githubusercontent.com/u/318253?v=4)](https://github.com/ueaner "ueaner (33 commits)")

---

Tags

eventevent-managerphpsolisoliphpeventeventmanagersoliphp

### Embed Badge

![Health badge](/badges/soliphp-events/health.svg)

```
[![Health](https://phpackages.com/badges/soliphp-events/health.svg)](https://phpackages.com/packages/soliphp-events)
```

###  Alternatives

[doctrine/event-manager

The Doctrine Event Manager is a simple PHP event system that was built to be used with the various Doctrine projects.

6.0k517.6M149](/packages/doctrine-event-manager)[laminas/laminas-eventmanager

Trigger and listen to events within a PHP application

1.0k71.7M251](/packages/laminas-laminas-eventmanager)[league/event

Event package

1.6k149.1M200](/packages/league-event)[winzou/state-machine

A very lightweight yet powerful PHP state machine

52413.9M18](/packages/winzou-state-machine)[spatie/laravel-event-sourcing

The easiest way to get started with event sourcing in Laravel

9094.1M28](/packages/spatie-laravel-event-sourcing)[spatie/laravel-google-calendar

Manage events on a Google Calendar

1.4k1.7M21](/packages/spatie-laravel-google-calendar)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
