Debian Codegraph @

lib/3rdParty/composer/illuminate/support/MultipleInstanceManager.php

raw symbols SCIP
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 * The application instance.
13 *
14 * @var \Illuminate\Contracts\Foundation\Application
15 */
16 protected $app;
17
18 /**
19 * The configuration repository instance.
20 *
21 * @var \Illuminate\Contracts\Config\Repository
22 */
23 protected $config;
24
25 /**
26 * The array of resolved instances.
27 *
28 * @var array
29 */
30 protected $instances = [];
31
32 /**
33 * The registered custom instance creators.
34 *
35 * @var array
36 */
37 protected $customCreators = [];
38
39 /**
40 * The key name of the "driver" equivalent configuration option.
41 *
42 * @var string
43 */
44 protected $driverKey = 'driver';
45
46 /**
47 * Create a new manager instance.
48 *
49 * @param \Illuminate\Contracts\Foundation\Application $app
50 */
51 public function __construct($app)
52 {
53 $this->app = $app;
54 $this->config = $app->make('config');
55 }
56
57 /**
58 * Get the default instance name.
59 *
60 * @return string
61 */
62 abstract public function getDefaultInstance();
63
64 /**
65 * Set the default instance name.
66 *
67 * @param string $name
68 * @return void
69 */
70 abstract public function setDefaultInstance($name);
71
72 /**
73 * Get the instance specific configuration.
74 *
75 * @param string $name
76 * @return array
77 */
78 abstract public function getInstanceConfig($name);
79
80 /**
81 * Get an instance by name.
82 *
83 * @param string|null $name
84 * @return mixed
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 * Attempt to get an instance from the local cache.
95 *
96 * @param string $name
97 * @return mixed
98 */
99 protected function get($name)
100 {
101 return $this->instances[$name] ?? $this->resolve($name);
102 }
103
104 /**
105 * Resolve the given instance.
106 *
107 * @param string $name
108 * @return mixed
109 *
110 * @throws \InvalidArgumentException
111 * @throws \RuntimeException
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 * Call a custom instance creator.
148 *
149 * @param array $config
150 * @return mixed
151 */
152 protected function callCustomCreator(array $config)
153 {
154 return $this->customCreators[$config[$this->driverKey]]($this->app, $config);
155 }
156
157 /**
158 * Unset the given instances.
159 *
160 * @param array|string|null $name
161 * @return $this
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 * Disconnect the given instance and remove from local cache.
178 *
179 * @param string|null $name
180 * @return void
181 */
182 public function purge($name = null)
183 {
184 $name ??= $this->getDefaultInstance();
185
186 unset($this->instances[$name]);
187 }
188
189 /**
190 * Register a custom instance creator Closure.
191 *
192 * @param string $name
193 * @param \Closure $callback
194 *
195 * @param-closure-this $this $callback
196 *
197 * @return $this
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 * Set the application instance used by the manager.
208 *
209 * @param \Illuminate\Contracts\Foundation\Application $app
210 * @return $this
211 */
212 public function setApplication($app)
213 {
214 $this->app = $app;
215
216 return $this;
217 }
218
219 /**
220 * Dynamically call the default instance.
221 *
222 * @param string $method
223 * @param array $parameters
224 * @return mixed
225 */
226 public function __call($method, $parameters)
227 {
228 return $this->instance()->$method(...$parameters);
229 }
230 }