OutputStream   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 112
rs 10
c 0
b 0
f 0
wmc 12
lcom 1
cbo 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A start() 0 9 3
A output() 0 6 1
A line() 0 3 1
A log() 0 12 3
A message() 0 11 2
A msg() 0 3 1
A close() 0 4 1
1
<?php
2
3
namespace Asymptix\core;
4
5
/**
6
 * Class wrapper for PHP output stream.
7
 *
8
 * @category Asymptix PHP Framework
9
 * @author Dmytro Zarezenko <[email protected]>
10
 * @copyright (c) 2015 - 2016, Dmytro Zarezenko
11
 *
12
 * @git https://github.com/Asymptix/Framework
13
 * @license http://opensource.org/licenses/MIT
14
 */
15
class OutputStream {
16
17
    /**
18
     * Message highlight types and colors.
19
     */
20
    const MSG_INFO    = "#0000CC";
21
    const MSG_DEBUG   = "#CC00CC";
22
    const MSG_SUCCESS = "#009900";
23
    const MSG_WARNING = "#CC9900";
24
    const MSG_ERROR   = "#CC0000";
25
26
    /**
27
     * Starts to flush output stream.
28
     */
29
    public static function start() {
30
        while (ob_get_level() > 0) {
31
            ob_end_flush();
32
        }
33
        if (ob_get_length() === false) {
34
            ob_start();
35
        }
36
        print("<pre>");
37
    }
38
39
    /**
40
     * Outputs of some string without end of line.
41
     *
42
     * @param string $str String to output.
43
     */
44
    public static function output($str) {
45
        print($str);
46
47
        ob_flush();
48
        flush();
49
    }
50
51
    /**
52
     * Outputs of some string with end of line.
53
     *
54
     * @param string $str String to output.
55
     */
56
    public static function line($str = "") {
57
        self::output($str . "\n");
58
    }
59
60
    /**
61
     * Outputs log string with time label before message or instead of {{time}}
62
     * label (synonym for outputLog(...)).
63
     *
64
     * @param string $str String to output.
65
     * @param string $format Format of the time label
66
     *            (optional, default: "\[Y-m-d H:i:s\]").
67
     * @param int $time Timestamp, if not passed - current time will be used.
68
     */
69
    public static function log($str, $format = "\[Y-m-d H:i:s\]", $time = null) {
70
        if (is_null($time)) {
71
            $time = time();
72
        }
73
74
        if (strpos($str, "{{time}}") !== false) {
75
            $str = str_replace("{{time}}", date($format, $time), $str);
76
            self::line($str);
77
        } else {
78
            self::line(date($format, $time) . " " . $str);
79
        }
80
    }
81
82
    /**
83
     * Outputs highlighted log string with time label before message or instead
84
     * of {{time}} label.
85
     *
86
     * @param string $msgType Type of the message constant or custom color string.
87
     * @param string $str String to output.
88
     * @param string $format Format of the time label
89
     *            (optional, default: "\[Y-m-d H:i:s\]").
90
     * @param int $time Timestamp, if not passed - current time will be used.
91
     */
92
    public static function message($msgType, $str, $format = "\[Y-m-d H:i:s\]", $time = null) {
93
        if (strpos($str, "{{time}}") === false) {
94
            $str = "{{time}} " . $str;
95
        }
96
97
        self::log(
98
            '<b><font color="' . $msgType . '">' . $str . '</font></b>',
99
            $format,
100
            $time
101
        );
102
    }
103
104
    /**
105
     * Outputs highlighted log string with time label before message or instead
106
     * of {{time}} label.
107
     *
108
     * @param string $msgType Type of the message constant or custom color string.
109
     * @param string $str String to output.
110
     * @param string $format Format of the time label
111
     *            (optional, default: "\[Y-m-d H:i:s\]").
112
     * @param int $time Timestamp, if not passed - current time will be used.
113
     */
114
    public static function msg($msgType, $str, $format = "\[Y-m-d H:i:s\]", $time = null) {
115
        self::message($msgType, $str, $format, $time);
116
    }
117
118
    /**
119
     * Closes output stream flushing.
120
     */
121
    public static function close() {
122
        print("</pre>");
123
        ob_end_flush();
124
    }
125
126
}
127