承接 debuss-a/awareness 相关项目开发

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

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

debuss-a/awareness

最新稳定版本:1.2.0

Composer 安装命令:

composer require debuss-a/awareness

包简介

A set of interfaces and traits to make your classes AWARE, just like JCVD.

README 文档

README

A set of interfaces and traits to make your classes AWARE, just like JCVD.

What is this?

Awareness provides a collection of PSR-compliant "aware" interfaces and traits that allow your classes to declare and receive dependencies through setter injection. Each interface follows the -aware pattern, making it easy to implement dependency injection in a standardized way.

Why use Awareness?

Perfect for Inflectors

Awareness interfaces are particularly useful with dependency injection containers that support inflectors, such as League Container. Inflectors allow you to automatically inject dependencies into any class implementing a specific interface, without having to configure each class individually.

Example with League Container:

use League\Container\Container;
use Psr\Http\Client\ClientInterface;
use Awareness\ClientAwareInterface;

$container = new Container();

// Register your HTTP client
$container->add(ClientInterface::class, GuzzleClient::class);

// Set up an inflector: any class implementing ClientAwareInterface
// will automatically get the HTTP client injected
$container->inflector(ClientAwareInterface::class)
    ->invokeMethod('setClient', [ClientInterface::class]);

// Now any ClientAwareInterface implementation gets the client automatically!
$myService = $container->get(MyApiService::class);
// MyApiService now has the HTTP client injected via setClient()

This approach is extremely powerful because:

  • No per-class configuration needed - just implement the interface and use the trait
  • Consistent dependency injection - same pattern across your entire application
  • Clean separation of concerns - dependencies are clearly declared through interfaces
  • Easy testing - swap implementations by just using different inflector configurations

Simple Implementation

To make a class "aware" of a dependency:

  1. Implement the corresponding aware interface
  2. Use the corresponding aware trait (optional, but recommended)
use Awareness\CacheAwareInterface;
use Awareness\CacheAwareTrait;

class MyService implements CacheAwareInterface
{
    use CacheAwareTrait;
    
    public function doSomething()
    {
        // Access the cache through $this->cache
        $this->cache->set('key', 'value');
    }
}

Available Interfaces & Traits

Awareness covers all major PSR interfaces:

PSR-6: Cache

  • CacheItemPoolAwareInterface / CacheItemPoolAwareTrait

PSR-11: Container

  • ContainerAwareInterface / ContainerAwareTrait

PSR-14: Event Dispatcher

  • EventDispatcherAwareInterface / EventDispatcherAwareTrait

PSR-16: Simple Cache

  • CacheAwareInterface / CacheAwareTrait

PSR-17: HTTP Factories

  • RequestFactoryAwareInterface / RequestFactoryAwareTrait
  • ResponseFactoryAwareInterface / ResponseFactoryAwareTrait
  • ServerRequestFactoryAwareInterface / ServerRequestFactoryAwareTrait
  • StreamFactoryAwareInterface / StreamFactoryAwareTrait
  • UploadedFileFactoryAwareInterface / UploadedFileFactoryAwareTrait
  • UriFactoryAwareInterface / UriFactoryAwareTrait

PSR-18: HTTP Client

  • ClientAwareInterface / ClientAwareTrait

And other useful interfaces :

Installation

composer require debuss-a/awareness

Requirements

  • PHP 8.0 or higher

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-25