ServiceContainer::__get()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the overtrue/wechat.
5
 *
6
 * (c) overtrue <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace EasyWeChat\Kernel;
13
14
use EasyWeChat\Kernel\Providers\ConfigServiceProvider;
15
use EasyWeChat\Kernel\Providers\EventDispatcherServiceProvider;
16
use EasyWeChat\Kernel\Providers\ExtensionServiceProvider;
17
use EasyWeChat\Kernel\Providers\HttpClientServiceProvider;
18
use EasyWeChat\Kernel\Providers\LogServiceProvider;
19
use EasyWeChat\Kernel\Providers\RequestServiceProvider;
20
use EasyWeChatComposer\Traits\WithAggregator;
21
use Pimple\Container;
22
23
/**
24
 * Class ServiceContainer.
25
 *
26
 * @author overtrue <[email protected]>
27
 *
28
 * @property \EasyWeChat\Kernel\Config                          $config
29
 * @property \Symfony\Component\HttpFoundation\Request          $request
30
 * @property \GuzzleHttp\Client                                 $http_client
31
 * @property \Monolog\Logger                                    $logger
32
 * @property \Symfony\Component\EventDispatcher\EventDispatcher $events
33
 */
34
class ServiceContainer extends Container
35
{
36
    use WithAggregator;
37
38
    /**
39
     * @var string
40
     */
41
    protected $id;
42
43
    /**
44
     * @var array
45
     */
46
    protected $providers = [];
47
48
    /**
49
     * @var array
50
     */
51
    protected $defaultConfig = [];
52
53
    /**
54
     * @var array
55
     */
56
    protected $userConfig = [];
57
58
    /**
59
     * Constructor.
60
     *
61
     * @param array       $config
62
     * @param array       $prepends
63
     * @param string|null $id
64
     */
65 292
    public function __construct(array $config = [], array $prepends = [], string $id = null)
66
    {
67 292
        $this->userConfig = $config;
68
69 292
        parent::__construct($prepends);
70
71 292
        $this->registerProviders($this->getProviders());
72
73 292
        $this->id = $id;
74
75 292
        $this->aggregate();
76
77 292
        $this->events->dispatch(new Events\ApplicationInitialized($this));
78 292
    }
79
80
    /**
81
     * @return string
82
     */
83 1
    public function getId()
84
    {
85 1
        return $this->id ?? $this->id = md5(json_encode($this->userConfig));
86
    }
87
88
    /**
89
     * @return array
90
     */
91 285
    public function getConfig()
92
    {
93
        $base = [
94
            // http://docs.guzzlephp.org/en/stable/request-options.html
95
            'http' => [
96 285
                'timeout' => 30.0,
97
                'base_uri' => 'https://api.weixin.qq.com/',
98
            ],
99
        ];
100
101 285
        return array_replace_recursive($base, $this->defaultConfig, $this->userConfig);
102
    }
103
104
    /**
105
     * Return all providers.
106
     *
107
     * @return array
108
     */
109 292
    public function getProviders()
110
    {
111 292
        return array_merge([
112 292
            ConfigServiceProvider::class,
113
            LogServiceProvider::class,
114
            RequestServiceProvider::class,
115
            HttpClientServiceProvider::class,
116
            ExtensionServiceProvider::class,
117
            EventDispatcherServiceProvider::class,
118 292
        ], $this->providers);
119
    }
120
121
    /**
122
     * @param string $id
123
     * @param mixed  $value
124
     */
125 35
    public function rebind($id, $value)
126
    {
127 35
        $this->offsetUnset($id);
128 35
        $this->offsetSet($id, $value);
129 35
    }
130
131
    /**
132
     * Magic get access.
133
     *
134
     * @param string $id
135
     *
136
     * @return mixed
137
     */
138 293
    public function __get($id)
139
    {
140 293
        if ($this->shouldDelegate($id)) {
141 1
            return $this->delegateTo($id);
142
        }
143
144 293
        return $this->offsetGet($id);
145
    }
146
147
    /**
148
     * Magic set access.
149
     *
150
     * @param string $id
151
     * @param mixed  $value
152
     */
153 1
    public function __set($id, $value)
154
    {
155 1
        $this->offsetSet($id, $value);
156 1
    }
157
158
    /**
159
     * @param array $providers
160
     */
161 292
    public function registerProviders(array $providers)
162
    {
163 292
        foreach ($providers as $provider) {
164 292
            parent::register(new $provider());
165
        }
166 292
    }
167
}
168