3 * This file is part of PHPUnit.
5 * (c) Sebastian Bergmann <sebastian@phpunit.de>
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
12 * Utility class that can print to STDOUT or write to a file.
14 * @since Class available since Release 2.0.0
16 class PHPUnit_Util_Printer
19 * If true, flush output after every write.
23 protected $autoFlush = false;
38 protected $printsHTML = false;
45 * @throws PHPUnit_Framework_Exception
47 public function __construct($out = null)
50 if (is_string($out)) {
51 if (strpos($out, 'socket://') === 0) {
52 $out = explode(':', str_replace('socket://', '', $out));
54 if (sizeof($out) != 2) {
55 throw new PHPUnit_Framework_Exception;
58 $this->out = fsockopen($out[0], $out[1]);
60 if (strpos($out, 'php://') === false &&
61 !is_dir(dirname($out))) {
62 mkdir(dirname($out), 0777, true);
65 $this->out = fopen($out, 'wt');
68 $this->outTarget = $out;
76 * Flush buffer, optionally tidy up HTML, and close output if it's not to a php stream
78 public function flush()
80 if ($this->out && strncmp($this->outTarget, 'php://', 6) !== 0) {
84 if ($this->printsHTML === true &&
85 $this->outTarget !== null &&
86 strpos($this->outTarget, 'php://') !== 0 &&
87 strpos($this->outTarget, 'socket://') !== 0 &&
88 extension_loaded('tidy')) {
93 array('indent' => true, 'wrap' => 0),
101 * Performs a safe, incremental flush.
103 * Do not confuse this function with the flush() function of this class,
104 * since the flush() function may close the file being written to, rendering
105 * the current object no longer usable.
107 * @since Method available since Release 3.3.0
109 public function incrementalFlush()
119 * @param string $buffer
121 public function write($buffer)
124 fwrite($this->out, $buffer);
126 if ($this->autoFlush) {
127 $this->incrementalFlush();
130 if (PHP_SAPI != 'cli' && PHP_SAPI != 'phpdbg') {
131 $buffer = htmlspecialchars($buffer, ENT_SUBSTITUTE);
136 if ($this->autoFlush) {
137 $this->incrementalFlush();
143 * Check auto-flush mode.
147 * @since Method available since Release 3.3.0
149 public function getAutoFlush()
151 return $this->autoFlush;
155 * Set auto-flushing mode.
157 * If set, *incremental* flushes will be done after each write. This should
158 * not be confused with the different effects of this class' flush() method.
160 * @param bool $autoFlush
162 * @since Method available since Release 3.3.0
164 public function setAutoFlush($autoFlush)
166 if (is_bool($autoFlush)) {
167 $this->autoFlush = $autoFlush;
169 throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'boolean');