1 | <?php |
||
8 | class HashidManager extends Manager |
||
9 | { |
||
10 | /** |
||
11 | * Get a hashid connection instance. |
||
12 | * |
||
13 | * @param string|null $name |
||
14 | * @return mixed |
||
15 | */ |
||
16 | 9 | public function connection($name = null) |
|
20 | |||
21 | /** |
||
22 | * Get all of the created connections. |
||
23 | * |
||
24 | * @return array |
||
25 | */ |
||
26 | 1 | public function getConnections() |
|
30 | |||
31 | /** |
||
32 | * Get the default connection name. |
||
33 | * |
||
34 | * @return string |
||
35 | */ |
||
36 | 2 | public function getDefaultConnection() |
|
40 | |||
41 | /** |
||
42 | * Set the default connection name. |
||
43 | * |
||
44 | * @param string $name |
||
45 | * @return $this |
||
46 | */ |
||
47 | 1 | public function setDefaultConnection($name) |
|
53 | |||
54 | /** |
||
55 | * {@inheritdoc} |
||
56 | */ |
||
57 | 1 | public function getDefaultDriver() |
|
61 | |||
62 | /** |
||
63 | * {@inheritdoc} |
||
64 | */ |
||
65 | 9 | protected function createDriver($name) |
|
66 | { |
||
67 | 9 | $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 | 9 | if (isset($this->customCreators[$name])) { |
|
72 | 5 | 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 | 4 | if (isset($config['driver']) && |
|
80 | 3 | isset($this->customCreators[$driver = $config['driver']]) |
|
81 | 4 | ) { |
|
82 | 1 | return $this->customCreators[$driver]($this->app, $this->getConfigWithoutDriver($config), $name); |
|
83 | } |
||
84 | |||
85 | 3 | return $this->createConnection($config); |
|
86 | } |
||
87 | |||
88 | /** |
||
89 | * Create a new hashid connection instance. |
||
90 | * |
||
91 | * @param array $config |
||
92 | * @return mixed |
||
93 | * |
||
94 | * @throws \InvalidArgumentException |
||
95 | */ |
||
96 | 3 | protected function createConnection(array $config) |
|
97 | { |
||
98 | 3 | if (! isset($config['driver'])) { |
|
99 | 1 | throw new InvalidArgumentException('A driver must be specified.'); |
|
100 | } |
||
101 | |||
102 | 2 | $driver = $config['driver']; |
|
103 | |||
104 | 2 | if ($this->app->bound($key = "hashid.connection.{$driver}")) { |
|
105 | 1 | if ($this->app->isShared($key)) { |
|
106 | return $this->app->make($key); |
||
107 | } |
||
108 | |||
109 | 1 | $make = method_exists($this->app, 'makeWith') ? 'makeWith' : 'make'; |
|
110 | |||
111 | 1 | return $this->app->{$make}($key, [ |
|
112 | 1 | 'app' => $this->app, |
|
113 | 1 | 'config' => $this->getConfigWithoutDriver($config), |
|
114 | 1 | ]); |
|
115 | } |
||
116 | |||
117 | 1 | throw new InvalidArgumentException("Unsupported driver [$driver]"); |
|
118 | } |
||
119 | |||
120 | /** |
||
121 | * Get connection configuration without "driver" element. |
||
122 | * |
||
123 | * @param array $config |
||
124 | * @return array |
||
125 | */ |
||
126 | 2 | protected function getConfigWithoutDriver($config) |
|
132 | } |
||
133 |