Passed
Push — master ( 6d80cc...7f2b1a )
by Sergey
03:18
created

Facade::clearResolvedFacadeComponent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 5
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
/**
3
 * Facades for Yii 2
4
 *
5
 * @see       https://github.com/sergeymakinen/yii2-facades
6
 * @copyright Copyright (c) 2016 Sergey Makinen (https://makinen.ru)
7
 * @license   https://github.com/sergeymakinen/yii2-facades/blob/master/LICENSE The MIT License
8
 */
9
10
namespace sergeymakinen\facades;
11
12
use yii\base\Application;
13
use yii\base\InvalidConfigException;
14
15
/**
16
 * Base facade class.
17
 */
18
abstract class Facade
19
{
20
    /**
21
     * Facaded component property accessors.
22
     *
23
     * @var array
24
     */
25
    private static $_accessors = [];
26
27
    /**
28
     * The facaded application.
29
     *
30
     * @var Application
31
     */
32
    private static $_app;
33
34
    /**
35
     * Facaded components.
36
     *
37
     * @var object[]
38
     */
39
    private static $_components = [];
40
41
    /**
42
     * Clears a resolved facade component.
43
     *
44
     * @param string $id
45
     */
46
    public static function clearResolvedFacadeComponent($id)
47
    {
48
        unset(self::$_accessors[$id]);
49
        unset(self::$_components[$id]);
50
    }
51
52
    /**
53
     * Clears all resolved facade components.
54
     */
55
    public static function clearResolvedFacadeComponents()
56
    {
57
        self::$_accessors = [];
58
        self::$_components = [];
59
    }
60
61
    /**
62
     * Returns a component ID being facaded.
63
     *
64
     * @return string
65
     */
66
    public static function getFacadeComponentId()
67
    {
68
        throw new InvalidConfigException('Facade must implement getFacadeComponentId method.');
69
    }
70
71
    /**
72
     * Returns a component being facaded.
73
     *
74
     * @return object
75
     */
76
    public static function getFacadeComponent()
77
    {
78
        $id = static::getFacadeComponentId();
79
        if (!isset(self::$_components[$id])) {
80
            self::$_components[$id] = static::getFacadeApplication()->get($id);
81
        }
82
        return self::$_components[$id];
83
    }
84
85
    /**
86
     * Returns the facaded application.
87
     *
88
     * @return Application
89
     */
90
    public static function getFacadeApplication()
91
    {
92
        if (!isset(self::$_app)) {
93
            self::$_app = \Yii::$app;
94
        }
95
        return self::$_app;
96
    }
97
98
    /**
99
     * Sets the facaded application.
100
     *
101
     * @param Application $value
102
     */
103
    public static function setFacadeApplication(Application $value)
104
    {
105
        self::$_app = $value;
106
        self::clearResolvedFacadeComponents();
107
    }
108
109
    /**
110
     * Redirects static calls to component instance calls.
111
     *
112
     * @inheritDoc
113
     */
114
    public static function __callStatic($name, $arguments)
115
    {
116
        $id = static::getFacadeComponentId();
117
        if (!isset(self::$_accessors[$id])) {
118
            self::$_accessors[$id] = [];
119
            foreach ((new \ReflectionClass(static::getFacadeComponent()))->getProperties(
120
                \ReflectionProperty::IS_PUBLIC & ~\ReflectionProperty::IS_STATIC
121
            ) as $property) {
122
                $accessor = ucfirst($property->getName());
123
                self::$_accessors[$id]['get' . $accessor] = $property->getName();
124
                self::$_accessors[$id]['set' . $accessor] = $property->getName();
125
            }
126
        }
127
        if (isset(self::$_accessors[$id][$name])) {
128
            if ($name[0] === 'g') {
129
                return static::getFacadeComponent()->{self::$_accessors[$id][$name]};
130
            } else {
131
                static::getFacadeComponent()->{self::$_accessors[$id][$name]} = reset($arguments);
132
                return null;
133
            }
134
        } else {
135
            return call_user_func_array([
136
                static::getFacadeComponent(),
137
                $name
138
            ], $arguments);
139
        }
140
    }
141
142
    /**
143
     * @inheritDoc
144
     */
145
    private function __construct()
146
    {}
147
148
    /**
149
     * @inheritDoc
150
     */
151
    private function __wakeup()
152
    {}
153
154
    /**
155
     * @inheritDoc
156
     */
157
    private function __clone()
158
    {}
159
}
160