Facade::__callStatic()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php
2
// +----------------------------------------------------------------------
3
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
4
// +----------------------------------------------------------------------
5
// | Copyright (c) 2006~2021 http://thinkphp.cn All rights reserved.
6
// +----------------------------------------------------------------------
7
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
8
// +----------------------------------------------------------------------
9
// | Author: liu21st <[email protected]>
10
// +----------------------------------------------------------------------
11
namespace think;
12
13
/**
14
 * Facade管理类
15
 */
16
class Facade
17
{
18
    /**
19
     * 始终创建新的对象实例
20
     * @var bool
21
     */
22
    protected static $alwaysNewInstance;
23
24
    /**
25
     * 创建Facade实例
26
     * @static
27
     * @access protected
28
     * @param  string $class       类名或标识
29
     * @param  array  $args        变量
30
     * @param  bool   $newInstance 是否每次创建新的实例
31
     * @return object
32
     */
33
    protected static function createFacade(string $class = '', array $args = [], bool $newInstance = false)
34
    {
35
        $class = $class ?: static::class;
36
37
        $facadeClass = static::getFacadeClass();
38
39
        if ($facadeClass) {
40
            $class = $facadeClass;
41
        }
42
43
        if (static::$alwaysNewInstance) {
44
            $newInstance = true;
45
        }
46
47
        return Container::getInstance()->make($class, $args, $newInstance);
48
    }
49
50
    /**
51
     * 获取当前Facade对应类名
52
     * @access protected
53
     * @return string
54
     */
55
    protected static function getFacadeClass()
56
    {}
57
58
    /**
59
     * 带参数实例化当前Facade类
60
     * @access public
61
     * @return object
62
     */
63
    public static function instance(...$args)
64
    {
65
        if (__CLASS__ != static::class) {
0 ignored issues
show
introduced by
The condition __CLASS__ != static::class is always false.
Loading history...
66
            return self::createFacade('', $args);
67
        }
68
    }
69
70
    /**
71
     * 调用类的实例
72
     * @access public
73
     * @param  string     $class       类名或者标识
74
     * @param  array|true $args        变量
75
     * @param  bool       $newInstance 是否每次创建新的实例
76
     * @return object
77
     */
78
    public static function make(string $class, $args = [], $newInstance = false)
79
    {
80
        if (__CLASS__ != static::class) {
0 ignored issues
show
introduced by
The condition __CLASS__ != static::class is always false.
Loading history...
81
            return self::__callStatic('make', func_get_args());
82
        }
83
84
        if (true === $args) {
85
            // 总是创建新的实例化对象
86
            $newInstance = true;
87
            $args        = [];
88
        }
89
90
        return self::createFacade($class, $args, $newInstance);
91
    }
92
93
    // 调用实际类的方法
94
    public static function __callStatic($method, $params)
95
    {
96
        return call_user_func_array([static::createFacade(), $method], $params);
97
    }
98
}
99