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