承接 phpdot/container-swoole 相关项目开发

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

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

phpdot/container-swoole

最新稳定版本:v1.2.0

Composer 安装命令:

composer require phpdot/container-swoole

包简介

Swoole context provider for phpdot/container

README 文档

README

Swoole adapter for phpdot/container.

Each Swoole coroutine gets its own isolated service scope via Coroutine::getContext(). When the coroutine exits, Swoole destroys the context automatically — no manual cleanup required.

Installation

composer require phpdot/container-swoole

Usage

use PHPdot\Container\ContainerBuilder;
use PHPdot\Container\Swoole\SwooleContextProvider;
use function PHPdot\Container\singleton;
use function PHPdot\Container\scoped;

$container = (new ContainerBuilder())
    ->withContextProvider(new SwooleContextProvider())
    ->addDefinitions([
        // Shared across all coroutines
        Router::class  => singleton(),
        Redis::class   => singleton(),

        // Isolated per coroutine — fresh for each request
        Session::class       => scoped(),
        SignalManager::class => scoped(),
    ])
    ->build();

How It Works

┌──────────────────────────────────────────────────────────┐
│ Swoole Worker                                            │
│                                                          │
│  Singletons (shared)                                     │
│  ┌────────────────────────────────────────────────────┐  │
│  │  Router       Redis       Config      LogBridge    │  │
│  └────────────────────────────────────────────────────┘  │
│                                                          │
│  Coroutine 1              Coroutine 2                    │
│  ┌──────────────────┐    ┌──────────────────┐           │
│  │ Session (User A)  │    │ Session (User B)  │           │
│  │ Signal (trace-1)  │    │ Signal (trace-2)  │           │
│  └──────────────────┘    └──────────────────┘           │
│    auto-destroyed           auto-destroyed               │
│    on coroutine exit        on coroutine exit             │
└──────────────────────────────────────────────────────────┘

Singleton services resolve once and are shared across all coroutines in the worker.

Scoped services resolve once per coroutine and are stored in Swoole\Coroutine::getContext(). When the coroutine finishes, Swoole's runtime destroys the context and all scoped instances are garbage collected.

Outside a coroutine (CLI bootstrap, onStart callback), the provider falls back to an in-memory ArrayContext.

Server Example

use Swoole\Http\Server;
use PHPdot\Container\ContainerBuilder;
use PHPdot\Container\Swoole\SwooleContextProvider;
use function PHPdot\Container\singleton;
use function PHPdot\Container\scoped;

$container = (new ContainerBuilder())
    ->withContextProvider(new SwooleContextProvider())
    ->addDefinitions([
        Config::class  => singleton(),
        Session::class => scoped(fn($c) => Session::fromRequest($c->get(Request::class))),
    ])
    ->build();

$server = new Server('0.0.0.0', 8080);

$server->on('request', function ($req, $res) use ($container) {
    // Each request runs in its own coroutine.
    // Scoped services are fresh. Singletons are shared.
    $session = $container->get(Session::class);

    $res->end('Hello ' . $session->user());
    // Coroutine ends here — scoped instances destroyed automatically.
});

$server->start();

Requirements

License

MIT

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-03-31

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固