项目作者: ellipsephp

项目描述 :
Psr-11 container decorator allowing to override container entries at runtime
高级语言: PHP
项目地址: git://github.com/ellipsephp/container-overridden.git
创建时间: 2017-12-06T10:47:35Z
项目社区:https://github.com/ellipsephp/container-overridden

开源协议:MIT License

下载


Overridden container

This package provides a Psr-11 container decorator allowing to override container entries at runtime.

Require php >= 7.0

Installation composer require ellipse/container-overridden

Run tests ./vendor/bin/kahlan

Decorating a container

This package provides a Ellipse\Container\OverriddenContainer class which can be used to decorate any Psr-11 container. It takes an associative array of alias => values pairs as second parameter. Once decorated, the container ->has() method will return true when the given alias is a key of this array and the ->get() method will return its associated value. When the alias is not a key of the array, the original container ->has() and ->get() methods are used.

It is especially useful when used with ellipse/container-reflection.

  1. <?php
  2. namespace App;
  3. class SomeClass
  4. {
  5. public function __construct(SomeOtherClass $class)
  6. {
  7. //
  8. }
  9. }
  1. <?php
  2. namespace App;
  3. use Psr\Http\Message\ServerRequestInterface;
  4. class SomeOtherClass
  5. {
  6. public function __construct(ServerRequestInterface $request)
  7. {
  8. //
  9. }
  10. }
  1. <?php
  2. use Psr\Http\Message\ServerRequestInterface;
  3. use Some\Psr7ServerRequestFactory;
  4. use Some\Psr11Container;
  5. use Ellipse\Container\ReflectionContainer;
  6. use Ellipse\Container\OverriddenContainer;
  7. use App\SomeClass;
  8. // Get a Psr-7 request from somewhere.
  9. $request = Psr7ServerRequestFactory::fromGlobals();
  10. // Get an instance of some Psr-11 container.
  11. $container = new Psr11Container;
  12. // Decorate the container.
  13. $container = new ReflectionContainer(
  14. new OverriddenContainer($container, [
  15. ServerRequestInterface::class => $request,
  16. ])
  17. );
  18. // Returns an instance of SomeClass with the overridden Psr-7 instance injected in it's
  19. // SomeOtherClass dependency.
  20. $container->get(SomeClass::class);