Completed
Pull Request — master (#298)
by Carlos
04:32
created

Application::setExceptionHandler()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
ccs 0
cts 3
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
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
/**
13
 * Application.php.
14
 *
15
 * Part of Overtrue\WeChat.
16
 *
17
 * For the full copyright and license information, please view the LICENSE
18
 * file that was distributed with this source code.
19
 *
20
 * @author    overtrue <[email protected]>
21
 * @copyright 2015
22
 *
23
 * @link      https://github.com/overtrue/wechat
24
 * @link      http://overtrue.me
25
 */
26
namespace EasyWeChat\Foundation;
27
28
use Doctrine\Common\Cache\FilesystemCache;
29
use EasyWeChat\Core\AccessToken;
30
use EasyWeChat\Support\Log;
31
use Monolog\Handler\NullHandler;
32
use Monolog\Handler\StreamHandler;
33
use Monolog\Logger;
34
use Pimple\Container;
35
use Symfony\Component\HttpFoundation\Request;
36
37
/**
38
 * Class Application.
39
 */
40
class Application extends Container
41
{
42
    /**
43
     * Service Providers.
44
     *
45
     * @var array
46
     */
47
    protected $providers = [
48
        ServiceProviders\ServerServiceProvider::class,
49
        ServiceProviders\UserServiceProvider::class,
50
        ServiceProviders\JsServiceProvider::class,
51
        ServiceProviders\OAuthServiceProvider::class,
52
        ServiceProviders\MenuServiceProvider::class,
53
        ServiceProviders\NoticeServiceProvider::class,
54
        ServiceProviders\MaterialServiceProvider::class,
55
        ServiceProviders\StaffServiceProvider::class,
56
        ServiceProviders\UrlServiceProvider::class,
57
        ServiceProviders\QRCodeServiceProvider::class,
58
        ServiceProviders\SemanticServiceProvider::class,
59
        ServiceProviders\StatsServiceProvider::class,
60
        ServiceProviders\PaymentServiceProvider::class,
61
        ServiceProviders\POIServiceProvider::class,
62
        ServiceProviders\ReplyServiceProvider::class,
63
        ServiceProviders\BroadcastServiceProvider::class,
64
    ];
65
66
    /**
67
     * Application constructor.
68
     *
69
     * @param array $config
70
     */
71 3
    public function __construct($config)
72
    {
73 3
        parent::__construct();
74
75
        $this['config'] = function () use ($config) {
76 3
            return new Config($config);
77
        };
78
79 3
        if ($this['config']['debug']) {
80
            error_reporting(E_ALL);
81
        }
82
83 3
        $this->registerProviders();
84 3
        $this->registerBase();
85 3
        $this->initializeLogger();
86
87 3
        Log::debug('Current configuration:', $config);
88 3
    }
89
90
    /**
91
     * Add a provider.
92
     *
93
     * @param string $provider
94
     *
95
     * @return Application
96
     */
97 1
    public function addProvider($provider)
98
    {
99 1
        array_push($this->providers, $provider);
100
101 1
        return $this;
102
    }
103
104
    /**
105
     * Set providers.
106
     *
107
     * @param array $providers
108
     */
109 1
    public function setProviders(array $providers)
110
    {
111 1
        $this->providers = [];
112
113 1
        foreach ($providers as $provider) {
114 1
            $this->addProvider($provider);
115 1
        }
116 1
    }
117
118
    /**
119
     * Return all providers.
120
     *
121
     * @return array
122
     */
123 2
    public function getProviders()
124
    {
125 2
        return $this->providers;
126
    }
127
128
    /**
129
     * Magic get access. 
130
     *
131
     * @param string $id
132
     *
133
     * @return mixed
134
     */
135 1
    public function __get($id)
136
    {
137 1
        return $this->offsetGet($id);
138
    }
139
140
    /**
141
     * Magic set access.
142
     *
143
     * @param string $id
144
     * @param mixed  $value
145
     */
146 1
    public function __set($id, $value)
147
    {
148 1
        return $this->offsetSet($id, $value);
149
    }
150
151
    /**
152
     * Register providers.
153
     */
154 3
    private function registerProviders()
155
    {
156 3
        foreach ($this->providers as $provider) {
157 3
            $this->register(new $provider());
158 3
        }
159 3
    }
160
161
    /**
162
     * Register basic providers.
163
     */
164 3
    private function registerBase()
165
    {
166
        $this['request'] = function () {
167
            return Request::createFromGlobals();
168
        };
169
170
        $this['cache'] = function () {
171
            return new FilesystemCache(sys_get_temp_dir());
172
        };
173
174
        $this['access_token'] = function () {
175
           return new AccessToken(
176
               $this['config']['app_id'],
177
               $this['config']['secret'],
178
               $this['cache']
179
           );
180
        };
181 3
    }
182
183
    /**
184
     * Initialize logger.
185
     */
186 3
    private function initializeLogger()
187
    {
188 3
        $logger = new Logger('easywechat');
189
190 3
        if (!$this['config']['debug'] || defined('PHPUNIT_RUNNING')) {
191 3
            $logger->pushHandler(new NullHandler());
192 3
        } elseif ($logFile = $this['config']['log.file']) {
193
            $logger->pushHandler(new StreamHandler($logFile, $this['config']->get('log.level', Logger::WARNING)));
194
        }
195
196 3
        Log::setLogger($logger);
197 3
    }
198
}
199