]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - tests/PHPUnit/PHPUnit/Util/Printer.php
Release 6.2.0
[Github/sugarcrm.git] / tests / PHPUnit / PHPUnit / Util / Printer.php
1 <?php
2 /**
3  * PHPUnit
4  *
5  * Copyright (c) 2002-2011, Sebastian Bergmann <sebastian@phpunit.de>.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  *   * Redistributions of source code must retain the above copyright
13  *     notice, this list of conditions and the following disclaimer.
14  *
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
18  *     distribution.
19  *
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.
23  *
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.
36  *
37  * @package    PHPUnit
38  * @subpackage Util
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
44  */
45
46 /**
47  * Utility class that can print to STDOUT or write to a file.
48  *
49  * @package    PHPUnit
50  * @subpackage Util
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.13
55  * @link       http://www.phpunit.de/
56  * @since      Class available since Release 2.0.0
57  */
58 abstract class PHPUnit_Util_Printer
59 {
60     /**
61      * If TRUE, flush output after every write.
62      *
63      * @var boolean
64      */
65     protected $autoFlush = FALSE;
66
67     /**
68      * @var    resource
69      */
70     protected $out;
71
72     /**
73      * @var    string
74      */
75     protected $outTarget;
76
77     /**
78      * @var    boolean
79      */
80     protected $printsHTML = FALSE;
81
82     /**
83      * Constructor.
84      *
85      * @param  mixed $out
86      * @throws InvalidArgumentException
87      */
88     public function __construct($out = NULL)
89     {
90         if ($out !== NULL) {
91             if (is_string($out)) {
92                 if (strpos($out, 'socket://') === 0) {
93                     $out = explode(':', str_replace('socket://', '', $out));
94
95                     if (sizeof($out) != 2) {
96                         throw new InvalidArgumentException;
97                     }
98
99                     $this->out = fsockopen($out[0], $out[1]);
100                 } else {
101                     if (strpos($out, 'php://') === FALSE &&
102                         !is_dir(dirname($out))) {
103                       mkdir(dirname($out), 0777, TRUE);
104                     }
105
106                     $this->out = fopen($out, 'wt');
107                 }
108
109                 $this->outTarget = $out;
110             } else {
111                 $this->out = $out;
112             }
113         }
114     }
115
116     /**
117      * Flush buffer, optionally tidy up HTML, and close output.
118      */
119     public function flush()
120     {
121         if ($this->out && $this->outTarget !== 'php://stderr') {
122             fclose($this->out);
123         }
124
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')) {
130             file_put_contents(
131               $this->outTarget,
132               tidy_repair_file(
133                 $this->outTarget, array('indent' => TRUE, 'wrap' => 0), 'utf8'
134               )
135             );
136         }
137     }
138
139     /**
140      * Performs a safe, incremental flush.
141      *
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.
145      *
146      * @since  Method available since Release 3.3.0
147      */
148     public function incrementalFlush()
149     {
150         if ($this->out) {
151             fflush($this->out);
152         } else {
153             flush();
154         }
155     }
156
157     /**
158      * @param  string $buffer
159      */
160     public function write($buffer)
161     {
162         if ($this->out) {
163             fwrite($this->out, $buffer);
164
165             if ($this->autoFlush) {
166                 $this->incrementalFlush();
167             }
168         } else {
169             if (PHP_SAPI != 'cli') {
170                 $buffer = htmlspecialchars($buffer);
171             }
172
173             print $buffer;
174
175             if ($this->autoFlush) {
176                 $this->incrementalFlush();
177             }
178         }
179     }
180
181     /**
182      * Check auto-flush mode.
183      *
184      * @return boolean
185      * @since  Method available since Release 3.3.0
186      */
187     public function getAutoFlush()
188     {
189         return $this->autoFlush;
190     }
191
192     /**
193      * Set auto-flushing mode.
194      *
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.
197      *
198      * @param boolean $autoFlush
199      * @since  Method available since Release 3.3.0
200      */
201     public function setAutoFlush($autoFlush)
202     {
203         if (is_bool($autoFlush)) {
204             $this->autoFlush = $autoFlush;
205         } else {
206             throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'boolean');
207         }
208     }
209 }