Completed
Push — master ( 86fce8...3dc221 )
by Vítězslav
05:51
created

ToMemory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php
2
/**
3
 * Class to Log messages.
4
 *
5
 * @author    Vitex <[email protected]>
6
 * @copyright 2009-2016 [email protected] (G)
7
 */
8
9
namespace Ease\Logger;
10
11
class ToMemory extends \Ease\Atom  implements Loggingable
12
{
13
    /**
14
     * Předvolená metoda logování.
15
     *
16
     * @var string
17
     */
18
    public $logType = 'memory';
19
20
    /**
21
     * Adresář do kterého se zapisují logy.
22
     *
23
     * @var string dirpath
24
     */
25
    public $logPrefix = null;
26
27
    /**
28
     * úroveň logování.
29
     *
30
     * @var string - silent,debug
31
     */
32
    public $logLevel = 'debug';
33
34
    /**
35
     * Hodnoty pro obarvování logu.
36
     *
37
     * @var array
38
     */
39
    public $logStyles = [
40
        'notice' => 'color: black;',
41
        'success' => 'color: #2C5F23;',
42
        'message' => 'color: #2C5F23;',
43
        'warning' => 'color: #AB250E;',
44
        'error' => 'color: red;',
45
        'debug' => 'font-style: italic;',
46
        'report' => 'font-wight: bold;',
47
        'info' => 'color: blue;',
48
    ];
49
50
    /**
51
     * Odkaz na vlastnící objekt.
52
     *
53
     * @var \Ease\Sand
54
     */
55
    public $parentObject = null;
56
57
    /**
58
     * ID naposledy ulozene zpravy.
59
     *
60
     * @var int unsigned
61
     */
62
    private $messageID = 0;
63
64
    /**
65
     * Saves obejct instace (singleton...).
66
     */
67
    private static $instance = null;
68
69
    /**
70
     * Pri vytvareni objektu pomoci funkce singleton (ma stejne parametry, jako
71
     * konstruktor) se bude v ramci behu programu pouzivat pouze jedna jeho
72
     * instance (ta prvni).
73
     *
74
     * @link http://docs.php.net/en/language.oop5.patterns.html Dokumentace a
75
     * priklad
76
     */
77
    public static function singleton()
78
    {
79
        if (!isset(self::$instance)) {
80
            $class           = __CLASS__;
81
            self::$instance = new $class();
82
        }
83
84
        return self::$instance;
85
    }
86
87
    /**
88
     * Zapise zapravu do logu.
89
     *
90
     * @param string $caller  název volajícího objektu
91
     * @param string $message zpráva
92
     * @param string $type    typ zprávy (success|info|error|warning|*)
93
     *
94
     * @return bool byl report zapsán ?
95
     */
96
    public function addToLog($caller, $message, $type = 'message')
97
    {
98
        ++$this->messageID;
99
        if ((($type != 'error') && ($this->logLevel == 'silent')) ||
100
            (($type == 'debug') && ($this->logLevel != 'debug'))) {
101
            return;
102
        }
103
104
        $this->statusMessages[$type][$this->messageID] = $message;
105
106
        $message = htmlspecialchars_decode(strip_tags(stripslashes($message)));
107
108
        $logLine = date(DATE_ATOM).' ('.$caller.') '.str_replace(['notice', 'message',
109
                'debug', 'report', 'error', 'warning', 'success', 'info', 'mail',],
110
                ['**', '##', '@@', '::'], $type).' '.$message."\n";
111
        if (!isset($this->logStyles[$type])) {
112
            $type = 'notice';
113
        }
114
        if ($this->logType == 'console' || $this->logType == 'both') {
115
            if (php_sapi_name() == 'cli') {
116
                echo $logLine;
117
            } else {
118
                echo '<div style="'.$this->logStyles[$type].'">'.$logLine."</div>\n";
119
                flush();
120
            }
121
        }
122
123
        return true;
124
    }
125
126
    /**
127
     * Vrací styl logování.
128
     *
129
     * @param string $logType typ logu warning|info|success|error|notice|*
130
     *
131
     * @return string
132
     */
133
    public function getLogStyle($logType = 'notice')
134
    {
135
        if (key_exists($logType, $this->logStyles)) {
136
            return $this->logStyles[$logType];
137
        } else {
138
            return '';
139
        }
140
    }
141
142
    /**
143
     * Do Nothing.
144
     */
145
    public function flush()
146
    {
147
        //Hotfix
148
    }
149
}
150