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\Mailer\Transport\Smtp\Stream;
13
 
14
use Symfony\Component\Mailer\Exception\TransportException;
15
 
16
/**
17
 * A stream supporting remote sockets and local processes.
18
 *
19
 * @author Fabien Potencier <fabien@symfony.com>
20
 * @author Nicolas Grekas <p@tchwork.com>
21
 * @author Chris Corbyn
22
 *
23
 * @internal
24
 */
25
abstract class AbstractStream
26
{
27
    protected $stream;
28
    protected $in;
29
    protected $out;
30
 
31
    private string $debug = '';
32
 
33
    public function write(string $bytes, bool $debug = true): void
34
    {
35
        if ($debug) {
36
            foreach (explode("\n", trim($bytes)) as $line) {
37
                $this->debug .= sprintf("> %s\n", $line);
38
            }
39
        }
40
 
41
        $bytesToWrite = \strlen($bytes);
42
        $totalBytesWritten = 0;
43
        while ($totalBytesWritten < $bytesToWrite) {
44
            $bytesWritten = @fwrite($this->in, substr($bytes, $totalBytesWritten));
45
            if (false === $bytesWritten || 0 === $bytesWritten) {
46
                throw new TransportException('Unable to write bytes on the wire.');
47
            }
48
 
49
            $totalBytesWritten += $bytesWritten;
50
        }
51
    }
52
 
53
    /**
54
     * Flushes the contents of the stream (empty it) and set the internal pointer to the beginning.
55
     */
56
    public function flush(): void
57
    {
58
        fflush($this->in);
59
    }
60
 
61
    /**
62
     * Performs any initialization needed.
63
     */
64
    abstract public function initialize(): void;
65
 
66
    public function terminate(): void
67
    {
68
        $this->stream = $this->out = $this->in = null;
69
    }
70
 
71
    public function readLine(): string
72
    {
73
        if (feof($this->out)) {
74
            return '';
75
        }
76
 
77
        $line = fgets($this->out);
78
        if ('' === $line || false === $line) {
79
            $metas = stream_get_meta_data($this->out);
80
            if ($metas['timed_out']) {
81
                throw new TransportException(sprintf('Connection to "%s" timed out.', $this->getReadConnectionDescription()));
82
            }
83
            if ($metas['eof']) {
84
                throw new TransportException(sprintf('Connection to "%s" has been closed unexpectedly.', $this->getReadConnectionDescription()));
85
            }
86
        }
87
 
88
        $this->debug .= sprintf('< %s', $line);
89
 
90
        return $line;
91
    }
92
 
93
    public function getDebug(): string
94
    {
95
        $debug = $this->debug;
96
        $this->debug = '';
97
 
98
        return $debug;
99
    }
100
 
101
    public static function replace(string $from, string $to, iterable $chunks): \Generator
102
    {
103
        if ('' === $from) {
104
            yield from $chunks;
105
 
106
            return;
107
        }
108
 
109
        $carry = '';
110
        $fromLen = \strlen($from);
111
 
112
        foreach ($chunks as $chunk) {
113
            if ('' === $chunk = $carry.$chunk) {
114
                continue;
115
            }
116
 
117
            if (str_contains($chunk, $from)) {
118
                $chunk = explode($from, $chunk);
119
                $carry = array_pop($chunk);
120
 
121
                yield implode($to, $chunk).$to;
122
            } else {
123
                $carry = $chunk;
124
            }
125
 
126
            if (\strlen($carry) > $fromLen) {
127
                yield substr($carry, 0, -$fromLen);
128
                $carry = substr($carry, -$fromLen);
129
            }
130
        }
131
 
132
        if ('' !== $carry) {
133
            yield $carry;
134
        }
135
    }
136
 
137
    abstract protected function getReadConnectionDescription(): string;
138
}