Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\Container;
4
 
5
use Illuminate\Contracts\Container\Container;
6
use Illuminate\Contracts\Container\ContextualBindingBuilder as ContextualBindingBuilderContract;
7
 
8
class ContextualBindingBuilder implements ContextualBindingBuilderContract
9
{
10
    /**
11
     * The underlying container instance.
12
     *
13
     * @var \Illuminate\Contracts\Container\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  \Illuminate\Contracts\Container\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  mixed  $default
90
     * @return void
91
     */
92
    public function giveConfig($key, $default = null)
93
    {
94
        $this->give(fn ($container) => $container->get('config')->get($key, $default));
95
    }
96
}