承接 small/swoole-resource-client-bundle 相关项目开发

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

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

small/swoole-resource-client-bundle

Composer 安装命令:

composer require small/swoole-resource-client-bundle

包简介

Symfony bundle client for small-resource-server, using Swoole HttpClient.

README 文档

README

                

A lightweight Symfony bundle that provides a typed client to interact with the small-resource-server, using a Swoole-based HTTP client.

  • Namespace: Small\SwooleResourceClientBundle
  • Requires: PHP ≥ 8.3, Symfony ≥7.2, small/swoole-symfony-http-client
  • Config keys: server_uri, api_key

Why

  • Acquire and release resource locks via a simple API
  • Read / write JSON data for a named resource + selector
  • Fully compatible with Symfony HttpClient contracts; the factory accepts any HttpClientInterface

Installation

composer require small/swoole-resource-client-bundle
# (this bundle) and its HTTP dependency:
composer require small/swoole-symfony-http-client

If Flex does not register it automatically, enable the bundle in config/bundles.php:

return [
    Small\SwooleResourceClientBundle\SmallSwooleResourceClientBundle::class => ['all' => true],
];

Configuration

Create config/packages/small_swoole_resource_client.yaml:

small_swoole_resource_client:
  server_uri: 'http://localhost:9501'  # resource server base URL
  api_key: '%env(RESOURCE_API_KEY)%'   # key that the server expects in "x-api-key"

Tip: provide RESOURCE_API_KEY through your secrets or environment.

Services

The bundle exposes a single public service:

  • small_swoole_resource_client.factorySmall\SwooleResourceClientBundle\Resource\Factory

Factory

final class Factory
{
    public function __construct(string $serverUri, string $apiKey, ?HttpClientInterface $httpClient = null);
    public function createResource(string $name, int $timeout): Resource; // POST /resource
    public function getResource(string $name): Resource;                  // client-side proxy only
}
  • If no client is injected, the factory builds a Small\SwooleSymfonyHttpClient\SwooleHttpClient and configures it with:
    • base_uri = rtrim(serverUri, '/')
    • default headers: accept: application/json and x-api-key: <api_key>
    • timeout: 10

Resource

final class Resource
{
    public function __construct(string $name, HttpClientInterface $client);
    public function getData(string $selector, bool $lock): array|null; // GET /resource/{name}/{selector}?lock=0|1
    public function lockData(string $selector): bool;                  // convenience wrapper over getData(..., true)
    public function writeData(string $selector, string $json): bool;   // PUT /resource/{name}/{selector}
    public function unlockData(string $selector): bool;                // POST /resource/{name}/{selector}/unlock
    public function getTicket(): ?string;                              // last x-ticket, if provided by server
}
  • When the server returns 202 Accepted, the client stores the x-ticket header and returns null.
  • On 200 OK, getData() returns the decoded JSON array.
  • writeData() requires the x-ticket header (set automatically if previously received).
  • unlockData() returns true on 200 OK.

Usage Example

use Small\SwooleResourceClientBundle\Resource\Factory;

final class ExampleService
{
    public function __construct(private readonly Factory $factory) {}

    public function run(): void
    {
        $res = $this->factory->createResource('printer', 300);

        // Try to acquire lock & get data
        $data = $res->getData('queue', true);
        if ($data === null) {
            // 202 Accepted: lock pending; you may retry later (ticket is kept inside the Resource).
            return;
        }

        // Process and write a new state
        $res->writeData('queue', json_encode(['status' => 'done'], JSON_THROW_ON_ERROR));
        $res->unlockData('queue');
    }
}

Testing

This repository uses Pest for tests. The code is written against HttpClientInterface which makes it easy to inject a fake client in test env.

Run the suite

composer install
composer test
# or directly: vendor/bin/pest

Feature tests (Kernel)

In config/packages/test/small_swoole_resource_client.yaml:

small_swoole_resource_client:
  server_uri: 'http://server.example:9501'
  api_key: 'SECRET_KEY'

Inject a fake client via config/services_test.yaml (or services_test.php):

services:
  Tests\FakeClass\DummySwooleHttpClient: { public: true }

  small_swoole_resource_client.factory:
    class: Small\SwooleResourceClientBundle\Resource\Factory
    public: true
    arguments:
      $serverUri: '%small_swoole_resource_client.server_uri%'
      $apiKey: '%small_swoole_resource_client.api_key%'
      $httpClient: '@Tests\FakeClass\DummySwooleHttpClient'

Static Analysis

We recommend PHPStan (at the highest level your codebase supports).

composer phpstan
# or vendor/bin/phpstan analyse

Error Handling

All network/transport failures are wrapped in RuntimeException with a clear message. For non-success HTTP statuses, the response body (if any) is included in the exception message to aid diagnostics.

Security

This bundle forwards x-api-key to the resource server. Treat that key as a secret. Consider scoping the key with minimal privileges when your server supports it.

License

Distributed under the MIT license. See LICENSE for details.

Author: Sébastien Kus
Contact: sebastien.kus@gmail.com

small/swoole-resource-client-bundle 适用场景与选型建议

small/swoole-resource-client-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 26 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 08 月 31 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 small/swoole-resource-client-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 small/swoole-resource-client-bundle 我们能提供哪些服务?
定制开发 / 二次开发

基于 small/swoole-resource-client-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-08-31