Completed
Push — master ( b8ee44...86e435 )
by Jim
02:27
created

LogServiceProvider::formatLogConfig()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 40
rs 9.28
c 0
b 0
f 0
cc 4
nc 3
nop 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: lenovo
5
 * Date: 6/15/2018
6
 * Time: 9:22 AM
7
 */
8
9
namespace TimSDK\Foundation\ServiceProviders;
10
11
use Monolog\Handler\NullHandler;
12
use Pimple\Container;
13
use TimSDK\Foundation\Log\LogManager;
14
use TimSDK\Support\Arr;
15
16
class LogServiceProvider extends ServiceProvider
17
{
18
    /**
19
     * Registers services on the given container.
20
     *
21
     * This method should only be used to configure services and parameters.
22
     * It should not get services.
23
     *
24
     * @param Container $pimple A container instance
25
     */
26
    public function register(Container $pimple)
27
    {
28
        $pimple['logger'] = $pimple['log'] = function ($app) {
29
            $config = $this->formatLogConfig($app);
30
31
            if (!empty($config)) {
32
                $app['config']->merge($config);
33
            }
34
35
            $log = new LogManager($app);
36
37
            if (defined('PHPUNIT_RUNNING') || 'cli' === php_sapi_name()) {
38
                if (Arr::get($config, 'cli_on', false)) {
39
                    $log->setDefaultDriver('errorlog')
40
                        ->addChannels([
41
                            'errorlog' => [
42
                                'driver' => 'errorlog',
43
                                'level'  => 'info',
44
                            ],
45
                        ]);
46
                } else {
47
                    $log->driver()->pushHandler(new NullHandler());
0 ignored issues
show
Bug introduced by
The method pushHandler() does not exist on Psr\Log\LoggerInterface. It seems like you code against a sub-type of Psr\Log\LoggerInterface such as TimSDK\Foundation\Log\LogManager or Monolog\Logger. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

47
                    $log->driver()->/** @scrutinizer ignore-call */ pushHandler(new NullHandler());
Loading history...
48
                }
49
            }
50
51
            return $log;
52
        };
53
    }
54
55
    public function formatLogConfig($app)
56
    {
57
        if (empty($app['config']->get('log'))) {
58
            return [
59
                'log' => [
60
                    'cli_on'   => false,
61
                    'default'  => 'errorlog',
62
                    'channels' => [
63
                        'errorlog' => [
64
                            'driver' => 'errorlog',
65
                            'level'  => 'debug',
66
                        ],
67
                    ],
68
                ],
69
            ];
70
        }
71
        // 4.0 version
72
        if (empty($app['config']->get('log.driver'))) {
73
            return [
74
                'log' => [
75
                    'cli_on'   => false,
76
                    'default'  => 'single',
77
                    'channels' => [
78
                        'single' => [
79
                            'driver' => 'single',
80
                            'path'   => $app['config']->get('log.file') ?: \sys_get_temp_dir() . '/logs/tim-sdk.log',
81
                            'level'  => $app['config']->get('log.level', 'debug'),
82
                        ],
83
                    ],
84
                ],
85
            ];
86
        }
87
        $name = $app['config']->get('log.driver');
88
89
        return [
90
            'log' => [
91
                'cli_on'   => false,
92
                'default'  => $name,
93
                'channels' => [
94
                    $name => $app['config']->get('log'),
95
                ],
96
            ],
97
        ];
98
    }
99
}
100