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\Support;
4
 
5
use Closure;
6
use Illuminate\Contracts\Container\Container;
7
use InvalidArgumentException;
8
 
9
abstract class Manager
10
{
11
    /**
12
     * The container instance.
13
     *
14
     * @var \Illuminate\Contracts\Container\Container
15
     */
16
    protected $container;
17
 
18
    /**
19
     * The configuration repository instance.
20
     *
21
     * @var \Illuminate\Contracts\Config\Repository
22
     */
23
    protected $config;
24
 
25
    /**
26
     * The registered custom driver creators.
27
     *
28
     * @var array
29
     */
30
    protected $customCreators = [];
31
 
32
    /**
33
     * The array of created "drivers".
34
     *
35
     * @var array
36
     */
37
    protected $drivers = [];
38
 
39
    /**
40
     * Create a new manager instance.
41
     *
42
     * @param  \Illuminate\Contracts\Container\Container  $container
43
     * @return void
44
     */
45
    public function __construct(Container $container)
46
    {
47
        $this->container = $container;
48
        $this->config = $container->make('config');
49
    }
50
 
51
    /**
52
     * Get the default driver name.
53
     *
54
     * @return string
55
     */
56
    abstract public function getDefaultDriver();
57
 
58
    /**
59
     * Get a driver instance.
60
     *
61
     * @param  string|null  $driver
62
     * @return mixed
63
     *
64
     * @throws \InvalidArgumentException
65
     */
66
    public function driver($driver = null)
67
    {
68
        $driver = $driver ?: $this->getDefaultDriver();
69
 
70
        if (is_null($driver)) {
71
            throw new InvalidArgumentException(sprintf(
72
                'Unable to resolve NULL driver for [%s].', static::class
73
            ));
74
        }
75
 
76
        // If the given driver has not been created before, we will create the instances
77
        // here and cache it so we can return it next time very quickly. If there is
78
        // already a driver created by this name, we'll just return that instance.
79
        if (! isset($this->drivers[$driver])) {
80
            $this->drivers[$driver] = $this->createDriver($driver);
81
        }
82
 
83
        return $this->drivers[$driver];
84
    }
85
 
86
    /**
87
     * Create a new driver instance.
88
     *
89
     * @param  string  $driver
90
     * @return mixed
91
     *
92
     * @throws \InvalidArgumentException
93
     */
94
    protected function createDriver($driver)
95
    {
96
        // First, we will determine if a custom driver creator exists for the given driver and
97
        // if it does not we will check for a creator method for the driver. Custom creator
98
        // callbacks allow developers to build their own "drivers" easily using Closures.
99
        if (isset($this->customCreators[$driver])) {
100
            return $this->callCustomCreator($driver);
101
        } else {
102
            $method = 'create'.Str::studly($driver).'Driver';
103
 
104
            if (method_exists($this, $method)) {
105
                return $this->$method();
106
            }
107
        }
108
 
109
        throw new InvalidArgumentException("Driver [$driver] not supported.");
110
    }
111
 
112
    /**
113
     * Call a custom driver creator.
114
     *
115
     * @param  string  $driver
116
     * @return mixed
117
     */
118
    protected function callCustomCreator($driver)
119
    {
120
        return $this->customCreators[$driver]($this->container);
121
    }
122
 
123
    /**
124
     * Register a custom driver creator Closure.
125
     *
126
     * @param  string  $driver
127
     * @param  \Closure  $callback
128
     * @return $this
129
     */
130
    public function extend($driver, Closure $callback)
131
    {
132
        $this->customCreators[$driver] = $callback;
133
 
134
        return $this;
135
    }
136
 
137
    /**
138
     * Get all of the created "drivers".
139
     *
140
     * @return array
141
     */
142
    public function getDrivers()
143
    {
144
        return $this->drivers;
145
    }
146
 
147
    /**
148
     * Get the container instance used by the manager.
149
     *
150
     * @return \Illuminate\Contracts\Container\Container
151
     */
152
    public function getContainer()
153
    {
154
        return $this->container;
155
    }
156
 
157
    /**
158
     * Set the container instance used by the manager.
159
     *
160
     * @param  \Illuminate\Contracts\Container\Container  $container
161
     * @return $this
162
     */
163
    public function setContainer(Container $container)
164
    {
165
        $this->container = $container;
166
 
167
        return $this;
168
    }
169
 
170
    /**
171
     * Forget all of the resolved driver instances.
172
     *
173
     * @return $this
174
     */
175
    public function forgetDrivers()
176
    {
177
        $this->drivers = [];
178
 
179
        return $this;
180
    }
181
 
182
    /**
183
     * Dynamically call the default driver instance.
184
     *
185
     * @param  string  $method
186
     * @param  array  $parameters
187
     * @return mixed
188
     */
189
    public function __call($method, $parameters)
190
    {
191
        return $this->driver()->$method(...$parameters);
192
    }
193
}