5 * Copyright (c) 2002-2011, Sebastian Bergmann <sebastian@phpunit.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 <sebastian@phpunit.de>
40 * @copyright 2002-2011 Sebastian Bergmann <sebastian@phpunit.de>
41 * @license http://www.opensource.org/licenses/bsd-license.php BSD License
42 * @link http://www.phpunit.de/
43 * @since File available since Release 2.0.0
47 * Utility class that can print to STDOUT or write to a file.
51 * @author Sebastian Bergmann <sebastian@phpunit.de>
52 * @copyright 2002-2011 Sebastian Bergmann <sebastian@phpunit.de>
53 * @license http://www.opensource.org/licenses/bsd-license.php BSD License
54 * @version Release: 3.5.14
55 * @link http://www.phpunit.de/
56 * @since Class available since Release 2.0.0
58 abstract class PHPUnit_Util_Printer
61 * If TRUE, flush output after every write.
65 protected $autoFlush = FALSE;
80 protected $printsHTML = FALSE;
86 * @throws InvalidArgumentException
88 public function __construct($out = NULL)
91 if (is_string($out)) {
92 if (strpos($out, 'socket://') === 0) {
93 $out = explode(':', str_replace('socket://', '', $out));
95 if (sizeof($out) != 2) {
96 throw new InvalidArgumentException;
99 $this->out = fsockopen($out[0], $out[1]);
101 if (strpos($out, 'php://') === FALSE &&
102 !is_dir(dirname($out))) {
103 mkdir(dirname($out), 0777, TRUE);
106 $this->out = fopen($out, 'wt');
109 $this->outTarget = $out;
117 * Flush buffer, optionally tidy up HTML, and close output.
119 public function flush()
121 if ($this->out && $this->outTarget !== 'php://stderr') {
125 if ($this->printsHTML === TRUE &&
126 $this->outTarget !== NULL &&
127 strpos($this->outTarget, 'php://') !== 0 &&
128 strpos($this->outTarget, 'socket://') !== 0 &&
129 extension_loaded('tidy')) {
133 $this->outTarget, array('indent' => TRUE, 'wrap' => 0), 'utf8'
140 * Performs a safe, incremental flush.
142 * Do not confuse this function with the flush() function of this class,
143 * since the flush() function may close the file being written to, rendering
144 * the current object no longer usable.
146 * @since Method available since Release 3.3.0
148 public function incrementalFlush()
158 * @param string $buffer
160 public function write($buffer)
163 fwrite($this->out, $buffer);
165 if ($this->autoFlush) {
166 $this->incrementalFlush();
169 if (PHP_SAPI != 'cli') {
170 $buffer = htmlspecialchars($buffer);
175 if ($this->autoFlush) {
176 $this->incrementalFlush();
182 * Check auto-flush mode.
185 * @since Method available since Release 3.3.0
187 public function getAutoFlush()
189 return $this->autoFlush;
193 * Set auto-flushing mode.
195 * If set, *incremental* flushes will be done after each write. This should
196 * not be confused with the different effects of this class' flush() method.
198 * @param boolean $autoFlush
199 * @since Method available since Release 3.3.0
201 public function setAutoFlush($autoFlush)
203 if (is_bool($autoFlush)) {
204 $this->autoFlush = $autoFlush;
206 throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'boolean');