vaibhavpandeyvpz/soochak
Composer 安装命令:
composer require vaibhavpandeyvpz/soochak
包简介
A simple but useful events manager based on PSR-14 for PHP >= 8.2.
README 文档
README
Soochak (
सूचक) - A modern, PSR-14 compliant event dispatcher for PHP 8.2+
Soochak is a lightweight, high-performance event management library that implements the PSR-14 Event Dispatcher standard. It provides a simple yet powerful API for implementing the observer pattern in your PHP applications.
Features
- ✅ PSR-14 Compliant - Full implementation of EventDispatcherInterface and ListenerProviderInterface
- ✅ Backward Compatible - Legacy API maintained for existing codebases
- ✅ Priority Support - Control listener execution order with priority queues
- ✅ Event Propagation - Stop event propagation when needed
- ✅ Type Safe - Full PHP 8.2+ type hints and union types
- ✅ Memory Efficient - Uses generators for lazy listener iteration
- ✅ 100% Test Coverage - Comprehensive test suite with 50+ tests
- ✅ Zero Dependencies - Only requires PSR-14 interfaces
Requirements
- PHP 8.2 or higher
- Composer
Installation
Install via Composer:
composer require vaibhavpandeyvpz/soochak
Quick Start
Basic Usage
<?php use Soochak\EventManager; use Soochak\Event; $em = new EventManager(); // Attach a listener to an event (using class name) $em->attach(Event::class, function (Event $event) { echo "Event dispatched!"; }); // Dispatch the event $em->dispatch(new Event());
Using PSR-14 Standard API
<?php use Soochak\EventManager; use Soochak\Event; $em = new EventManager(); $event = new Event(); // Attach listener $em->attach(Event::class, function (object $event) { // Handle the event }); // Dispatch using PSR-14 interface $em->dispatch($event);
Usage Examples
Priority-Based Listeners
Listeners with higher priority values are executed first:
$em = new EventManager(); // Lower priority (executed last) $em->attach(Event::class, function (Event $event) { echo "Sending email notification...\n"; }, 10); // Higher priority (executed first) $em->attach(Event::class, function (Event $event) { echo "Updating inventory...\n"; }, 20); $em->dispatch(new Event()); // Output: // Updating inventory... // Sending email notification...
Event Propagation Control
Stop further listeners from executing:
$em = new EventManager(); $em->attach(Event::class, function (Event $event) { $event->stopPropagation(true); echo "Validation failed, stopping propagation\n"; }); $em->attach(Event::class, function (Event $event) { echo "This won't execute if propagation was stopped\n"; }); $event = new Event(); $em->dispatch($event);
Working with Event Objects
use Soochak\Event; // Create an event $event = new Event(); // Stop propagation if needed $event->stopPropagation(true); // Check if propagation is stopped if ($event->isPropagationStopped()) { echo "Propagation is stopped\n"; } // Dispatch the event $em->dispatch($event);
Custom Event Objects
You can use any object as an event:
class UserRegisteredEvent { public function __construct( public readonly int $userId, public readonly string $email ) {} } $em = new EventManager(); // Attach listener using class name $em->attach(UserRegisteredEvent::class, function (UserRegisteredEvent $event) { echo "User {$event->userId} registered with email {$event->email}\n"; }); // Or attach using instance $event = new UserRegisteredEvent(123, 'user@example.com'); $em->attach($event, function (UserRegisteredEvent $e) { // Handle event }); // Dispatch $em->dispatch($event);
Managing Listeners
$em = new EventManager(); $listener = function (Event $event) { echo "Listener executed\n"; }; // Attach $em->attach(Event::class, $listener); // Detach $em->detach(Event::class, $listener); // Clear all listeners for an event $em->clear(Event::class);
Getting Listeners for an Event
$em = new EventManager(); $em->attach(Event::class, function () {}, 10); $em->attach(Event::class, function () {}, 20); $event = new Event(); $listeners = iterator_to_array($em->getListenersForEvent($event)); echo count($listeners); // 2
API Reference
EventManager
The main event manager class implementing PSR-14 interfaces.
Methods
attach(string|object $event, callable $callback, int $priority = 0): void- Attach a listener to an eventdetach(string|object $event, callable $callback): bool- Remove a specific listenerclear(string|object $event): void- Clear all listeners for an eventdispatch(object $event): object- Dispatch an event (PSR-14)getListenersForEvent(object $event): iterable- Get all listeners for an event (PSR-14)
Event
Minimal event implementation implementing StoppableEventInterface.
Methods
isPropagationStopped(): bool- Check if propagation is stoppedstopPropagation(bool $flag = true): void- Stop event propagation
PSR-14 Compliance
Soochak fully implements the PSR-14 Event Dispatcher standard:
- ✅
Psr\EventDispatcher\EventDispatcherInterface - ✅
Psr\EventDispatcher\ListenerProviderInterface - ✅
Psr\EventDispatcher\StoppableEventInterface(via Event class)
You can use Soochak with any PSR-14 compatible library or framework.
Testing
The project includes a comprehensive test suite with 100% code coverage:
# Run tests vendor/bin/phpunit # Run with coverage XDEBUG_MODE=coverage vendor/bin/phpunit --coverage-text # Generate HTML coverage report XDEBUG_MODE=coverage vendor/bin/phpunit --coverage-html coverage
Performance
Soochak is designed for performance:
- Lazy Evaluation - Uses generators to avoid loading all listeners into memory
- Direct Callbacks - Uses direct function calls instead of
call_user_func() - Efficient Queues - Priority queues with FIFO ordering for same-priority listeners
- Minimal Overhead - Zero dependencies beyond PSR-14 interfaces
Architecture
Components
- EventManager - Main dispatcher implementing PSR-14 interfaces, manages listener registration and retrieval
- Event - Standard event implementation
- EventListenerQueue - Priority queue for listener ordering
Design Patterns
- Observer Pattern - Event/listener architecture
- Strategy Pattern - Pluggable listener providers
- Priority Queue - Efficient listener ordering
License
This project is licensed under the MIT License - see the LICENSE file for details.
Author
Vaibhav Pandey
- Email: contact@vaibhavpandey.com
- Homepage: https://github.com/vaibhavpandeyvpz/soochak
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Changelog
Version 2.0.0 (Current)
- ✅ Upgraded to PHP 8.2+
- ✅ Full PSR-14 compliance
- ✅ Modern type hints and union types
- ✅ 100% test coverage
- ✅ Performance optimizations
- ✅ Comprehensive documentation
Version 1.0.0 (Legacy)
- Initial release with PHP 5.3+ support
- Basic event management functionality
Soochak - Simple, powerful, and standards-compliant event management for PHP.
vaibhavpandeyvpz/soochak 适用场景与选型建议
vaibhavpandeyvpz/soochak 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 54 次下载、GitHub Stars 达 2, 最近一次更新时间为 2016 年 12 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「event-dispatcher」 「php」 「manager」 「events」 「psr-14」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 vaibhavpandeyvpz/soochak 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 vaibhavpandeyvpz/soochak 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 vaibhavpandeyvpz/soochak 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
The file manager intended for using Laravel with CKEditor / TinyMCE / Colorbox
Interfaces, Traits and Nominal Classes used by Domain Entities Implementing Domain Events
Domain Event Implementation for Symfony and Doctrine
Implementation of the transactional outbox pattern on top of rekalogika/domain-event
PHP 5.2 port of Événement
KCFinder web file manager
统计信息
- 总下载量: 54
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 4
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-12-24