Passed
Push — master ( bd72d4...a51a38 )
by 世昌
02:21
created

FileLogger::analyse()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 7
nc 3
nop 1
dl 0
loc 11
rs 9.6111
c 0
b 0
f 0
1
<?php
2
namespace nebula\component\debug\log\logger;
3
4
use nebula\component\debug\log\logger\filelogger\AttachTrait;
5
use nebula\component\debug\log\logger\filelogger\AttachAttribute;
6
7
class FileLogger extends AbstractLogger
0 ignored issues
show
Bug introduced by
The type nebula\component\debug\log\logger\AbstractLogger was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
{
9
    protected $config = [
10
        'save-path' => './logs',
11
        'max-file-size' => 2097152,
12
        'file-name' => 'latest.log',
13
        'log-level' => 'debug',
14
        'log-format' => '%time-format% - %memory-format% [%level%] %file%:%line% %message%',
15
        'debug-path' => null,
16
        'start-time' => null,
17
        'start-memory' => null,
18
    ];
19
20
    protected $file;
21
22
    /**
23
     * 属性数组
24
     *
25
     * @var array
26
     */
27
    protected $attribute;
28
29
    /**
30
     * 附加数组
31
     *
32
     * @var array
33
     */
34
    protected $attach;
35
36
    public function __construct(array $config=[], array $attribute=[])
37
    {
38
        $this->applyConfig($config);
39
        $this->attribute = $attribute;
40
    }
41
42
   
43
    public function applyConfig(array $value)
44
    {
45
        foreach ($config as $name => $value) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $config seems to be never defined.
Loading history...
46
            if (in_array($name, array_keys($defaultConfig))) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $defaultConfig seems to be never defined.
Loading history...
47
                $this->config[$name] = $config[$name] ?? $this->defaultConfig[$name];
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $config seems to never exist and therefore isset should always be false.
Loading history...
48
            } else {
49
                throw new \Exception(sprintf(__CLASS__.' : unknown config property %s', $name));
50
            }
51
        }
52
    }
53
54
    public function mergeAttribute(array $value)
55
    {
56
        $this->attribute = array_merge($this->attribute, $value);
57
    }
58
59
    public function log($level, string $message, array $context = [])
60
    {
61
        $this->attribute['level'] = $level;
62
        $this->analyse($context);
63
        $this->attribute['message'] = $this->interpolate($message, $context);
64
        $write = $this->interpolate($this->config['log-format'], $replace);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $replace seems to be never defined.
Loading history...
65
        foreach ($this->attach as $name => $value) {
66
            $write.=PHP_EOL.$name .'<' . get_class($val).'>'.PHP_EOL;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $val seems to be never defined.
Loading history...
67
            if ($value instanceof AttachAttribute) {
68
                $write.= $value->getLogAttach().PHP_EOL;
69
            } elseif ($value instanceof \Exception) {
70
                $write.= AttachTrait::dumpException($value).PHP_EOL;
71
            } else {
72
                $write.= AttachTrait::parameterToString($value).PHP_EOL;
73
            }
74
        }
75
        // write file
76
    }
77
78
    public function analyse(array $context)
79
    {
80
        $replace = [];
81
        foreach ($context as $key => $val) {
82
            if (!is_array($val) && (!is_object($val) || method_exists($val, '__toString'))) {
83
                $replace['{' . $key . '}'] = $val;
84
            } else {
85
                $this->attach[$key] = $val;
86
            }
87
        }
88
        return $replace;
89
    }
90
91
    public function interpolate(string $message, array $context)
92
    {
93
        $replace = [];
94
        foreach ($context as $key => $val) {
95
            $replace['{' . $key . '}'] = $val;
96
        }
97
        foreach ($this->attribute as $key => $val) {
98
            $replace['%' . $key . '%'] = $val;
99
        }
100
        return strtr($message, $replace);
101
    }
102
}
103