| 1 |
<?php |
| 2 |
|
| 3 |
namespace Illuminate\Support; |
| 4 |
|
| 5 |
use Closure; |
| 6 |
use InvalidArgumentException; |
| 7 |
use RuntimeException; |
| 8 |
|
| 9 |
abstract class MultipleInstanceManager |
| 10 |
{ |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
protected $app; |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
protected $config; |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
protected $instances = []; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
protected $customCreators = []; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
protected $driverKey = 'driver'; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public function __construct($app) |
| 52 |
{ |
| 53 |
$this->app = $app; |
| 54 |
$this->config = $app->make('config'); |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
abstract public function getDefaultInstance(); |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
abstract public function setDefaultInstance($name); |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
abstract public function getInstanceConfig($name); |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
public function instance($name = null) |
| 87 |
{ |
| 88 |
$name = $name ?: $this->getDefaultInstance(); |
| 89 |
|
| 90 |
return $this->instances[$name] = $this->get($name); |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
protected function get($name) |
| 100 |
{ |
| 101 |
return $this->instances[$name] ?? $this->resolve($name); |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
protected function resolve($name) |
| 114 |
{ |
| 115 |
$config = $this->getInstanceConfig($name); |
| 116 |
|
| 117 |
if (is_null($config)) { |
| 118 |
throw new InvalidArgumentException("Instance [{$name}] is not defined."); |
| 119 |
} |
| 120 |
|
| 121 |
if (! array_key_exists($this->driverKey, $config)) { |
| 122 |
throw new RuntimeException("Instance [{$name}] does not specify a {$this->driverKey}."); |
| 123 |
} |
| 124 |
|
| 125 |
$driverName = $config[$this->driverKey]; |
| 126 |
|
| 127 |
if (isset($this->customCreators[$driverName])) { |
| 128 |
return $this->callCustomCreator($config); |
| 129 |
} else { |
| 130 |
$createMethod = 'create'.ucfirst($driverName).ucfirst($this->driverKey); |
| 131 |
|
| 132 |
if (method_exists($this, $createMethod)) { |
| 133 |
return $this->{$createMethod}($config); |
| 134 |
} |
| 135 |
|
| 136 |
$createMethod = 'create'.Str::studly($driverName).ucfirst($this->driverKey); |
| 137 |
|
| 138 |
if (method_exists($this, $createMethod)) { |
| 139 |
return $this->{$createMethod}($config); |
| 140 |
} |
| 141 |
|
| 142 |
throw new InvalidArgumentException("Instance {$this->driverKey} [{$config[$this->driverKey]}] is not supported."); |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
protected function callCustomCreator(array $config) |
| 153 |
{ |
| 154 |
return $this->customCreators[$config[$this->driverKey]]($this->app, $config); |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
public function forgetInstance($name = null) |
| 164 |
{ |
| 165 |
$name ??= $this->getDefaultInstance(); |
| 166 |
|
| 167 |
foreach ((array) $name as $instanceName) { |
| 168 |
if (isset($this->instances[$instanceName])) { |
| 169 |
unset($this->instances[$instanceName]); |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
return $this; |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
public function purge($name = null) |
| 183 |
{ |
| 184 |
$name ??= $this->getDefaultInstance(); |
| 185 |
|
| 186 |
unset($this->instances[$name]); |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
public function extend($name, Closure $callback) |
| 200 |
{ |
| 201 |
$this->customCreators[$name] = $callback->bindTo($this, $this); |
| 202 |
|
| 203 |
return $this; |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
public function setApplication($app) |
| 213 |
{ |
| 214 |
$this->app = $app; |
| 215 |
|
| 216 |
return $this; |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
public function __call($method, $parameters) |
| 227 |
{ |
| 228 |
return $this->instance()->$method(...$parameters); |
| 229 |
} |
| 230 |
} |