fluent-support
/
vendor
/
wpfluent
/
framework
/
src
/
WPFluent
/
Container
/
ContextualBindingBuilder.php
ContextualBindingBuilder.php in Fluent Support – Helpdesk & Customer Support Ticket System trunk, at vendor/wpfluent/framework/src/WPFluent/Container/ContextualBindingBuilder.php
| 1 | <?php |
| 2 | |
| 3 | namespace FluentSupport\Framework\Container; |
| 4 | |
| 5 | use FluentSupport\Framework\Container\Contracts\Container; |
| 6 | use FluentSupport\Framework\Container\Contracts\ContextualBindingBuilder as ContextualBindingBuilderContract; |
| 7 | |
| 8 | class ContextualBindingBuilder implements ContextualBindingBuilderContract |
| 9 | { |
| 10 | /** |
| 11 | * The underlying container instance. |
| 12 | * |
| 13 | * @var \FluentSupport\Framework\Container\Contracts\Container |
| 14 | */ |
| 15 | protected $container; |
| 16 | |
| 17 | /** |
| 18 | * The concrete instance. |
| 19 | * |
| 20 | * @var string|array |
| 21 | */ |
| 22 | protected $concrete; |
| 23 | |
| 24 | /** |
| 25 | * The abstract target. |
| 26 | * |
| 27 | * @var string |
| 28 | */ |
| 29 | protected $needs; |
| 30 | |
| 31 | /** |
| 32 | * Create a new contextual binding builder. |
| 33 | * |
| 34 | * @param \FluentSupport\Framework\Container\Contracts\Container $container |
| 35 | * @param string|array $concrete |
| 36 | * @return void |
| 37 | */ |
| 38 | public function __construct(Container $container, $concrete) |
| 39 | { |
| 40 | $this->concrete = $concrete; |
| 41 | $this->container = $container; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Define the abstract target that depends on the context. |
| 46 | * |
| 47 | * @param string $abstract |
| 48 | * @return $this |
| 49 | */ |
| 50 | public function needs($abstract) |
| 51 | { |
| 52 | $this->needs = $abstract; |
| 53 | |
| 54 | return $this; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Define the implementation for the contextual binding. |
| 59 | * |
| 60 | * @param \Closure|string|array $implementation |
| 61 | * @return void |
| 62 | */ |
| 63 | public function give($implementation) |
| 64 | { |
| 65 | foreach (Util::arrayWrap($this->concrete) as $concrete) { |
| 66 | $this->container->addContextualBinding($concrete, $this->needs, $implementation); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Define tagged services to be used as the implementation for the contextual binding. |
| 72 | * |
| 73 | * @param string $tag |
| 74 | * @return void |
| 75 | */ |
| 76 | public function giveTagged($tag) |
| 77 | { |
| 78 | $this->give(function ($container) use ($tag) { |
| 79 | $taggedServices = $container->tagged($tag); |
| 80 | |
| 81 | return is_array($taggedServices) ? $taggedServices : iterator_to_array($taggedServices); |
| 82 | }); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Specify the configuration item to bind as a primitive. |
| 87 | * |
| 88 | * @param string $key |
| 89 | * @param ?string $default |
| 90 | * @return void |
| 91 | */ |
| 92 | public function giveConfig($key, $default = null) |
| 93 | { |
| 94 | $this->give(function ($container) use ($key, $default) { |
| 95 | return $container->get('config')->get($key, $default); |
| 96 | }); |
| 97 | } |
| 98 | } |
| 99 |