| 1 |
<?php |
| 2 |
|
| 3 |
namespace Illuminate\Support; |
| 4 |
|
| 5 |
use Closure; |
| 6 |
use Illuminate\Contracts\Container\Container; |
| 7 |
use InvalidArgumentException; |
| 8 |
|
| 9 |
abstract class Manager |
| 10 |
{ |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
protected $container; |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
protected $config; |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
protected $customCreators = []; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
protected $drivers = []; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
public function __construct(Container $container) |
| 45 |
{ |
| 46 |
$this->container = $container; |
| 47 |
$this->config = $container->make('config'); |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
abstract public function getDefaultDriver(); |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
public function driver($driver = null) |
| 66 |
{ |
| 67 |
$driver = $driver ?: $this->getDefaultDriver(); |
| 68 |
|
| 69 |
if (is_null($driver)) { |
| 70 |
throw new InvalidArgumentException(sprintf( |
| 71 |
'Unable to resolve NULL driver for [%s].', static::class |
| 72 |
)); |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
return $this->drivers[$driver] ??= $this->createDriver($driver); |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
protected function createDriver($driver) |
| 90 |
{ |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
if (isset($this->customCreators[$driver])) { |
| 95 |
return $this->callCustomCreator($driver); |
| 96 |
} |
| 97 |
|
| 98 |
$method = 'create'.Str::studly($driver).'Driver'; |
| 99 |
|
| 100 |
if (method_exists($this, $method)) { |
| 101 |
return $this->$method(); |
| 102 |
} |
| 103 |
|
| 104 |
throw new InvalidArgumentException("Driver [$driver] not supported."); |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
protected function callCustomCreator($driver) |
| 114 |
{ |
| 115 |
return $this->customCreators[$driver]($this->container); |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
public function extend($driver, Closure $callback) |
| 126 |
{ |
| 127 |
$this->customCreators[$driver] = $callback; |
| 128 |
|
| 129 |
return $this; |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
public function getDrivers() |
| 138 |
{ |
| 139 |
return $this->drivers; |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
public function getContainer() |
| 148 |
{ |
| 149 |
return $this->container; |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
public function setContainer(Container $container) |
| 159 |
{ |
| 160 |
$this->container = $container; |
| 161 |
|
| 162 |
return $this; |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
public function forgetDrivers() |
| 171 |
{ |
| 172 |
$this->drivers = []; |
| 173 |
|
| 174 |
return $this; |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
public function __call($method, $parameters) |
| 185 |
{ |
| 186 |
return $this->driver()->$method(...$parameters); |
| 187 |
} |
| 188 |
} |