1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ElfSundae\Laravel\Hashid; |
4
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Container\Container; |
6
|
|
|
use Illuminate\Support\Arr; |
7
|
|
|
use Illuminate\Support\Manager; |
8
|
|
|
use InvalidArgumentException; |
9
|
|
|
|
10
|
|
|
class HashidManager extends Manager |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* The container instance. |
14
|
|
|
* |
15
|
|
|
* The $app property was removed in Laravel 8.x. |
16
|
|
|
* |
17
|
|
|
* @var \Illuminate\Contracts\Container\Container |
18
|
|
|
*/ |
19
|
|
|
protected $app; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Create a new manager instance. |
23
|
|
|
* |
24
|
|
|
* @param \Illuminate\Contracts\Container\Container $container |
25
|
|
|
* @return void |
26
|
|
|
*/ |
27
|
56 |
|
public function __construct($container) |
28
|
|
|
{ |
29
|
56 |
|
parent::__construct($container); |
30
|
|
|
|
31
|
56 |
|
$this->app = $container; |
32
|
42 |
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Get a hashid connection instance. |
36
|
|
|
* |
37
|
|
|
* @param string|null $name |
38
|
|
|
* @return mixed |
39
|
|
|
*/ |
40
|
44 |
|
public function connection($name = null) |
41
|
|
|
{ |
42
|
44 |
|
return $this->driver($name); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Get all of the created connections. |
47
|
|
|
* |
48
|
|
|
* @return array |
49
|
|
|
*/ |
50
|
4 |
|
public function getConnections() |
51
|
|
|
{ |
52
|
4 |
|
return $this->getDrivers(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Get the default connection name. |
57
|
|
|
* |
58
|
|
|
* @return string |
59
|
|
|
*/ |
60
|
12 |
|
public function getDefaultConnection() |
61
|
|
|
{ |
62
|
12 |
|
return $this->app['config']['hashid.default']; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Set the default connection name. |
67
|
|
|
* |
68
|
|
|
* @param string $name |
69
|
|
|
* @return $this |
70
|
|
|
*/ |
71
|
4 |
|
public function setDefaultConnection($name) |
72
|
|
|
{ |
73
|
4 |
|
$this->app['config']['hashid.default'] = $name; |
74
|
|
|
|
75
|
4 |
|
return $this; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* {@inheritdoc} |
80
|
|
|
*/ |
81
|
4 |
|
public function getDefaultDriver() |
82
|
|
|
{ |
83
|
4 |
|
return $this->getDefaultConnection(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* {@inheritdoc} |
88
|
|
|
*/ |
89
|
44 |
|
protected function createDriver($name) |
90
|
|
|
{ |
91
|
44 |
|
$config = Arr::get($this->app['config']['hashid.connections'], $name, []); |
92
|
|
|
|
93
|
44 |
|
return $this->createForConnection($name, $config) ?: |
94
|
44 |
|
$this->createForDriver(Arr::pull($config, 'driver', $name), $config); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Create a new driver instance for the given connection. |
99
|
|
|
* |
100
|
|
|
* @param string $name |
101
|
|
|
* @param array $config |
102
|
|
|
* @return mixed |
103
|
|
|
*/ |
104
|
44 |
|
protected function createForConnection($name, array $config = []) |
105
|
|
|
{ |
106
|
44 |
|
if (isset($this->customCreators[$name])) { |
107
|
28 |
|
return $this->callCustom($name, compact('config')); |
108
|
|
|
} |
109
|
15 |
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Create a new driver instance for the given driver. |
113
|
|
|
* |
114
|
|
|
* We will check to see if a creator method exists for the given driver, |
115
|
|
|
* and will call the Closure if so, which allows us to have a more generic |
116
|
|
|
* resolver for the drivers themselves which applies to all connections. |
117
|
|
|
* |
118
|
|
|
* @param string $driver |
119
|
|
|
* @param array $config |
120
|
|
|
* @return mixed |
121
|
|
|
* |
122
|
|
|
* @throws \InvalidArgumentException |
123
|
|
|
*/ |
124
|
20 |
|
protected function createForDriver($driver, array $config = []) |
125
|
|
|
{ |
126
|
20 |
|
if (isset($this->customCreators[$driver])) { |
127
|
4 |
|
return $this->callCustom($driver, compact('config')); |
128
|
|
|
} |
129
|
|
|
|
130
|
16 |
|
if ($binding = $this->getBindingKeyForDriver($driver)) { |
131
|
12 |
|
return $this->resolveBinding($binding, compact('config')); |
132
|
|
|
} |
133
|
|
|
|
134
|
4 |
|
throw new InvalidArgumentException("Unsupported driver [$driver]"); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Call a custom creator. |
139
|
|
|
* |
140
|
|
|
* @param string $key |
141
|
|
|
* @param array $parameters |
142
|
|
|
* @return mixed |
143
|
|
|
*/ |
144
|
28 |
|
protected function callCustom($key, array $parameters = []) |
145
|
|
|
{ |
146
|
28 |
|
return $this->app->call($this->customCreators[$key], $parameters); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Get the binding key for the driver. |
151
|
|
|
* |
152
|
|
|
* @param string $driver |
153
|
|
|
* @return string|null |
154
|
|
|
*/ |
155
|
16 |
|
protected function getBindingKeyForDriver($driver) |
156
|
|
|
{ |
157
|
16 |
|
if (class_exists($driver)) { |
158
|
4 |
|
return $driver; |
159
|
|
|
} |
160
|
|
|
|
161
|
12 |
|
if ($this->app->bound($key = "hashid.driver.$driver")) { |
162
|
8 |
|
return $key; |
163
|
|
|
} |
164
|
3 |
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Resolve the given binding from the container. |
168
|
|
|
* |
169
|
|
|
* NOTE: |
170
|
|
|
* `Container::make($abstract, $parameters)` which can pass additional |
171
|
|
|
* parameters to the constructor was removed in Laravel 5.4 |
172
|
|
|
* (https://github.com/laravel/internals/issues/391), but then re-added |
173
|
|
|
* as `makeWith()` in v5.4.16 (https://github.com/laravel/framework/pull/18271). |
174
|
|
|
* And in L55 the `makeWith()` is just an alias to `make()`. |
175
|
|
|
* |
176
|
|
|
* @param string $key |
177
|
|
|
* @param array $parameters |
178
|
|
|
* @return mixed |
179
|
|
|
*/ |
180
|
12 |
|
protected function resolveBinding($key, array $parameters = []) |
181
|
|
|
{ |
182
|
12 |
|
if ($this->app->isShared($key)) { |
183
|
4 |
|
return $this->app->make($key); |
184
|
|
|
} |
185
|
|
|
|
186
|
8 |
|
$makeWith = method_exists($this->app, 'makeWith') ? 'makeWith' : 'make'; |
187
|
|
|
|
188
|
8 |
|
return $this->app->$makeWith($key, $parameters); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* {@inheritdoc} |
193
|
|
|
*/ |
194
|
|
|
public function setContainer(Container $container) |
195
|
|
|
{ |
196
|
|
|
$this->app = $container; |
197
|
|
|
|
198
|
|
|
return parent::setContainer($container); |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|