Debian Codegraph @

lib/3rdParty/composer/illuminate/support/Facades/Facade.php

raw symbols SCIP
1 <?php
2
3 namespace Illuminate\Support\Facades;
4
5 use Closure;
6 use Illuminate\Database\Eloquent\Model;
7 use Illuminate\Support\Arr;
8 use Illuminate\Support\Benchmark;
9 use Illuminate\Support\Collection;
10 use Illuminate\Support\Js;
11 use Illuminate\Support\Number;
12 use Illuminate\Support\Str;
13 use Illuminate\Support\Testing\Fakes\Fake;
14 use Illuminate\Support\Uri;
15 use Mockery;
16 use Mockery\LegacyMockInterface;
17 use RuntimeException;
18
19 abstract class Facade
20 {
21 /**
22 * The application instance being facaded.
23 *
24 * @var \Illuminate\Contracts\Foundation\Application|null
25 */
26 protected static $app;
27
28 /**
29 * The resolved object instances.
30 *
31 * @var array
32 */
33 protected static $resolvedInstance;
34
35 /**
36 * Indicates if the resolved instance should be cached.
37 *
38 * @var bool
39 */
40 protected static $cached = true;
41
42 /**
43 * Run a Closure when the facade has been resolved.
44 *
45 * @param \Closure $callback
46 * @return void
47 */
48 public static function resolved(Closure $callback)
49 {
50 $accessor = static::getFacadeAccessor();
51
52 if (static::$app->resolved($accessor) === true) {
53 $callback(static::getFacadeRoot(), static::$app);
54 }
55
56 static::$app->afterResolving($accessor, function ($service, $app) use ($callback) {
57 $callback($service, $app);
58 });
59 }
60
61 /**
62 * Convert the facade into a Mockery spy.
63 *
64 * @return \Mockery\MockInterface
65 */
66 public static function spy()
67 {
68 if (! static::isMock()) {
69 $class = static::getMockableClass();
70
71 return tap($class ? Mockery::spy($class) : Mockery::spy(), function ($spy) {
72 static::swap($spy);
73 });
74 }
75 }
76
77 /**
78 * Initiate a partial mock on the facade.
79 *
80 * @return \Mockery\MockInterface
81 */
82 public static function partialMock()
83 {
84 $name = static::getFacadeAccessor();
85
86 $mock = static::isMock()
87 ? static::$resolvedInstance[$name]
88 : static::createFreshMockInstance();
89
90 return $mock->makePartial();
91 }
92
93 /**
94 * Initiate a mock expectation on the facade.
95 *
96 * @return \Mockery\Expectation
97 */
98 public static function shouldReceive()
99 {
100 $name = static::getFacadeAccessor();
101
102 $mock = static::isMock()
103 ? static::$resolvedInstance[$name]
104 : static::createFreshMockInstance();
105
106 return $mock->shouldReceive(...func_get_args());
107 }
108
109 /**
110 * Initiate a mock expectation on the facade.
111 *
112 * @return \Mockery\Expectation
113 */
114 public static function expects()
115 {
116 $name = static::getFacadeAccessor();
117
118 $mock = static::isMock()
119 ? static::$resolvedInstance[$name]
120 : static::createFreshMockInstance();
121
122 return $mock->expects(...func_get_args());
123 }
124
125 /**
126 * Create a fresh mock instance for the given class.
127 *
128 * @return \Mockery\MockInterface
129 */
130 protected static function createFreshMockInstance()
131 {
132 return tap(static::createMock(), function ($mock) {
133 static::swap($mock);
134
135 $mock->shouldAllowMockingProtectedMethods();
136 });
137 }
138
139 /**
140 * Create a fresh mock instance for the given class.
141 *
142 * @return \Mockery\MockInterface
143 */
144 protected static function createMock()
145 {
146 $class = static::getMockableClass();
147
148 return $class ? Mockery::mock($class) : Mockery::mock();
149 }
150
151 /**
152 * Determines whether a mock is set as the instance of the facade.
153 *
154 * @return bool
155 */
156 protected static function isMock()
157 {
158 $name = static::getFacadeAccessor();
159
160 return isset(static::$resolvedInstance[$name]) &&
161 static::$resolvedInstance[$name] instanceof LegacyMockInterface;
162 }
163
164 /**
165 * Get the mockable class for the bound instance.
166 *
167 * @return string|null
168 */
169 protected static function getMockableClass()
170 {
171 if ($root = static::getFacadeRoot()) {
172 return get_class($root);
173 }
174 }
175
176 /**
177 * Hotswap the underlying instance behind the facade.
178 *
179 * @param mixed $instance
180 * @return void
181 */
182 public static function swap($instance)
183 {
184 static::$resolvedInstance[static::getFacadeAccessor()] = $instance;
185
186 if (isset(static::$app)) {
187 static::$app->instance(static::getFacadeAccessor(), $instance);
188 }
189 }
190
191 /**
192 * Determines whether a "fake" has been set as the facade instance.
193 *
194 * @return bool
195 */
196 public static function isFake()
197 {
198 $name = static::getFacadeAccessor();
199
200 return isset(static::$resolvedInstance[$name]) &&
201 static::$resolvedInstance[$name] instanceof Fake;
202 }
203
204 /**
205 * Get the root object behind the facade.
206 *
207 * @return mixed
208 */
209 public static function getFacadeRoot()
210 {
211 return static::resolveFacadeInstance(static::getFacadeAccessor());
212 }
213
214 /**
215 * Get the registered name of the component.
216 *
217 * @return string
218 *
219 * @throws \RuntimeException
220 */
221 protected static function getFacadeAccessor()
222 {
223 throw new RuntimeException('Facade does not implement getFacadeAccessor method.');
224 }
225
226 /**
227 * Resolve the facade root instance from the container.
228 *
229 * @param string $name
230 * @return mixed
231 */
232 protected static function resolveFacadeInstance($name)
233 {
234 if (isset(static::$resolvedInstance[$name])) {
235 return static::$resolvedInstance[$name];
236 }
237
238 if (static::$app) {
239 if (static::$cached) {
240 return static::$resolvedInstance[$name] = static::$app[$name];
241 }
242
243 return static::$app[$name];
244 }
245 }
246
247 /**
248 * Clear a resolved facade instance.
249 *
250 * @param ?string $name
251 * @return void
252 */
253 public static function clearResolvedInstance($name = null)
254 {
255 unset(static::$resolvedInstance[$name ?? static::getFacadeAccessor()]);
256 }
257
258 /**
259 * Clear all of the resolved instances.
260 *
261 * @return void
262 */
263 public static function clearResolvedInstances()
264 {
265 static::$resolvedInstance = [];
266 }
267
268 /**
269 * Get the application default aliases.
270 *
271 * @return \Illuminate\Support\Collection
272 */
273 public static function defaultAliases()
274 {
275 return new Collection([
276 'App' => App::class,
277 'Arr' => Arr::class,
278 'Artisan' => Artisan::class,
279 'Auth' => Auth::class,
280 'Benchmark' => Benchmark::class,
281 'Blade' => Blade::class,
282 'Broadcast' => Broadcast::class,
283 'Bus' => Bus::class,
284 'Cache' => Cache::class,
285 'Concurrency' => Concurrency::class,
286 'Config' => Config::class,
287 'Context' => Context::class,
288 'Cookie' => Cookie::class,
289 'Crypt' => Crypt::class,
290 'Date' => Date::class,
291 'DB' => DB::class,
292 'Eloquent' => Model::class,
293 'Event' => Event::class,
294 'File' => File::class,
295 'Gate' => Gate::class,
296 'Hash' => Hash::class,
297 'Http' => Http::class,
298 'Js' => Js::class,
299 'Lang' => Lang::class,
300 'Log' => Log::class,
301 'Mail' => Mail::class,
302 'Notification' => Notification::class,
303 'Number' => Number::class,
304 'Password' => Password::class,
305 'Process' => Process::class,
306 'Queue' => Queue::class,
307 'RateLimiter' => RateLimiter::class,
308 'Redirect' => Redirect::class,
309 'Request' => Request::class,
310 'Response' => Response::class,
311 'Route' => Route::class,
312 'Schedule' => Schedule::class,
313 'Schema' => Schema::class,
314 'Session' => Session::class,
315 'Storage' => Storage::class,
316 'Str' => Str::class,
317 'Uri' => Uri::class,
318 'URL' => URL::class,
319 'Validator' => Validator::class,
320 'View' => View::class,
321 'Vite' => Vite::class,
322 ]);
323 }
324
325 /**
326 * Get the application instance behind the facade.
327 *
328 * @return \Illuminate\Contracts\Foundation\Application|null
329 */
330 public static function getFacadeApplication()
331 {
332 return static::$app;
333 }
334
335 /**
336 * Set the application instance.
337 *
338 * @param \Illuminate\Contracts\Foundation\Application|null $app
339 * @return void
340 */
341 public static function setFacadeApplication($app)
342 {
343 static::$app = $app;
344 }
345
346 /**
347 * Handle dynamic, static calls to the object.
348 *
349 * @param string $method
350 * @param array $args
351 * @return mixed
352 *
353 * @throws \RuntimeException
354 */
355 public static function __callStatic($method, $args)
356 {
357 $instance = static::getFacadeRoot();
358
359 if (! $instance) {
360 throw new RuntimeException('A facade root has not been set.');
361 }
362
363 return $instance->$method(...$args);
364 }
365 }