A set of interfaces and traits to make your classes AWARE, just like JCVD.
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.
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
To make a class "aware" of a dependency:
- Implement the corresponding aware interface
- 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');
}
}Awareness covers all major PSR interfaces:
CacheItemPoolAwareInterface/CacheItemPoolAwareTrait
ContainerAwareInterface/ContainerAwareTrait
EventDispatcherAwareInterface/EventDispatcherAwareTrait
CacheAwareInterface/CacheAwareTrait
RequestFactoryAwareInterface/RequestFactoryAwareTraitResponseFactoryAwareInterface/ResponseFactoryAwareTraitServerRequestFactoryAwareInterface/ServerRequestFactoryAwareTraitStreamFactoryAwareInterface/StreamFactoryAwareTraitUploadedFileFactoryAwareInterface/UploadedFileFactoryAwareTraitUriFactoryAwareInterface/UriFactoryAwareTrait
ClientAwareInterface/ClientAwareTrait
And other useful interfaces :
TemplateRendererAwareInterface/TemplateRendererAwareTrait
composer require debuss-a/awareness- PHP 8.0 or higher