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\Redis;
4
 
5
use Closure;
6
use Illuminate\Contracts\Redis\Factory;
7
use Illuminate\Redis\Connections\Connection;
8
use Illuminate\Redis\Connectors\PhpRedisConnector;
9
use Illuminate\Redis\Connectors\PredisConnector;
10
use Illuminate\Support\Arr;
11
use Illuminate\Support\ConfigurationUrlParser;
12
use InvalidArgumentException;
13
 
14
/**
15
 * @mixin \Illuminate\Redis\Connections\Connection
16
 */
17
class RedisManager implements Factory
18
{
19
    /**
20
     * The application instance.
21
     *
22
     * @var \Illuminate\Contracts\Foundation\Application
23
     */
24
    protected $app;
25
 
26
    /**
27
     * The name of the default driver.
28
     *
29
     * @var string
30
     */
31
    protected $driver;
32
 
33
    /**
34
     * The registered custom driver creators.
35
     *
36
     * @var array
37
     */
38
    protected $customCreators = [];
39
 
40
    /**
41
     * The Redis server configurations.
42
     *
43
     * @var array
44
     */
45
    protected $config;
46
 
47
    /**
48
     * The Redis connections.
49
     *
50
     * @var mixed
51
     */
52
    protected $connections;
53
 
54
    /**
55
     * Indicates whether event dispatcher is set on connections.
56
     *
57
     * @var bool
58
     */
59
    protected $events = false;
60
 
61
    /**
62
     * Create a new Redis manager instance.
63
     *
64
     * @param  \Illuminate\Contracts\Foundation\Application  $app
65
     * @param  string  $driver
66
     * @param  array  $config
67
     * @return void
68
     */
69
    public function __construct($app, $driver, array $config)
70
    {
71
        $this->app = $app;
72
        $this->driver = $driver;
73
        $this->config = $config;
74
    }
75
 
76
    /**
77
     * Get a Redis connection by name.
78
     *
79
     * @param  string|null  $name
80
     * @return \Illuminate\Redis\Connections\Connection
81
     */
82
    public function connection($name = null)
83
    {
84
        $name = $name ?: 'default';
85
 
86
        if (isset($this->connections[$name])) {
87
            return $this->connections[$name];
88
        }
89
 
90
        return $this->connections[$name] = $this->configure(
91
            $this->resolve($name), $name
92
        );
93
    }
94
 
95
    /**
96
     * Resolve the given connection by name.
97
     *
98
     * @param  string|null  $name
99
     * @return \Illuminate\Redis\Connections\Connection
100
     *
101
     * @throws \InvalidArgumentException
102
     */
103
    public function resolve($name = null)
104
    {
105
        $name = $name ?: 'default';
106
 
107
        $options = $this->config['options'] ?? [];
108
 
109
        if (isset($this->config[$name])) {
110
            return $this->connector()->connect(
111
                $this->parseConnectionConfiguration($this->config[$name]),
112
                array_merge(Arr::except($options, 'parameters'), ['parameters' => Arr::get($options, 'parameters.'.$name, Arr::get($options, 'parameters', []))])
113
            );
114
        }
115
 
116
        if (isset($this->config['clusters'][$name])) {
117
            return $this->resolveCluster($name);
118
        }
119
 
120
        throw new InvalidArgumentException("Redis connection [{$name}] not configured.");
121
    }
122
 
123
    /**
124
     * Resolve the given cluster connection by name.
125
     *
126
     * @param  string  $name
127
     * @return \Illuminate\Redis\Connections\Connection
128
     */
129
    protected function resolveCluster($name)
130
    {
131
        return $this->connector()->connectToCluster(
132
            array_map(function ($config) {
133
                return $this->parseConnectionConfiguration($config);
134
            }, $this->config['clusters'][$name]),
135
            $this->config['clusters']['options'] ?? [],
136
            $this->config['options'] ?? []
137
        );
138
    }
139
 
140
    /**
141
     * Configure the given connection to prepare it for commands.
142
     *
143
     * @param  \Illuminate\Redis\Connections\Connection  $connection
144
     * @param  string  $name
145
     * @return \Illuminate\Redis\Connections\Connection
146
     */
147
    protected function configure(Connection $connection, $name)
148
    {
149
        $connection->setName($name);
150
 
151
        if ($this->events && $this->app->bound('events')) {
152
            $connection->setEventDispatcher($this->app->make('events'));
153
        }
154
 
155
        return $connection;
156
    }
157
 
158
    /**
159
     * Get the connector instance for the current driver.
160
     *
161
     * @return \Illuminate\Contracts\Redis\Connector|null
162
     */
163
    protected function connector()
164
    {
165
        $customCreator = $this->customCreators[$this->driver] ?? null;
166
 
167
        if ($customCreator) {
168
            return $customCreator();
169
        }
170
 
171
        return match ($this->driver) {
172
            'predis' => new PredisConnector,
173
            'phpredis' => new PhpRedisConnector,
174
            default => null,
175
        };
176
    }
177
 
178
    /**
179
     * Parse the Redis connection configuration.
180
     *
181
     * @param  mixed  $config
182
     * @return array
183
     */
184
    protected function parseConnectionConfiguration($config)
185
    {
186
        $parsed = (new ConfigurationUrlParser)->parseConfiguration($config);
187
 
188
        $driver = strtolower($parsed['driver'] ?? '');
189
 
190
        if (in_array($driver, ['tcp', 'tls'])) {
191
            $parsed['scheme'] = $driver;
192
        }
193
 
194
        return array_filter($parsed, function ($key) {
195
            return ! in_array($key, ['driver'], true);
196
        }, ARRAY_FILTER_USE_KEY);
197
    }
198
 
199
    /**
200
     * Return all of the created connections.
201
     *
202
     * @return array
203
     */
204
    public function connections()
205
    {
206
        return $this->connections;
207
    }
208
 
209
    /**
210
     * Enable the firing of Redis command events.
211
     *
212
     * @return void
213
     */
214
    public function enableEvents()
215
    {
216
        $this->events = true;
217
    }
218
 
219
    /**
220
     * Disable the firing of Redis command events.
221
     *
222
     * @return void
223
     */
224
    public function disableEvents()
225
    {
226
        $this->events = false;
227
    }
228
 
229
    /**
230
     * Set the default driver.
231
     *
232
     * @param  string  $driver
233
     * @return void
234
     */
235
    public function setDriver($driver)
236
    {
237
        $this->driver = $driver;
238
    }
239
 
240
    /**
241
     * Disconnect the given connection and remove from local cache.
242
     *
243
     * @param  string|null  $name
244
     * @return void
245
     */
246
    public function purge($name = null)
247
    {
248
        $name = $name ?: 'default';
249
 
250
        unset($this->connections[$name]);
251
    }
252
 
253
    /**
254
     * Register a custom driver creator Closure.
255
     *
256
     * @param  string  $driver
257
     * @param  \Closure  $callback
258
     * @return $this
259
     */
260
    public function extend($driver, Closure $callback)
261
    {
262
        $this->customCreators[$driver] = $callback->bindTo($this, $this);
263
 
264
        return $this;
265
    }
266
 
267
    /**
268
     * Pass methods onto the default Redis connection.
269
     *
270
     * @param  string  $method
271
     * @param  array  $parameters
272
     * @return mixed
273
     */
274
    public function __call($method, $parameters)
275
    {
276
        return $this->connection()->{$method}(...$parameters);
277
    }
278
}