Yaffs site version 1.1
[yaffs-website] / vendor / phpunit / phpunit / src / Util / Printer.php
1 <?php
2 /*
3  * This file is part of PHPUnit.
4  *
5  * (c) Sebastian Bergmann <sebastian@phpunit.de>
6  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10
11 /**
12  * Utility class that can print to STDOUT or write to a file.
13  *
14  * @since Class available since Release 2.0.0
15  */
16 class PHPUnit_Util_Printer
17 {
18     /**
19      * If true, flush output after every write.
20      *
21      * @var bool
22      */
23     protected $autoFlush = false;
24
25     /**
26      * @var resource
27      */
28     protected $out;
29
30     /**
31      * @var string
32      */
33     protected $outTarget;
34
35     /**
36      * @var bool
37      */
38     protected $printsHTML = false;
39
40     /**
41      * Constructor.
42      *
43      * @param mixed $out
44      *
45      * @throws PHPUnit_Framework_Exception
46      */
47     public function __construct($out = null)
48     {
49         if ($out !== null) {
50             if (is_string($out)) {
51                 if (strpos($out, 'socket://') === 0) {
52                     $out = explode(':', str_replace('socket://', '', $out));
53
54                     if (sizeof($out) != 2) {
55                         throw new PHPUnit_Framework_Exception;
56                     }
57
58                     $this->out = fsockopen($out[0], $out[1]);
59                 } else {
60                     if (strpos($out, 'php://') === false &&
61                         !is_dir(dirname($out))) {
62                         mkdir(dirname($out), 0777, true);
63                     }
64
65                     $this->out = fopen($out, 'wt');
66                 }
67
68                 $this->outTarget = $out;
69             } else {
70                 $this->out = $out;
71             }
72         }
73     }
74
75     /**
76      * Flush buffer, optionally tidy up HTML, and close output if it's not to a php stream
77      */
78     public function flush()
79     {
80         if ($this->out && strncmp($this->outTarget, 'php://', 6) !== 0) {
81             fclose($this->out);
82         }
83
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')) {
89             file_put_contents(
90                 $this->outTarget,
91                 tidy_repair_file(
92                     $this->outTarget,
93                     array('indent' => true, 'wrap' => 0),
94                     'utf8'
95                 )
96             );
97         }
98     }
99
100     /**
101      * Performs a safe, incremental flush.
102      *
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.
106      *
107      * @since  Method available since Release 3.3.0
108      */
109     public function incrementalFlush()
110     {
111         if ($this->out) {
112             fflush($this->out);
113         } else {
114             flush();
115         }
116     }
117
118     /**
119      * @param string $buffer
120      */
121     public function write($buffer)
122     {
123         if ($this->out) {
124             fwrite($this->out, $buffer);
125
126             if ($this->autoFlush) {
127                 $this->incrementalFlush();
128             }
129         } else {
130             if (PHP_SAPI != 'cli' && PHP_SAPI != 'phpdbg') {
131                 $buffer = htmlspecialchars($buffer, ENT_SUBSTITUTE);
132             }
133
134             print $buffer;
135
136             if ($this->autoFlush) {
137                 $this->incrementalFlush();
138             }
139         }
140     }
141
142     /**
143      * Check auto-flush mode.
144      *
145      * @return bool
146      *
147      * @since  Method available since Release 3.3.0
148      */
149     public function getAutoFlush()
150     {
151         return $this->autoFlush;
152     }
153
154     /**
155      * Set auto-flushing mode.
156      *
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.
159      *
160      * @param bool $autoFlush
161      *
162      * @since  Method available since Release 3.3.0
163      */
164     public function setAutoFlush($autoFlush)
165     {
166         if (is_bool($autoFlush)) {
167             $this->autoFlush = $autoFlush;
168         } else {
169             throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'boolean');
170         }
171     }
172 }