承接 scriptmancer/ulak 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

scriptmancer/ulak

最新稳定版本:v1.0.0

Composer 安装命令:

composer require scriptmancer/ulak

包简介

A modern, attribute-based PHP event system with Result pattern for clean, type-safe event handling

README 文档

README

A modern, attribute-based PHP event system with Result pattern for clean, type-safe event handling.

Features

  • 🎯 Attribute-based event registration with PHP 8.1+ attributes
  • 🔄 Clean Success/Failure result pattern with explicit error handling
  • 🌟 Async event support with Redis integration
  • 🎭 Framework agnostic design
  • 🌳 PSR-compatible interfaces
  • 🔍 Type-safe event handling with union types
  • ⚡ High performance with attribute caching
  • 🛡️ Modern PHP 8.1+ features

Requirements

  • PHP 8.1 or higher
  • Composer
  • Redis (for async examples)

Installation

composer require scriptmancer/ulak

Quick Start

1. Basic Event Handling

use Ulak\Attributes\Event;
use Ulak\Results\{Success, Failure};

class UserService
{
    #[Event('user.registered')]
    #[Event('user.failed', on: 'failure')]
    public function register(array $data): Success|Failure
    {
        if (!$this->validate($data)) {
            return new Failure(new ValidationException('Invalid data'));
        }

        // Registration logic
        return new Success([
            'id' => $userId,
            'email' => $data['email']
        ]);
    }
}

2. Async Event Processing

use Ulak\Results\AsyncSuccess;

class FileService
{
    #[Event('file.queued', on: 'async')]
    public function processLargeFile(File $file): AsyncSuccess|Failure
    {
        $jobId = uniqid('job-');
        return new AsyncSuccess($file, $jobId)
            ->setProgress(0);
    }
}

3. Event Listeners

use Ulak\EventDispatcher;

$dispatcher = new EventDispatcher();

// Add a simple listener
$dispatcher->addListener('user.registered', function(string $event, array $data) {
    // Handle user registration
});

// Add a high-priority listener
$dispatcher->addListener('user.registered', 
    function(string $event, array $data) {
        // Handle with high priority
    },
    EventDispatcher::PRIORITY_HIGH
);

Examples

The repository includes several examples demonstrating different features:

  • Basic console event handling
  • Async event processing with Redis
  • Web-based event handling
  • Progress tracking
  • Event logging

To run the examples:

# Console examples
cd examples/console/basic
php run.php

# Web examples
cd examples/web
php -S localhost:8080

Development Setup

  1. Clone the repository:
git clone https://github.com/scriptmancer/ulak.git
cd ulak
  1. Install dependencies:
composer install
  1. Run tests:
composer test

Testing

The project uses Pest PHP for testing:

# Run all tests
composer test

# Run with coverage
composer test:coverage

Contributing

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

License

MIT License. See LICENSE for more information.

统计信息

  • 总下载量: 8
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-02-18