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
|
10 |
|
public function connection($name = null) |
18
|
|
|
{ |
19
|
10 |
|
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
|
2 |
|
public function getDefaultConnection() |
38
|
|
|
{ |
39
|
2 |
|
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
|
10 |
|
protected function createDriver($name) |
67
|
|
|
{ |
68
|
10 |
|
$config = $this->app['config']->get("hashid.connections.{$name}", []); |
69
|
|
|
|
70
|
10 |
|
if (isset($this->customCreators[$name])) { |
71
|
5 |
|
return $this->customCreators[$name]($config, $this->app, $name); |
72
|
|
|
} |
73
|
|
|
|
74
|
5 |
|
$driver = Arr::pull($config, 'driver'); |
75
|
|
|
|
76
|
5 |
|
return $this->createConnectionForDriver($name, $driver, $config); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Create a new hashid connection instance for the driver. |
81
|
|
|
* |
82
|
|
|
* @param string $name |
83
|
|
|
* @param string $driver |
84
|
|
|
* @param array $config |
85
|
|
|
* @return mixed |
86
|
|
|
* |
87
|
|
|
* @throws \InvalidArgumentException |
88
|
|
|
*/ |
89
|
5 |
|
protected function createConnectionForDriver($name, $driver, array $config = []) |
90
|
|
|
{ |
91
|
5 |
|
if (is_null($driver)) { |
92
|
1 |
|
throw new InvalidArgumentException('A driver must be specified.'); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
// We will check to see if a creator method exists for the given driver, |
96
|
|
|
// and will call the Closure if so, which allows us to have a more generic |
97
|
|
|
// resolver for the drivers themselves which applies to all connections. |
98
|
4 |
|
if (isset($this->customCreators[$driver])) { |
99
|
1 |
|
return $this->customCreators[$driver]($config, $this->app, $name); |
100
|
|
|
} |
101
|
|
|
|
102
|
3 |
|
if ($this->app->bound($key = "hashid.driver.{$driver}")) { |
103
|
2 |
|
if ($this->app->isShared($key)) { |
104
|
1 |
|
return $this->app->make($key); |
105
|
|
|
} |
106
|
|
|
|
107
|
1 |
|
$makeWith = method_exists($this->app, 'makeWith') ? 'makeWith' : 'make'; |
108
|
|
|
|
109
|
1 |
|
return $this->app->{$makeWith}($key, [ |
110
|
1 |
|
'app' => $this->app, |
111
|
1 |
|
'config' => $config, |
112
|
1 |
|
]); |
113
|
|
|
} |
114
|
|
|
|
115
|
1 |
|
throw new InvalidArgumentException("Unsupported driver [$driver]"); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|