Completed
Pull Request — master (#2)
by reallyli
01:23
created

UnicomponentServiceManager::__get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: reallyli
5
 * Date: 18/10/11
6
 * Time: 上午10:37.
7
 */
8
9
namespace Reallyli\LaravelUnicomponent;
10
11
class UnicomponentServiceManager
12
{
13
    /**
14
     * @var array
15
     */
16
    private $servicesComponents = [];
17
18
    /**
19
     * @var mixed
20
     */
21
    private $config;
22
23
    /**
24
     * Construct.
25
     *
26
     * @author reallyli <[email protected]>
27
     * @since 18/10/11
28
     * @param mixed $config
29
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
30
     * 返回值类型:string,array,object,mixed(多种,不确定的),void(无返回值)
31
     */
32
    public function __construct($config)
33
    {
34
        $this->config = $config;
35
        $this->registerDefaultComponent();
36
    }
37
38
    /**
39
     * Method description:registerComponent.
40
     *
41
     * @author reallyli <[email protected]>
42
     * @since 18/10/11
43
     * @param UnicomponentServiceInterface $service
44
     * @return mixed
45
     * 返回值类型:string,array,object,mixed(多种,不确定的),void(无返回值)
46
     */
47
    public function registerComponent(UnicomponentServiceInterface $service)
48
    {
49
        $serviceProvider = $service->provider();
50
51
        $this->setServiceComponents($service->alias(), new $serviceProvider);
52
    }
53
54
    /**
55
     * Method description:registerDefaultComponent.
56
     *
57
     * @author reallyli <[email protected]>
58
     * @since 18/10/11
59
     * @return void
60
     * 返回值类型:string,array,object,mixed(多种,不确定的),void(无返回值)
61
     */
62
    public function registerDefaultComponent()
63
    {
64
        if (! $this->config) {
65
            return;
66
        }
67
68
        foreach ($this->config['components'] as $component) {
69
            $this->registerComponent(new $component['provider']);
70
        }
71
    }
72
73
    /**
74
     * Method description:setServiceInstance.
75
     *
76
     * @author reallyli <[email protected]>
77
     * @since 18/10/11
78
     * @param $alias
79
     * @param $instance
80
     * @return mixed
81
     * 返回值类型:string,array,object,mixed(多种,不确定的),void(无返回值)
82
     */
83
    protected function setServiceComponents($alias, $instance)
84
    {
85
        if (! array_key_exists($alias, $this->getServiceComponents())) {
86
            $this->servicesComponents[$alias] = $instance;
87
        }
88
89
        return $this->servicesComponents;
90
    }
91
92
    /**
93
     * Method description:getServices.
94
     *
95
     * @author reallyli <[email protected]>
96
     * @since 18/10/11
97
     * @return mixed
98
     * 返回值类型:string,array,object,mixed(多种,不确定的),void(无返回值)
99
     */
100
    public function getServiceComponents()
101
    {
102
        return $this->servicesComponents;
103
    }
104
105
    /**
106
     * Method description:getServiceComponent.
107
     *
108
     * @author reallyli <[email protected]>
109
     * @since 18/10/11
110
     * @param $serviceAlias
111
     * @return mixed
112
     * 返回值类型:string,array,object,mixed(多种,不确定的),void(无返回值)
113
     */
114
    public function getServiceComponent($serviceAlias)
115
    {
116
        return $this->getServiceComponents()[$serviceAlias] ?? null;
117
    }
118
119
    /**
120
     * Method description:__call.
121
     *
122
     * @author reallyli <[email protected]>
123
     * @since 18/10/11
124
     * @param string $method
125
     * @param array $parameters
126
     * @return mixed
127
     * 返回值类型:string,array,object,mixed(多种,不确定的),void(无返回值)
128
     */
129
    public function __call(string $method, array $parameters)
130
    {
131
        return $this->getServiceComponent($method);
132
    }
133
134
    /**
135
     * Method description:__get.
136
     *
137
     * @author reallyli <[email protected]>
138
     * @since 2018/11/20
139
     * @param string $componentName
140
     * @return mixed
141
     */
142
    public function __get(string $componentName)
143
    {
144
        return $this->getServiceComponent($componentName);
145
    }
146
}
147