PluginProbe
Fluent Support – Helpdesk & Customer Support Ticket System / 2.2.0
Fluent Support – Helpdesk & Customer Support Ticket System v2.2.0
2.4.0 2.3.2 2.3.1 2.3.0 2.2.1 2.2.0 trunk 1.10.0 1.10.1 1.10.2 1.10.3 1.10.4 1.10.5 1.4.0 1.4.1 1.4.2 1.4.5 1.4.6 1.4.7 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 All 68 releases
fluent-support / vendor / wpfluent / framework / src / WPFluent / Container / ContextualBindingBuilder.php

ContextualBindingBuilder.php in Fluent Support – Helpdesk & Customer Support Ticket System 2.2.0, at vendor/wpfluent/framework/src/WPFluent/Container/ContextualBindingBuilder.php

99 lines 2.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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