Passed
Push — master ( 6ce998...91641e )
by Fabio
05:04
created

TStdOutWriter   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 38
rs 10
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __destruct() 0 6 3
A flush() 0 15 3
1
<?php
2
/**
3
 * TStdOutWriter class file
4
 *
5
 * @author Brad Anderson <[email protected]>
6
 * @link https://github.com/pradosoft/prado
7
 * @license https://github.com/pradosoft/prado/blob/master/LICENSE
8
 */
9
10
namespace Prado\IO;
11
12
/**
13
 * TStdOutWriter class.
14
 *
15
 * TStdOutWriter extends TTextWriter to fwrite the buffer to STDOUT when {@see flush}ed.
16
 * This allows for testing of the Shell output.
17
 *
18
 * STDOUT is only defined in the CLI.  When processing a PHP web page, this opens
19
 * a new handle to 'php://stdout'.
20
 *
21
 * @author Brad Anderson <[email protected]>
22
 * @since 4.2.3
23
 */
24
class TStdOutWriter extends TTextWriter
25
{
26
	/** The file path to open a data stream in memory */
27
	public const STDOUT_URI = 'php://stdout';
28
29
	/** @var mixed the Standard Out stream handle */
30
	private mixed $_stdout = null;
31
32
	/**
33
	 * Closes the StdOut handle when STDOUT is not defined
34
	 */
35
	public function __destruct()
36
	{
37
		if (!defined('STDOUT') && $this->_stdout) {
38
			fclose($this->_stdout);
39
		}
40
		parent::__destruct();
41
	}
42
43
	/**
44
	 * Flushes the content that has been written.
45
	 * @return string the content being flushed.
46
	 */
47
	public function flush()
48
	{
49
		$str = parent::flush();
50
51
		if (!$this->_stdout) {
52
			if (!defined('STDOUT')) {
53
				$this->_stdout = fopen(TStdOutWriter::STDOUT_URI, 'wb');
54
			} else {
55
				$this->_stdout = STDOUT;
56
			}
57
		}
58
59
		fwrite($this->_stdout, $str);
60
61
		return $str;
62
	}
63
}
64