Skip to content

debuss/awareness

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Awareness

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 :

Mezzio Template Renderer

  • TemplateRendererAwareInterface / TemplateRendererAwareTrait

Installation

composer require debuss-a/awareness

Requirements

  • PHP 8.0 or higher

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages