Completed
Pull Request — master (#2)
by reallyli
02:11
created

UnicomponentServiceManager::__call()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 2
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
        throw_unless($this->config, '\Exception', 'Please check if the configuration file is published');
65
66
        foreach ($this->config['components'] as $component) {
67
            $this->registerComponent(new $component['provider']);
68
        }
69
    }
70
71
    /**
72
     * Method description:setServiceInstance.
73
     *
74
     * @author reallyli <[email protected]>
75
     * @since 18/10/11
76
     * @param $alias
77
     * @param $instance
78
     * @return mixed
79
     * 返回值类型:string,array,object,mixed(多种,不确定的),void(无返回值)
80
     */
81
    protected function setServiceComponents($alias, $instance)
82
    {
83
        if (! array_key_exists($alias, $this->getServiceComponents())) {
84
            $this->servicesComponents[$alias] = $instance;
85
        }
86
87
        return $this->servicesComponents;
88
    }
89
90
    /**
91
     * Method description:getServices.
92
     *
93
     * @author reallyli <[email protected]>
94
     * @since 18/10/11
95
     * @return mixed
96
     * 返回值类型:string,array,object,mixed(多种,不确定的),void(无返回值)
97
     */
98
    public function getServiceComponents()
99
    {
100
        return $this->servicesComponents;
101
    }
102
103
    /**
104
     * Method description:getServiceComponent.
105
     *
106
     * @author reallyli <[email protected]>
107
     * @since 18/10/11
108
     * @param $serviceAlias
109
     * @return mixed
110
     * 返回值类型:string,array,object,mixed(多种,不确定的),void(无返回值)
111
     */
112
    public function getServiceComponent($serviceAlias)
113
    {
114
        return $this->getServiceComponents()[$serviceAlias] ?? null;
115
    }
116
117
    /**
118
     * Method description:__call.
119
     *
120
     * @author reallyli <[email protected]>
121
     * @since 18/10/11
122
     * @param string $method
123
     * @param array $parameters
124
     * @return mixed
125
     * 返回值类型:string,array,object,mixed(多种,不确定的),void(无返回值)
126
     */
127
    public function __call(string $method, array $parameters)
128
    {
129
        return $this->getServiceComponent($method);
130
    }
131
132
    /**
133
     * Method description:__get
134
     *
135
     * @author reallyli <[email protected]>
136
     * @since 2018/11/20
137
     * @param string $componentName
138
     * @return mixed
139
     */
140
    public function __get(string $componentName)
141
    {
142
        return $this->getServiceComponent($componentName);
143
    }
144
}
145