1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ElfSundae\Laravel\Hashid; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
use Illuminate\Support\Manager; |
7
|
|
|
|
8
|
|
|
class HashidManager extends Manager |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Get a hashid connection instance. |
12
|
|
|
* |
13
|
|
|
* @param string|null $name |
14
|
|
|
* @return \ElfSundae\Laravel\Hashid\ConnectionInterface |
15
|
|
|
*/ |
16
|
|
|
public function connection($name = null) |
17
|
|
|
{ |
18
|
|
|
return $this->driver($name); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Get all of the created connections. |
23
|
|
|
* |
24
|
|
|
* @return array |
25
|
|
|
*/ |
26
|
|
|
public function getConnections() |
27
|
|
|
{ |
28
|
|
|
return $this->getDrivers(); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Get the default connection name. |
33
|
|
|
* |
34
|
|
|
* @return string |
35
|
|
|
*/ |
36
|
|
|
public function getDefaultConnection() |
37
|
|
|
{ |
38
|
|
|
return $this->app['config']['hashid.default']; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Set the default connection name. |
43
|
|
|
* |
44
|
|
|
* @param string $name |
45
|
|
|
* @return $this |
46
|
|
|
*/ |
47
|
|
|
public function setDefaultConnection($name) |
48
|
|
|
{ |
49
|
|
|
$this->app['config']['hashid.default'] = $name; |
50
|
|
|
|
51
|
|
|
return $this; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritdoc} |
56
|
|
|
*/ |
57
|
|
|
public function getDefaultDriver() |
58
|
|
|
{ |
59
|
|
|
return $this->getDefaultConnection(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* {@inheritdoc} |
64
|
|
|
*/ |
65
|
|
|
protected function createDriver($name) |
66
|
|
|
{ |
67
|
|
|
$config = $this->app['config']->get("hashid.connections.{$name}", []); |
68
|
|
|
|
69
|
|
|
// First we will check by the connection name to see if a creator method |
70
|
|
|
// exists for the given connection. |
71
|
|
|
if (isset($this->customCreators[$name])) { |
72
|
|
|
return $this->customCreators[$name]($this->app, $config, $name); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
// Next we will check by the driver name to see if a creator method |
76
|
|
|
// exists for the given driver, and will call the Closure if so, |
77
|
|
|
// which allows us to have a more generic resolver for the drivers |
78
|
|
|
// themselves which applies to all connections. |
79
|
|
|
if (isset($config['driver']) && |
80
|
|
|
isset($this->customCreators[$driver = $config['driver']]) |
81
|
|
|
) { |
82
|
|
|
return $this->customCreators[$driver]($this->app, $config, $name); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $this->createConnection($config); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Create a new connection instance. |
90
|
|
|
* |
91
|
|
|
* @param array $config |
92
|
|
|
* @return \ElfSundae\Laravel\Hashid\ConnectionInterface |
93
|
|
|
* |
94
|
|
|
* @throws \InvalidArgumentException |
95
|
|
|
*/ |
96
|
|
|
protected function createConnection(array $config) |
97
|
|
|
{ |
98
|
|
|
if (! isset($config['driver'])) { |
99
|
|
|
throw new InvalidArgumentException('A driver must be specified.'); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$driver = $config['driver']; |
103
|
|
|
|
104
|
|
|
if ($this->app->bound($key = "hashid.connection.{$driver}")) { |
105
|
|
|
return $this->app->make($key, [$this->app, $config]); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
throw new InvalidArgumentException("Unsupported driver [$driver]"); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|