Facade::getFacadeRoot()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace GeminiLabs\Castor;
4
5
use RuntimeException;
6
7
abstract class Facade
8
{
9
    /**
10
     * The application instance being facaded.
11
     *
12
     * @var Application
13
     */
14
    protected static $app;
15
16
    /**
17
     * The resolved object instances.
18
     *
19
     * @var array
20
     */
21
    protected static $resolvedInstance;
22
23
    /**
24
     * Handle dynamic, static calls to the object.
25
     *
26
     * @param string $method
27
     * @param array  $args
28
     *
29
     * @return mixed
30
     * @throws \RuntimeException
31
     */
32
    public static function __callStatic($method, $args)
33
    {
34
        $instance = static::getFacadeRoot();
35
36
        if (!$instance) {
37
            throw new RuntimeException('A facade root has not been set.');
38
        }
39
40
        return $instance->$method(...$args);
41
    }
42
43
    /**
44
     * Clear all of the resolved instances.
45
     *
46
     * @return void
47
     */
48
    public static function clearResolvedInstances()
49
    {
50
        static::$resolvedInstance = [];
51
    }
52
53
    /**
54
     * Get the application instance behind the facade.
55
     *
56
     * @return Application
57
     */
58
    public static function getFacadeApplication()
59
    {
60
        return static::$app;
61
    }
62
63
    /**
64
     * Get the root object behind the facade.
65
     *
66
     * @return mixed
67
     */
68
    public static function getFacadeRoot()
69
    {
70
        return static::resolveFacadeInstance(static::getFacadeAccessor());
71
    }
72
73
    /**
74
     * Set the application instance.
75
     *
76
     * @return void
77
     */
78
    public static function setFacadeApplication(Application $app)
79
    {
80
        static::$app = $app;
81
    }
82
83
    /**
84
     * Get the registered name of the component.
85
     *
86
     * @return string
87
     *
88
     * @throws \RuntimeException
89
     */
90
    protected static function getFacadeAccessor()
91
    {
92
        throw new RuntimeException('Facade does not implement getFacadeAccessor method.');
93
    }
94
95
    /**
96
     * Resolve the facade root instance from the container.
97
     *
98
     * @param string|object $name
99
     *
100
     * @return mixed
101
     */
102
    protected static function resolveFacadeInstance($name)
103
    {
104
        if (is_object($name)) {
105
            return $name;
106
        }
107
108
        if (isset(static::$resolvedInstance[$name])) {
109
            return static::$resolvedInstance[$name];
110
        }
111
112
        return static::$resolvedInstance[$name] = static::$app->make($name);
113
    }
114
}
115