Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/**
3
 * PHPUnit
4
 *
5
 * Copyright (c) 2002-2010, Sebastian Bergmann <sb@sebastian-bergmann.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
 * @category   Testing
38
 * @package    PHPUnit
39
 * @author     Sebastian Bergmann <sb@sebastian-bergmann.de>
40
 * @copyright  2002-2010 Sebastian Bergmann <sb@sebastian-bergmann.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
require_once 'PHPUnit/Util/Filter.php';
47
 
48
PHPUnit_Util_Filter::addFileToFilter(__FILE__, 'PHPUNIT');
49
 
50
/**
51
 * Utility class that can print to STDOUT or write to a file.
52
 *
53
 * @category   Testing
54
 * @package    PHPUnit
55
 * @author     Sebastian Bergmann <sb@sebastian-bergmann.de>
56
 * @copyright  2002-2010 Sebastian Bergmann <sb@sebastian-bergmann.de>
57
 * @license    http://www.opensource.org/licenses/bsd-license.php  BSD License
58
 * @version    Release: 3.4.15
59
 * @link       http://www.phpunit.de/
60
 * @since      Class available since Release 2.0.0
61
 */
62
abstract class PHPUnit_Util_Printer
63
{
64
    /**
65
     * If TRUE, flush output after every write.
66
     *
67
     * @var boolean
68
     */
69
    protected $autoFlush = FALSE;
70
 
71
    /**
72
     * @var    resource
73
     */
74
    protected $out;
75
 
76
    /**
77
     * @var    string
78
     */
79
    protected $outTarget;
80
 
81
    /**
82
     * @var    boolean
83
     */
84
    protected $printsHTML = FALSE;
85
 
86
    /**
87
     * Constructor.
88
     *
89
     * @param  mixed $out
90
     * @throws InvalidArgumentException
91
     */
92
    public function __construct($out = NULL)
93
    {
94
        if ($out !== NULL) {
95
            if (is_string($out)) {
96
                if (strpos($out, 'socket://') === 0) {
97
                    $out = explode(':', str_replace('socket://', '', $out));
98
 
99
                    if (sizeof($out) != 2) {
100
                        throw new InvalidArgumentException;
101
                    }
102
 
103
                    $this->out = fsockopen($out[0], $out[1]);
104
                } else {
105
                    $this->out = fopen($out, 'wt');
106
                }
107
 
108
                $this->outTarget = $out;
109
            } else {
110
                $this->out = $out;
111
            }
112
        }
113
    }
114
 
115
    /**
116
     * Flush buffer, optionally tidy up HTML, and close output.
117
     */
118
    public function flush()
119
    {
120
        if ($this->out !== NULL && $this->outTarget !== 'php://stderr') {
121
            fclose($this->out);
122
        }
123
 
124
        if ($this->printsHTML === TRUE && $this->outTarget !== NULL &&
125
            strpos($this->outTarget, 'php://') !== 0 &&
126
            strpos($this->outTarget, 'socket://') !== 0 &&
127
            extension_loaded('tidy')) {
128
            file_put_contents(
129
              $this->outTarget,
130
              tidy_repair_file(
131
                $this->outTarget, array('indent' => TRUE, 'wrap' => 0), 'utf8'
132
              )
133
            );
134
        }
135
    }
136
 
137
    /**
138
     * Performs a safe, incremental flush.
139
     *
140
     * Do not confuse this function with the flush() function of this class,
141
     * since the flush() function may close the file being written to, rendering
142
     * the current object no longer usable.
143
     *
144
     * @since  Method available since Release 3.3.0
145
     */
146
    public function incrementalFlush()
147
    {
148
        if ($this->out !== NULL) {
149
            fflush($this->out);
150
        } else {
151
            flush();
152
        }
153
    }
154
 
155
    /**
156
     * @param  string $buffer
157
     */
158
    public function write($buffer)
159
    {
160
        if ($this->out !== NULL) {
161
            fwrite($this->out, $buffer);
162
 
163
            if ($this->autoFlush) {
164
                $this->incrementalFlush();
165
            }
166
        } else {
167
            if (PHP_SAPI != 'cli') {
168
                $buffer = htmlspecialchars($buffer);
169
            }
170
 
171
            print $buffer;
172
 
173
            if ($this->autoFlush) {
174
                $this->incrementalFlush();
175
            }
176
        }
177
    }
178
 
179
    /**
180
     * Check auto-flush mode.
181
     *
182
     * @return boolean
183
     * @since  Method available since Release 3.3.0
184
     */
185
    public function getAutoFlush()
186
    {
187
        return $this->autoFlush;
188
    }
189
 
190
    /**
191
     * Set auto-flushing mode.
192
     *
193
     * If set, *incremental* flushes will be done after each write. This should
194
     * not be confused with the different effects of this class' flush() method.
195
     *
196
     * @param boolean $autoFlush
197
     * @since  Method available since Release 3.3.0
198
     */
199
    public function setAutoFlush($autoFlush)
200
    {
201
        if (is_bool($autoFlush)) {
202
            $this->autoFlush = $autoFlush;
203
        } else {
204
            throw PHPUnit_Util_InvalidArgumentHelper::factory(1, 'boolean');
205
        }
206
    }
207
}
208
?>