5 * Copyright (c) 2002-2009, Sebastian Bergmann <sb@sebastian-bergmann.de>.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * * Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * * Neither the name of Sebastian Bergmann nor the names of his
21 * contributors may be used to endorse or promote products derived
22 * from this software without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
39 * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
40 * @copyright 2002-2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
41 * @license http://www.opensource.org/licenses/bsd-license.php BSD License
43 * @link http://www.phpunit.de/
44 * @since File available since Release 2.0.0
47 require_once 'PHPUnit/Util/Filter.php';
49 PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
52 * Utility class that can print to STDOUT or write to a file.
56 * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
57 * @copyright 2002-2009 Sebastian Bergmann <sb@sebastian-bergmann.de>
58 * @license http://www.opensource.org/licenses/bsd-license.php BSD License
59 * @version Release: 3.3.17
60 * @link http://www.phpunit.de/
61 * @since Class available since Release 2.0.0
64 abstract class PHPUnit_Util_Printer
67 * If TRUE, flush output after every write.
71 protected $autoFlush = FALSE;
86 protected $printsHTML = FALSE;
92 * @throws InvalidArgumentException
94 public function __construct($out = NULL)
97 if (is_string($out)) {
98 if (strpos($out, 'socket://') === 0) {
99 $out = explode(':', str_replace('socket://', '', $out));
101 if (sizeof($out) != 2) {
102 throw new InvalidArgumentException;
105 $this->out = fsockopen($out[0], $out[1]);
107 $this->out = fopen($out, 'wt');
110 $this->outTarget = $out;
118 * Flush buffer, optionally tidy up HTML, and close output.
121 public function flush()
123 if ($this->out !== NULL) {
127 if ($this->printsHTML === TRUE && $this->outTarget !== NULL &&
128 strpos($this->outTarget, 'php://') !== 0 &&
129 strpos($this->outTarget, 'socket://') !== 0 &&
130 extension_loaded('tidy')) {
134 $this->outTarget, array('indent' => TRUE, 'wrap' => 0), 'utf8'
141 * Performs a safe, incremental flush.
143 * Do not confuse this function with the flush() function of this class,
144 * since the flush() function may close the file being written to, rendering
145 * the current object no longer usable.
147 * @since Method available since Release 3.3.0
149 public function incrementalFlush()
151 if ($this->out !== NULL) {
159 * @param string $buffer
161 public function write($buffer)
163 if ($this->out !== NULL) {
164 fwrite($this->out, $buffer);
166 if ($this->autoFlush) {
167 $this->incrementalFlush();
170 if (PHP_SAPI != 'cli') {
171 $buffer = htmlspecialchars($buffer);
176 if ($this->autoFlush) {
177 $this->incrementalFlush();
183 * Check auto-flush mode.
186 * @since Method available since Release 3.3.0
188 public function getAutoFlush()
190 return $this->autoFlush;
194 * Set auto-flushing mode.
196 * If set, *incremental* flushes will be done after each write. This should
197 * not be confused with the different effects of this class' flush() method.
199 * @param boolean $autoFlush
200 * @since Method available since Release 3.3.0
202 public function setAutoFlush($autoFlush)
204 if (is_bool($autoFlush)) {
205 $this->autoFlush = $autoFlush;
207 throw new InvalidArgumentException;