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 InvalidArgumentException;
7
use RuntimeException;
8
 
9
abstract class MultipleInstanceManager
10
{
11
    /**
12
     * The application instance.
13
     *
14
     * @var \Illuminate\Contracts\Foundation\Application
15
     */
16
    protected $app;
17
 
18
    /**
19
     * The array of resolved instances.
20
     *
21
     * @var array
22
     */
23
    protected $instances = [];
24
 
25
    /**
26
     * The registered custom instance creators.
27
     *
28
     * @var array
29
     */
30
    protected $customCreators = [];
31
 
32
    /**
33
     * Create a new manager instance.
34
     *
35
     * @param  \Illuminate\Contracts\Foundation\Application  $app
36
     * @return void
37
     */
38
    public function __construct($app)
39
    {
40
        $this->app = $app;
41
    }
42
 
43
    /**
44
     * Get the default instance name.
45
     *
46
     * @return string
47
     */
48
    abstract public function getDefaultInstance();
49
 
50
    /**
51
     * Set the default instance name.
52
     *
53
     * @param  string  $name
54
     * @return void
55
     */
56
    abstract public function setDefaultInstance($name);
57
 
58
    /**
59
     * Get the instance specific configuration.
60
     *
61
     * @param  string  $name
62
     * @return array
63
     */
64
    abstract public function getInstanceConfig($name);
65
 
66
    /**
67
     * Get an instance instance by name.
68
     *
69
     * @param  string|null  $name
70
     * @return mixed
71
     */
72
    public function instance($name = null)
73
    {
74
        $name = $name ?: $this->getDefaultInstance();
75
 
76
        return $this->instances[$name] = $this->get($name);
77
    }
78
 
79
    /**
80
     * Attempt to get an instance from the local cache.
81
     *
82
     * @param  string  $name
83
     * @return mixed
84
     */
85
    protected function get($name)
86
    {
87
        return $this->instances[$name] ?? $this->resolve($name);
88
    }
89
 
90
    /**
91
     * Resolve the given instance.
92
     *
93
     * @param  string  $name
94
     * @return mixed
95
     *
96
     * @throws \InvalidArgumentException
97
     */
98
    protected function resolve($name)
99
    {
100
        $config = $this->getInstanceConfig($name);
101
 
102
        if (is_null($config)) {
103
            throw new InvalidArgumentException("Instance [{$name}] is not defined.");
104
        }
105
 
106
        if (! array_key_exists('driver', $config)) {
107
            throw new RuntimeException("Instance [{$name}] does not specify a driver.");
108
        }
109
 
110
        if (isset($this->customCreators[$config['driver']])) {
111
            return $this->callCustomCreator($config);
112
        } else {
113
            $driverMethod = 'create'.ucfirst($config['driver']).'Driver';
114
 
115
            if (method_exists($this, $driverMethod)) {
116
                return $this->{$driverMethod}($config);
117
            } else {
118
                throw new InvalidArgumentException("Instance driver [{$config['driver']}] is not supported.");
119
            }
120
        }
121
    }
122
 
123
    /**
124
     * Call a custom instance creator.
125
     *
126
     * @param  array  $config
127
     * @return mixed
128
     */
129
    protected function callCustomCreator(array $config)
130
    {
131
        return $this->customCreators[$config['driver']]($this->app, $config);
132
    }
133
 
134
    /**
135
     * Unset the given instances.
136
     *
137
     * @param  array|string|null  $name
138
     * @return $this
139
     */
140
    public function forgetInstance($name = null)
141
    {
142
        $name ??= $this->getDefaultInstance();
143
 
144
        foreach ((array) $name as $instanceName) {
145
            if (isset($this->instances[$instanceName])) {
146
                unset($this->instances[$instanceName]);
147
            }
148
        }
149
 
150
        return $this;
151
    }
152
 
153
    /**
154
     * Disconnect the given instance and remove from local cache.
155
     *
156
     * @param  string|null  $name
157
     * @return void
158
     */
159
    public function purge($name = null)
160
    {
161
        $name ??= $this->getDefaultInstance();
162
 
163
        unset($this->instances[$name]);
164
    }
165
 
166
    /**
167
     * Register a custom instance creator Closure.
168
     *
169
     * @param  string  $name
170
     * @param  \Closure  $callback
171
     * @return $this
172
     */
173
    public function extend($name, Closure $callback)
174
    {
175
        $this->customCreators[$name] = $callback->bindTo($this, $this);
176
 
177
        return $this;
178
    }
179
 
180
    /**
181
     * Dynamically call the default instance.
182
     *
183
     * @param  string  $method
184
     * @param  array  $parameters
185
     * @return mixed
186
     */
187
    public function __call($method, $parameters)
188
    {
189
        return $this->instance()->$method(...$parameters);
190
    }
191
}