Log::interpolate()   B
last analyzed

Complexity

Conditions 8
Paths 6

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 8
eloc 12
c 1
b 0
f 1
nc 6
nop 2
dl 0
loc 17
rs 8.4444
1
<?php
2
3
namespace GeminiLabs\Castor\Helpers;
4
5
use GeminiLabs\Castor\Facades\Development as DevelopmentFacade;
6
use ReflectionClass;
7
8
class Log
9
{
10
    const EMERGENCY = 'emergency';
11
    const ALERT = 'alert';
12
    const CRITICAL = 'critical';
13
    const ERROR = 'error';
14
    const WARNING = 'warning';
15
    const NOTICE = 'notice';
16
    const INFO = 'info';
17
    const DEBUG = 'debug';
18
19
    protected $file;
20
    protected $log;
21
22
    public function __construct($filename)
23
    {
24
        $this->file = $filename;
25
        $this->log = file_exists($filename)
26
            ? file_get_contents($filename)
27
            : '';
28
    }
29
30
    public function __toString()
31
    {
32
        return $this->log;
33
    }
34
35
    /**
36
     * Action must be taken immediately.
37
     * Example: Entire website down, database unavailable, etc. This should
38
     * trigger the SMS alerts and wake you up.
39
     *
40
     * @param string $message
41
     * @param array $context
42
     * @return void
43
     */
44
    public function alert($message, array $context = [])
45
    {
46
        $this->log(static::ALERT, $message, $context);
47
    }
48
49
    /**
50
     * @return void
51
     */
52
    public function clear()
53
    {
54
        $this->log = '';
55
        file_put_contents($this->file, $this->log);
56
    }
57
58
    /**
59
     * Critical conditions.
60
     * Example: Application component unavailable, unexpected exception.
61
     *
62
     * @param string $message
63
     * @param array $context
64
     * @return void
65
     */
66
    public function critical($message, array $context = [])
67
    {
68
        $this->log(static::CRITICAL, $message, $context);
69
    }
70
71
    /**
72
     * Detailed debug information.
73
     *
74
     * @param string $message
75
     * @param array $context
76
     * @return void
77
     */
78
    public function debug($message, array $context = [])
79
    {
80
        $this->log(static::DEBUG, $message, $context);
81
    }
82
83
    /**
84
     * System is unusable.
85
     *
86
     * @param string $message
87
     * @param array $context
88
     * @return void
89
     */
90
    public function emergency($message, array $context = [])
91
    {
92
        $this->log(static::EMERGENCY, $message, $context);
93
    }
94
95
    /**
96
     * Runtime errors that do not require immediate action but should typically
97
     * be logged and monitored.
98
     *
99
     * @param string $message
100
     * @param array $context
101
     * @return void
102
     */
103
    public function error($message, array $context = [])
104
    {
105
        $this->log(static::ERROR, $message, $context);
106
    }
107
108
    /**
109
     * Interesting events.
110
     * Example: User logs in, SQL logs.
111
     *
112
     * @param string $message
113
     * @param array $context
114
     * @return void
115
     */
116
    public function info($message, array $context = [])
117
    {
118
        $this->log(static::INFO, $message, $context);
119
    }
120
121
    /**
122
     * Normal but significant events.
123
     *
124
     * @param string $message
125
     * @param array $context
126
     * @return void
127
     */
128
    public function notice($message, array $context = [])
129
    {
130
        $this->log(static::NOTICE, $message, $context);
131
    }
132
133
    /**
134
     * Exceptional occurrences that are not errors.
135
     * Example: Use of deprecated APIs, poor use of an API, undesirable things
136
     * that are not necessarily wrong.
137
     *
138
     * @param string $message
139
     * @param array $context
140
     * @return void
141
     */
142
    public function warning($message, array $context = [])
143
    {
144
        $this->log(static::WARNING, $message, $context);
145
    }
146
147
    /**
148
     * @param string $message
149
     * @param array $context
150
     * @return array|string
151
     */
152
    protected function interpolate($message, array $context = [])
153
    {
154
        if (is_array($message)) {
0 ignored issues
show
introduced by
The condition is_array($message) is always false.
Loading history...
155
            return htmlspecialchars(print_r($message, true), ENT_QUOTES, 'UTF-8');
156
        }
157
        $replace = [];
158
        foreach ($context as $key => $val) {
159
            if (is_object($val) && 'DateTime' === get_class($val)) {
160
                $val = $val->format('Y-m-d H:i:s');
161
            } elseif (is_object($val) || is_array($val)) {
162
                $val = json_encode($val);
163
            } elseif (is_resource($val)) {
164
                $val = (string) $val;
165
            }
166
            $replace['{'.$key.'}'] = $val;
167
        }
168
        return strtr($message, $replace);
169
    }
170
171
    /**
172
     * @param mixed $level
173
     * @param string $message
174
     * @param array $context
175
     * @return void
176
     */
177
    protected function log($level, $message, array $context = [])
178
    {
179
        if (!in_array($level, (new ReflectionClass(__NAMESPACE__.'\Log'))->getConstants(), true)
180
            || !DevelopmentFacade::isDev()
181
        ) {
182
            return;
183
        }
184
        $date = get_date_from_gmt(gmdate('Y-m-d H:i:s'));
185
        $level = strtoupper($level);
186
        $message = $this->interpolate($message, $context);
187
        $entry = "[$date] $level: $message".PHP_EOL;
188
        file_put_contents($this->file, $entry, FILE_APPEND | LOCK_EX);
189
    }
190
}
191