/
PredisServiceProvider.php
130 lines (105 loc) · 3.8 KB
/
PredisServiceProvider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
/*
* This file is part of the PredisServiceProvider package.
*
* (c) Daniele Alessandri <suppakilla@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Predis\Silex;
use Silex\Application;
use Silex\ServiceProviderInterface;
use Predis\Client;
/**
* Exposes one or more client instances of Predis to Silex.
*
* @author Daniele Alessandri <suppakilla@gmail.com>
*/
class PredisServiceProvider implements ServiceProviderInterface
{
protected static $reserved = array(
'parameters', 'options', 'default_parameters', 'default_options',
'clients', 'client_initializer',
);
protected $prefix;
/**
* @param string $predix Prefix name used to register the service provider in Silex.
*/
public function __construct($prefix = 'predis')
{
if (empty($prefix)) {
throw new \InvalidArgumentException('The specified prefix is not valid.');
}
$this->prefix = $prefix;
}
/**
* {@inheritdoc}
*/
public function boot(Application $app)
{
$prefix = $this->prefix;
if (!isset($app["$prefix.default_parameters"])) {
$app["$prefix.default_parameters"] = array();
}
if (!isset($app["$prefix.default_options"])) {
$app["$prefix.default_options"] = array();
}
if (isset($app["$prefix.clients"])) {
foreach ($app["$prefix.clients"] as $alias => $args) {
if (in_array($alias, self::$reserved, true)) {
throw new \InvalidArgumentException("The specified alias '$alias' is not valid.");
}
$app["$prefix.$alias"] = $app->share(function () use ($app, $prefix, $args) {
$initializer = $app["$prefix.client_initializer"];
if (!isset($args['parameters'])) {
if (!isset($args['options']) && !isset($args['default'])) {
$args = array('parameters' => $args);
}
}
return $initializer($args);
});
if (is_array($args) && isset($args['default']) && $args['default'] == true) {
$app[$prefix] = $app->share(function () use ($app, $prefix, $alias) {
return $app["$prefix.$alias"];
});
}
}
} else {
$app[$prefix] = $app->share(function () use ($app, $prefix) {
$initializer = $app["$prefix.client_initializer"];
return $initializer($app);
});
}
}
/**
* {@inheritdoc}
*/
public function register(Application $app)
{
$prefix = $this->prefix;
$app["$prefix.client_initializer"] = $app->protect(function ($arguments) use ($app, $prefix) {
$extract = function ($bag, $key) use ($app, $prefix) {
$default = "default_$key";
if ($bag instanceof Application) {
$key = "$prefix.$key";
}
if (!isset($bag[$key])) {
return $app["$prefix.$default"];
}
if (is_array($bag[$key])) {
return array_merge($app["$prefix.$default"], $bag[$key]);
}
return $bag[$key];
};
if (is_string($arguments)) {
$parameters = $arguments;
$options = $app["$prefix.default_options"];
} else {
$parameters = $extract($arguments, 'parameters');
$options = $extract($arguments, 'options');
}
return new Client($parameters, $options);
});
}
}