Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php declare(strict_types=1);
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
namespace PHPUnit\Util;
11
 
12
use const ENT_COMPAT;
13
use const ENT_SUBSTITUTE;
14
use const PHP_SAPI;
15
use function assert;
16
use function count;
17
use function dirname;
18
use function explode;
19
use function fclose;
20
use function fopen;
21
use function fsockopen;
22
use function fwrite;
23
use function htmlspecialchars;
24
use function is_resource;
25
use function is_string;
26
use function sprintf;
27
use function str_replace;
28
use function strncmp;
29
use function strpos;
30
 
31
/**
32
 * @internal This class is not covered by the backward compatibility promise for PHPUnit
33
 */
34
class Printer
35
{
36
    /**
37
     * @psalm-var closed-resource|resource
38
     */
39
    private $stream;
40
 
41
    /**
42
     * @var bool
43
     */
44
    private $isPhpStream;
45
 
46
    /**
47
     * @param null|resource|string $out
48
     *
49
     * @throws Exception
50
     */
51
    public function __construct($out = null)
52
    {
53
        if (is_resource($out)) {
54
            $this->stream = $out;
55
 
56
            return;
57
        }
58
 
59
        if (!is_string($out)) {
60
            return;
61
        }
62
 
63
        if (strpos($out, 'socket://') === 0) {
64
            $tmp = explode(':', str_replace('socket://', '', $out));
65
 
66
            if (count($tmp) !== 2) {
67
                throw new Exception(
68
                    sprintf(
69
                        '"%s" does not match "socket://hostname:port" format',
70
                        $out
71
                    )
72
                );
73
            }
74
 
75
            $this->stream = fsockopen($tmp[0], (int) $tmp[1]);
76
 
77
            return;
78
        }
79
 
80
        if (strpos($out, 'php://') === false && !Filesystem::createDirectory(dirname($out))) {
81
            throw new Exception(
82
                sprintf(
83
                    'Directory "%s" was not created',
84
                    dirname($out)
85
                )
86
            );
87
        }
88
 
89
        $this->stream      = fopen($out, 'wb');
90
        $this->isPhpStream = strncmp($out, 'php://', 6) !== 0;
91
    }
92
 
93
    public function write(string $buffer): void
94
    {
95
        if ($this->stream) {
96
            assert(is_resource($this->stream));
97
 
98
            fwrite($this->stream, $buffer);
99
        } else {
100
            if (PHP_SAPI !== 'cli' && PHP_SAPI !== 'phpdbg') {
101
                $buffer = htmlspecialchars($buffer, ENT_COMPAT | ENT_SUBSTITUTE);
102
            }
103
 
104
            print $buffer;
105
        }
106
    }
107
 
108
    public function flush(): void
109
    {
110
        if ($this->stream && $this->isPhpStream) {
111
            assert(is_resource($this->stream));
112
 
113
            fclose($this->stream);
114
        }
115
    }
116
}