Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
/*
4
 * This file is part of the Symfony package.
5
 *
6
 * (c) Fabien Potencier <fabien@symfony.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
 
12
namespace Symfony\Component\Process\Pipes;
13
 
14
use Symfony\Component\Process\Process;
15
 
16
/**
17
 * UnixPipes implementation uses unix pipes as handles.
18
 *
19
 * @author Romain Neutron <imprec@gmail.com>
20
 *
21
 * @internal
22
 */
23
class UnixPipes extends AbstractPipes
24
{
25
    private $ttyMode;
26
    private $ptyMode;
27
    private $haveReadSupport;
28
 
29
    public function __construct(?bool $ttyMode, bool $ptyMode, mixed $input, bool $haveReadSupport)
30
    {
31
        $this->ttyMode = $ttyMode;
32
        $this->ptyMode = $ptyMode;
33
        $this->haveReadSupport = $haveReadSupport;
34
 
35
        parent::__construct($input);
36
    }
37
 
38
    public function __sleep(): array
39
    {
40
        throw new \BadMethodCallException('Cannot serialize '.__CLASS__);
41
    }
42
 
43
    public function __wakeup()
44
    {
45
        throw new \BadMethodCallException('Cannot unserialize '.__CLASS__);
46
    }
47
 
48
    public function __destruct()
49
    {
50
        $this->close();
51
    }
52
 
53
    public function getDescriptors(): array
54
    {
55
        if (!$this->haveReadSupport) {
56
            $nullstream = fopen('/dev/null', 'c');
57
 
58
            return [
59
                ['pipe', 'r'],
60
                $nullstream,
61
                $nullstream,
62
            ];
63
        }
64
 
65
        if ($this->ttyMode) {
66
            return [
67
                ['file', '/dev/tty', 'r'],
68
                ['file', '/dev/tty', 'w'],
69
                ['file', '/dev/tty', 'w'],
70
            ];
71
        }
72
 
73
        if ($this->ptyMode && Process::isPtySupported()) {
74
            return [
75
                ['pty'],
76
                ['pty'],
77
                ['pty'],
78
            ];
79
        }
80
 
81
        return [
82
            ['pipe', 'r'],
83
            ['pipe', 'w'], // stdout
84
            ['pipe', 'w'], // stderr
85
        ];
86
    }
87
 
88
    public function getFiles(): array
89
    {
90
        return [];
91
    }
92
 
93
    public function readAndWrite(bool $blocking, bool $close = false): array
94
    {
95
        $this->unblock();
96
        $w = $this->write();
97
 
98
        $read = $e = [];
99
        $r = $this->pipes;
100
        unset($r[0]);
101
 
102
        // let's have a look if something changed in streams
103
        set_error_handler($this->handleError(...));
104
        if (($r || $w) && false === stream_select($r, $w, $e, 0, $blocking ? Process::TIMEOUT_PRECISION * 1E6 : 0)) {
105
            restore_error_handler();
106
            // if a system call has been interrupted, forget about it, let's try again
107
            // otherwise, an error occurred, let's reset pipes
108
            if (!$this->hasSystemCallBeenInterrupted()) {
109
                $this->pipes = [];
110
            }
111
 
112
            return $read;
113
        }
114
        restore_error_handler();
115
 
116
        foreach ($r as $pipe) {
117
            // prior PHP 5.4 the array passed to stream_select is modified and
118
            // lose key association, we have to find back the key
119
            $read[$type = array_search($pipe, $this->pipes, true)] = '';
120
 
121
            do {
122
                $data = @fread($pipe, self::CHUNK_SIZE);
123
                $read[$type] .= $data;
124
            } while (isset($data[0]) && ($close || isset($data[self::CHUNK_SIZE - 1])));
125
 
126
            if (!isset($read[$type][0])) {
127
                unset($read[$type]);
128
            }
129
 
130
            if ($close && feof($pipe)) {
131
                fclose($pipe);
132
                unset($this->pipes[$type]);
133
            }
134
        }
135
 
136
        return $read;
137
    }
138
 
139
    public function haveReadSupport(): bool
140
    {
141
        return $this->haveReadSupport;
142
    }
143
 
144
    public function areOpen(): bool
145
    {
146
        return (bool) $this->pipes;
147
    }
148
}