Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 148 | Details | Vergleich mit vorheriger | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
/**
4
 * Hoa
5
 *
6
 *
7
 * @license
8
 *
9
 * New BSD License
10
 *
11
 * Copyright © 2007-2017, Hoa community. All rights reserved.
12
 *
13
 * Redistribution and use in source and binary forms, with or without
14
 * modification, are permitted provided that the following conditions are met:
15
 *     * Redistributions of source code must retain the above copyright
16
 *       notice, this list of conditions and the following disclaimer.
17
 *     * Redistributions in binary form must reproduce the above copyright
18
 *       notice, this list of conditions and the following disclaimer in the
19
 *       documentation and/or other materials provided with the distribution.
20
 *     * Neither the name of the Hoa nor the names of its contributors may be
21
 *       used to endorse or promote products derived from this software without
22
 *       specific prior written permission.
23
 *
24
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
28
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34
 * POSSIBILITY OF SUCH DAMAGE.
35
 */
36
 
37
namespace Psy\Readline\Hoa;
38
 
39
/**
40
 * Class \Hoa\Console\Output.
41
 *
42
 * This class represents the output of a program. Most of the time, this is
43
 * going to be STDOUT.
44
 */
45
class ConsoleOutput implements StreamOut
46
{
47
    /**
48
     * Whether the multiplexer must be considered while writing on the output.
49
     */
50
    protected $_considerMultiplexer = false;
51
 
52
    /**
53
     * Real output stream.
54
     */
55
    protected $_output = null;
56
 
57
    /**
58
     * Wraps an `Hoa\Stream\IStream\Out` stream.
59
     */
60
    public function __construct(StreamOut $output = null)
61
    {
62
        $this->_output = $output;
63
 
64
        return;
65
    }
66
 
67
    /**
68
     * Get the real output stream.
69
     */
70
    public function getStream(): StreamOut
71
    {
72
        return $this->_output;
73
    }
74
 
75
    /**
76
     * Write n characters.
77
     */
78
    public function write(string $string, int $length)
79
    {
80
        if (0 > $length) {
81
            throw new ConsoleException('Length must be greater than 0, given %d.', 0, $length);
82
        }
83
 
84
        $out = \substr($string, 0, $length);
85
 
86
        if (true === $this->isMultiplexerConsidered()) {
87
            if (true === Console::isTmuxRunning()) {
88
                $out =
89
                    "\033Ptmux;".
90
                    \str_replace("\033", "\033\033", $out).
91
                    "\033\\";
92
            }
93
 
94
            $length = \strlen($out);
95
        }
96
 
97
        if (null === $this->_output) {
98
            echo $out;
99
        } else {
100
            $this->_output->write($out, $length);
101
        }
102
    }
103
 
104
    /**
105
     * Write a string.
106
     */
107
    public function writeString(string $string)
108
    {
109
        $string = (string) $string;
110
 
111
        return $this->write($string, \strlen($string));
112
    }
113
 
114
    /**
115
     * Write a character.
116
     */
117
    public function writeCharacter(string $character)
118
    {
119
        return $this->write((string) $character[0], 1);
120
    }
121
 
122
    /**
123
     * Write a boolean.
124
     */
125
    public function writeBoolean(bool $boolean)
126
    {
127
        return $this->write(((bool) $boolean) ? '1' : '0', 1);
128
    }
129
 
130
    /**
131
     * Write an integer.
132
     */
133
    public function writeInteger(int $integer)
134
    {
135
        $integer = (string) (int) $integer;
136
 
137
        return $this->write($integer, \strlen($integer));
138
    }
139
 
140
    /**
141
     * Write a float.
142
     */
143
    public function writeFloat(float $float)
144
    {
145
        $float = (string) (float) $float;
146
 
147
        return $this->write($float, \strlen($float));
148
    }
149
 
150
    /**
151
     * Write an array.
152
     */
153
    public function writeArray(array $array)
154
    {
155
        $array = \var_export($array, true);
156
 
157
        return $this->write($array, \strlen($array));
158
    }
159
 
160
    /**
161
     * Write a line.
162
     */
163
    public function writeLine(string $line)
164
    {
165
        if (false === $n = \strpos($line, "\n")) {
166
            return $this->write($line."\n", \strlen($line) + 1);
167
        }
168
 
169
        ++$n;
170
 
171
        return $this->write(\substr($line, 0, $n), $n);
172
    }
173
 
174
    /**
175
     * Write all, i.e. as much as possible.
176
     */
177
    public function writeAll(string $string)
178
    {
399 lars 179
        return $this->write($string ?? '', \strlen($string ?? ''));
148 lars 180
    }
181
 
182
    /**
183
     * Truncate a stream to a given length.
184
     */
185
    public function truncate(int $size): bool
186
    {
187
        return false;
188
    }
189
 
190
    /**
191
     * Consider the multiplexer (if running) while writing on the output.
192
     */
193
    public function considerMultiplexer(bool $consider): bool
194
    {
195
        $old = $this->_considerMultiplexer;
196
        $this->_considerMultiplexer = $consider;
197
 
198
        return $old;
199
    }
200
 
201
    /**
202
     * Check whether the multiplexer must be considered or not.
203
     */
204
    public function isMultiplexerConsidered(): bool
205
    {
206
        return $this->_considerMultiplexer;
207
    }
208
}