Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

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