Passed
Push — master ( dc15a9...bf933d )
by Carlos
02:25
created

ServiceContainer::__set()   A

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 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
ccs 0
cts 0
cp 0
crap 2
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\ExtensionServiceProvider;
16
use EasyWeChat\Kernel\Providers\HttpClientServiceProvider;
17
use EasyWeChat\Kernel\Providers\LogServiceProvider;
18
use EasyWeChat\Kernel\Providers\RequestServiceProvider;
19
use Pimple\Container;
20
21
/**
22
 * Class ServiceContainer.
23
 *
24
 * @author overtrue <[email protected]>
25
 *
26
 * @property \EasyWeChat\Kernel\Config                  $config
27
 * @property \Symfony\Component\HttpFoundation\Request  $request
28
 * @property \GuzzleHttp\Client                         $http_client
29
 * @property \Monolog\Logger                            $logger
30
 */
31
class ServiceContainer extends Container
32
{
33
    /**
34
     * @var string
35
     */
36
    protected $id;
37
38
    /**
39
     * @var array
40
     */
41
    protected $providers = [];
42
43
    /**
44
     * @var array
45
     */
46
    protected $defaultConfig = [];
47
48
    /**
49
     * @var array
50
     */
51
    protected $userConfig = [];
52
53
    /**
54
     * Constructor.
55
     *
56
     * @param array       $config
57
     * @param array       $prepends
58
     * @param string|null $id
59
     */
60
    public function __construct(array $config = [], array $prepends = [], string $id = null)
61
    {
62
        $this->registerProviders($this->getProviders());
63
64
        parent::__construct($prepends);
65
66
        $this->userConfig = $config;
67
68
        $this->id = $id;
69
    }
70
71
    /**
72
     * @return string
73
     */
74
    public function getId()
75
    {
76
        return $this->id ?? $this->id = md5(json_encode($this->userConfig));
77
    }
78
79
    /**
80
     * @return array
81
     */
82
    public function getConfig()
83
    {
84
        $base = [
85
            // http://docs.guzzlephp.org/en/stable/request-options.html
86
            'http' => [
87
                'timeout' => 5.0,
88
                'base_uri' => 'https://api.weixin.qq.com/',
89
            ],
90
        ];
91
92
        return array_replace_recursive($base, $this->defaultConfig, $this->userConfig);
93
    }
94
95
    /**
96
     * Return all providers.
97
     *
98
     * @return array
99
     */
100
    public function getProviders()
101
    {
102
        return array_merge([
103
            ConfigServiceProvider::class,
104
            LogServiceProvider::class,
105
            RequestServiceProvider::class,
106
            HttpClientServiceProvider::class,
107
            ExtensionServiceProvider::class,
108
        ], $this->providers);
109
    }
110
111
    /**
112
     * Magic get access.
113
     *
114
     * @param string $id
115
     *
116
     * @return mixed
117
     */
118
    public function __get($id)
119
    {
120
        return $this->offsetGet($id);
121
    }
122
123
    /**
124
     * Magic set access.
125
     *
126
     * @param string $id
127
     * @param mixed  $value
128
     */
129
    public function __set($id, $value)
130
    {
131
        $this->offsetSet($id, $value);
132
    }
133
134
    /**
135
     * @param array $providers
136
     */
137
    public function registerProviders(array $providers)
138
    {
139
        foreach ($providers as $provider) {
140
            parent::register(new $provider());
141
        }
142
    }
143
}
144