SimpleFormatter::formatBatch()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 9
rs 9.6666
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
/*
3
 * This file is part of JoliCi.
4
*
5
* (c) Joel Wurtz <[email protected]>
6
*
7
* For the full copyright and license information, please view the LICENSE
8
* file that was distributed with this source code.
9
*/
10
11
namespace Joli\JoliCi\Log;
12
13
use Monolog\Formatter\FormatterInterface;
14
15
class SimpleFormatter implements FormatterInterface
16
{
17
    private $static = array();
18
19
    private $lineNumber = 0;
20
21
    /*
22
     * (non-PHPdoc) @see \Monolog\Formatter\FormatterInterface::format()
23
     */
24
    public function format(array $record)
25
    {
26
        if (isset($record['context']['clear-static'])) {
27
            $this->static = array();
28
29
            return "";
30
        }
31
32
        $message = $record['message'];
33
34
        if (isset($record['context']['static']) && $record['context']['static']) {
35
            $id      = $record['context']['static-id'];
36
37
            if (!isset($this->static[$id])) {
38
                $this->static[$id] = array(
39
                    'current_line' => $this->lineNumber,
40
                    'message'      => $message,
41
                );
42
43
                $message = sprintf("%s\n", $message);
44
            } else {
45
                $diff                         = ($this->lineNumber - $this->static[$id]['current_line']);
46
                $lastMessage                  = $this->static[$id]['message'];
47
                $this->static[$id]['message'] = $message;
48
49
                //Add space to replace old string in message
50
                if (mb_strlen($lastMessage) > mb_strlen($message)) {
51
                    $message = str_pad($message, mb_strlen($lastMessage), " ", STR_PAD_RIGHT);
52
                }
53
54
                $message = sprintf("\x0D\x1B[%sA%s\x1B[%sB\x0D", $diff, $message, $diff);
55
            }
56
        }
57
58
        if (preg_match('#\n#', $message)) {
59
            $this->lineNumber++;
60
        }
61
62
        return $message;
63
    }
64
65
    /*
66
     * (non-PHPdoc) @see \Monolog\Formatter\FormatterInterface::formatBatch()
67
     */
68
    public function formatBatch(array $records)
69
    {
70
        $message = '';
71
        foreach ($records as $record) {
72
            $message .= $this->format($record);
73
        }
74
75
        return $message;
76
    }
77
}
78