@@ -18,29 +18,29 @@ |
||
18 | 18 | */ |
19 | 19 | interface Swift_OutputByteStream |
20 | 20 | { |
21 | - /** |
|
22 | - * Reads $length bytes from the stream into a string and moves the pointer |
|
23 | - * through the stream by $length. |
|
24 | - * |
|
25 | - * If less bytes exist than are requested the remaining bytes are given instead. |
|
26 | - * If no bytes are remaining at all, boolean false is returned. |
|
27 | - * |
|
28 | - * @param int $length |
|
29 | - * |
|
30 | - * @throws Swift_IoException |
|
31 | - * |
|
32 | - * @return string|bool |
|
33 | - */ |
|
34 | - public function read($length); |
|
21 | + /** |
|
22 | + * Reads $length bytes from the stream into a string and moves the pointer |
|
23 | + * through the stream by $length. |
|
24 | + * |
|
25 | + * If less bytes exist than are requested the remaining bytes are given instead. |
|
26 | + * If no bytes are remaining at all, boolean false is returned. |
|
27 | + * |
|
28 | + * @param int $length |
|
29 | + * |
|
30 | + * @throws Swift_IoException |
|
31 | + * |
|
32 | + * @return string|bool |
|
33 | + */ |
|
34 | + public function read($length); |
|
35 | 35 | |
36 | - /** |
|
37 | - * Move the internal read pointer to $byteOffset in the stream. |
|
38 | - * |
|
39 | - * @param int $byteOffset |
|
40 | - * |
|
41 | - * @throws Swift_IoException |
|
42 | - * |
|
43 | - * @return bool |
|
44 | - */ |
|
45 | - public function setReadPointer($byteOffset); |
|
36 | + /** |
|
37 | + * Move the internal read pointer to $byteOffset in the stream. |
|
38 | + * |
|
39 | + * @param int $byteOffset |
|
40 | + * |
|
41 | + * @throws Swift_IoException |
|
42 | + * |
|
43 | + * @return bool |
|
44 | + */ |
|
45 | + public function setReadPointer($byteOffset); |
|
46 | 46 | } |
@@ -15,12 +15,12 @@ |
||
15 | 15 | */ |
16 | 16 | interface Swift_CharacterReaderFactory |
17 | 17 | { |
18 | - /** |
|
19 | - * Returns a CharacterReader suitable for the charset applied. |
|
20 | - * |
|
21 | - * @param string $charset |
|
22 | - * |
|
23 | - * @return Swift_CharacterReader |
|
24 | - */ |
|
25 | - public function getReaderFor($charset); |
|
18 | + /** |
|
19 | + * Returns a CharacterReader suitable for the charset applied. |
|
20 | + * |
|
21 | + * @param string $charset |
|
22 | + * |
|
23 | + * @return Swift_CharacterReader |
|
24 | + */ |
|
25 | + public function getReaderFor($charset); |
|
26 | 26 | } |
@@ -15,90 +15,90 @@ |
||
15 | 15 | */ |
16 | 16 | interface Swift_KeyCache |
17 | 17 | { |
18 | - /** Mode for replacing existing cached data */ |
|
19 | - const MODE_WRITE = 1; |
|
18 | + /** Mode for replacing existing cached data */ |
|
19 | + const MODE_WRITE = 1; |
|
20 | 20 | |
21 | - /** Mode for appending data to the end of existing cached data */ |
|
22 | - const MODE_APPEND = 2; |
|
21 | + /** Mode for appending data to the end of existing cached data */ |
|
22 | + const MODE_APPEND = 2; |
|
23 | 23 | |
24 | - /** |
|
25 | - * Set a string into the cache under $itemKey for the namespace $nsKey. |
|
26 | - * |
|
27 | - * @see MODE_WRITE, MODE_APPEND |
|
28 | - * |
|
29 | - * @param string $nsKey |
|
30 | - * @param string $itemKey |
|
31 | - * @param string $string |
|
32 | - * @param int $mode |
|
33 | - */ |
|
34 | - public function setString($nsKey, $itemKey, $string, $mode); |
|
24 | + /** |
|
25 | + * Set a string into the cache under $itemKey for the namespace $nsKey. |
|
26 | + * |
|
27 | + * @see MODE_WRITE, MODE_APPEND |
|
28 | + * |
|
29 | + * @param string $nsKey |
|
30 | + * @param string $itemKey |
|
31 | + * @param string $string |
|
32 | + * @param int $mode |
|
33 | + */ |
|
34 | + public function setString($nsKey, $itemKey, $string, $mode); |
|
35 | 35 | |
36 | - /** |
|
37 | - * Set a ByteStream into the cache under $itemKey for the namespace $nsKey. |
|
38 | - * |
|
39 | - * @see MODE_WRITE, MODE_APPEND |
|
40 | - * |
|
41 | - * @param string $nsKey |
|
42 | - * @param string $itemKey |
|
43 | - * @param int $mode |
|
44 | - */ |
|
45 | - public function importFromByteStream($nsKey, $itemKey, Swift_OutputByteStream $os, $mode); |
|
36 | + /** |
|
37 | + * Set a ByteStream into the cache under $itemKey for the namespace $nsKey. |
|
38 | + * |
|
39 | + * @see MODE_WRITE, MODE_APPEND |
|
40 | + * |
|
41 | + * @param string $nsKey |
|
42 | + * @param string $itemKey |
|
43 | + * @param int $mode |
|
44 | + */ |
|
45 | + public function importFromByteStream($nsKey, $itemKey, Swift_OutputByteStream $os, $mode); |
|
46 | 46 | |
47 | - /** |
|
48 | - * Provides a ByteStream which when written to, writes data to $itemKey. |
|
49 | - * |
|
50 | - * NOTE: The stream will always write in append mode. |
|
51 | - * If the optional third parameter is passed all writes will go through $is. |
|
52 | - * |
|
53 | - * @param string $nsKey |
|
54 | - * @param string $itemKey |
|
55 | - * @param Swift_InputByteStream $is optional input stream |
|
56 | - * |
|
57 | - * @return Swift_InputByteStream |
|
58 | - */ |
|
59 | - public function getInputByteStream($nsKey, $itemKey, Swift_InputByteStream $is = null); |
|
47 | + /** |
|
48 | + * Provides a ByteStream which when written to, writes data to $itemKey. |
|
49 | + * |
|
50 | + * NOTE: The stream will always write in append mode. |
|
51 | + * If the optional third parameter is passed all writes will go through $is. |
|
52 | + * |
|
53 | + * @param string $nsKey |
|
54 | + * @param string $itemKey |
|
55 | + * @param Swift_InputByteStream $is optional input stream |
|
56 | + * |
|
57 | + * @return Swift_InputByteStream |
|
58 | + */ |
|
59 | + public function getInputByteStream($nsKey, $itemKey, Swift_InputByteStream $is = null); |
|
60 | 60 | |
61 | - /** |
|
62 | - * Get data back out of the cache as a string. |
|
63 | - * |
|
64 | - * @param string $nsKey |
|
65 | - * @param string $itemKey |
|
66 | - * |
|
67 | - * @return string |
|
68 | - */ |
|
69 | - public function getString($nsKey, $itemKey); |
|
61 | + /** |
|
62 | + * Get data back out of the cache as a string. |
|
63 | + * |
|
64 | + * @param string $nsKey |
|
65 | + * @param string $itemKey |
|
66 | + * |
|
67 | + * @return string |
|
68 | + */ |
|
69 | + public function getString($nsKey, $itemKey); |
|
70 | 70 | |
71 | - /** |
|
72 | - * Get data back out of the cache as a ByteStream. |
|
73 | - * |
|
74 | - * @param string $nsKey |
|
75 | - * @param string $itemKey |
|
76 | - * @param Swift_InputByteStream $is stream to write the data to |
|
77 | - */ |
|
78 | - public function exportToByteStream($nsKey, $itemKey, Swift_InputByteStream $is); |
|
71 | + /** |
|
72 | + * Get data back out of the cache as a ByteStream. |
|
73 | + * |
|
74 | + * @param string $nsKey |
|
75 | + * @param string $itemKey |
|
76 | + * @param Swift_InputByteStream $is stream to write the data to |
|
77 | + */ |
|
78 | + public function exportToByteStream($nsKey, $itemKey, Swift_InputByteStream $is); |
|
79 | 79 | |
80 | - /** |
|
81 | - * Check if the given $itemKey exists in the namespace $nsKey. |
|
82 | - * |
|
83 | - * @param string $nsKey |
|
84 | - * @param string $itemKey |
|
85 | - * |
|
86 | - * @return bool |
|
87 | - */ |
|
88 | - public function hasKey($nsKey, $itemKey); |
|
80 | + /** |
|
81 | + * Check if the given $itemKey exists in the namespace $nsKey. |
|
82 | + * |
|
83 | + * @param string $nsKey |
|
84 | + * @param string $itemKey |
|
85 | + * |
|
86 | + * @return bool |
|
87 | + */ |
|
88 | + public function hasKey($nsKey, $itemKey); |
|
89 | 89 | |
90 | - /** |
|
91 | - * Clear data for $itemKey in the namespace $nsKey if it exists. |
|
92 | - * |
|
93 | - * @param string $nsKey |
|
94 | - * @param string $itemKey |
|
95 | - */ |
|
96 | - public function clearKey($nsKey, $itemKey); |
|
90 | + /** |
|
91 | + * Clear data for $itemKey in the namespace $nsKey if it exists. |
|
92 | + * |
|
93 | + * @param string $nsKey |
|
94 | + * @param string $itemKey |
|
95 | + */ |
|
96 | + public function clearKey($nsKey, $itemKey); |
|
97 | 97 | |
98 | - /** |
|
99 | - * Clear all data in the namespace $nsKey if it exists. |
|
100 | - * |
|
101 | - * @param string $nsKey |
|
102 | - */ |
|
103 | - public function clearAll($nsKey); |
|
98 | + /** |
|
99 | + * Clear all data in the namespace $nsKey if it exists. |
|
100 | + * |
|
101 | + * @param string $nsKey |
|
102 | + */ |
|
103 | + public function clearAll($nsKey); |
|
104 | 104 | } |
@@ -15,162 +15,162 @@ |
||
15 | 15 | */ |
16 | 16 | abstract class Swift_ByteStream_AbstractFilterableInputStream implements Swift_InputByteStream, Swift_Filterable |
17 | 17 | { |
18 | - /** |
|
19 | - * Write sequence. |
|
20 | - */ |
|
21 | - protected $sequence = 0; |
|
22 | - |
|
23 | - /** |
|
24 | - * StreamFilters. |
|
25 | - * |
|
26 | - * @var Swift_StreamFilter[] |
|
27 | - */ |
|
28 | - private $filters = []; |
|
29 | - |
|
30 | - /** |
|
31 | - * A buffer for writing. |
|
32 | - */ |
|
33 | - private $writeBuffer = ''; |
|
34 | - |
|
35 | - /** |
|
36 | - * Bound streams. |
|
37 | - * |
|
38 | - * @var Swift_InputByteStream[] |
|
39 | - */ |
|
40 | - private $mirrors = []; |
|
41 | - |
|
42 | - /** |
|
43 | - * Commit the given bytes to the storage medium immediately. |
|
44 | - * |
|
45 | - * @param string $bytes |
|
46 | - */ |
|
47 | - abstract protected function doCommit($bytes); |
|
48 | - |
|
49 | - /** |
|
50 | - * Flush any buffers/content with immediate effect. |
|
51 | - */ |
|
52 | - abstract protected function flush(); |
|
53 | - |
|
54 | - /** |
|
55 | - * Add a StreamFilter to this InputByteStream. |
|
56 | - * |
|
57 | - * @param string $key |
|
58 | - */ |
|
59 | - public function addFilter(Swift_StreamFilter $filter, $key) |
|
60 | - { |
|
61 | - $this->filters[$key] = $filter; |
|
62 | - } |
|
63 | - |
|
64 | - /** |
|
65 | - * Remove an already present StreamFilter based on its $key. |
|
66 | - * |
|
67 | - * @param string $key |
|
68 | - */ |
|
69 | - public function removeFilter($key) |
|
70 | - { |
|
71 | - unset($this->filters[$key]); |
|
72 | - } |
|
73 | - |
|
74 | - /** |
|
75 | - * Writes $bytes to the end of the stream. |
|
76 | - * |
|
77 | - * @param string $bytes |
|
78 | - * |
|
79 | - * @throws Swift_IoException |
|
80 | - * |
|
81 | - * @return int |
|
82 | - */ |
|
83 | - public function write($bytes) |
|
84 | - { |
|
85 | - $this->writeBuffer .= $bytes; |
|
86 | - foreach ($this->filters as $filter) { |
|
87 | - if ($filter->shouldBuffer($this->writeBuffer)) { |
|
88 | - return; |
|
89 | - } |
|
90 | - } |
|
91 | - $this->doWrite($this->writeBuffer); |
|
92 | - |
|
93 | - return ++$this->sequence; |
|
94 | - } |
|
95 | - |
|
96 | - /** |
|
97 | - * For any bytes that are currently buffered inside the stream, force them |
|
98 | - * off the buffer. |
|
99 | - * |
|
100 | - * @throws Swift_IoException |
|
101 | - */ |
|
102 | - public function commit() |
|
103 | - { |
|
104 | - $this->doWrite($this->writeBuffer); |
|
105 | - } |
|
106 | - |
|
107 | - /** |
|
108 | - * Attach $is to this stream. |
|
109 | - * |
|
110 | - * The stream acts as an observer, receiving all data that is written. |
|
111 | - * All {@link write()} and {@link flushBuffers()} operations will be mirrored. |
|
112 | - */ |
|
113 | - public function bind(Swift_InputByteStream $is) |
|
114 | - { |
|
115 | - $this->mirrors[] = $is; |
|
116 | - } |
|
117 | - |
|
118 | - /** |
|
119 | - * Remove an already bound stream. |
|
120 | - * |
|
121 | - * If $is is not bound, no errors will be raised. |
|
122 | - * If the stream currently has any buffered data it will be written to $is |
|
123 | - * before unbinding occurs. |
|
124 | - */ |
|
125 | - public function unbind(Swift_InputByteStream $is) |
|
126 | - { |
|
127 | - foreach ($this->mirrors as $k => $stream) { |
|
128 | - if ($is === $stream) { |
|
129 | - if ('' !== $this->writeBuffer) { |
|
130 | - $stream->write($this->writeBuffer); |
|
131 | - } |
|
132 | - unset($this->mirrors[$k]); |
|
133 | - } |
|
134 | - } |
|
135 | - } |
|
136 | - |
|
137 | - /** |
|
138 | - * Flush the contents of the stream (empty it) and set the internal pointer |
|
139 | - * to the beginning. |
|
140 | - * |
|
141 | - * @throws Swift_IoException |
|
142 | - */ |
|
143 | - public function flushBuffers() |
|
144 | - { |
|
145 | - if ('' !== $this->writeBuffer) { |
|
146 | - $this->doWrite($this->writeBuffer); |
|
147 | - } |
|
148 | - $this->flush(); |
|
149 | - |
|
150 | - foreach ($this->mirrors as $stream) { |
|
151 | - $stream->flushBuffers(); |
|
152 | - } |
|
153 | - } |
|
154 | - |
|
155 | - /** Run $bytes through all filters */ |
|
156 | - private function filter($bytes) |
|
157 | - { |
|
158 | - foreach ($this->filters as $filter) { |
|
159 | - $bytes = $filter->filter($bytes); |
|
160 | - } |
|
161 | - |
|
162 | - return $bytes; |
|
163 | - } |
|
164 | - |
|
165 | - /** Just write the bytes to the stream */ |
|
166 | - private function doWrite($bytes) |
|
167 | - { |
|
168 | - $this->doCommit($this->filter($bytes)); |
|
169 | - |
|
170 | - foreach ($this->mirrors as $stream) { |
|
171 | - $stream->write($bytes); |
|
172 | - } |
|
173 | - |
|
174 | - $this->writeBuffer = ''; |
|
175 | - } |
|
18 | + /** |
|
19 | + * Write sequence. |
|
20 | + */ |
|
21 | + protected $sequence = 0; |
|
22 | + |
|
23 | + /** |
|
24 | + * StreamFilters. |
|
25 | + * |
|
26 | + * @var Swift_StreamFilter[] |
|
27 | + */ |
|
28 | + private $filters = []; |
|
29 | + |
|
30 | + /** |
|
31 | + * A buffer for writing. |
|
32 | + */ |
|
33 | + private $writeBuffer = ''; |
|
34 | + |
|
35 | + /** |
|
36 | + * Bound streams. |
|
37 | + * |
|
38 | + * @var Swift_InputByteStream[] |
|
39 | + */ |
|
40 | + private $mirrors = []; |
|
41 | + |
|
42 | + /** |
|
43 | + * Commit the given bytes to the storage medium immediately. |
|
44 | + * |
|
45 | + * @param string $bytes |
|
46 | + */ |
|
47 | + abstract protected function doCommit($bytes); |
|
48 | + |
|
49 | + /** |
|
50 | + * Flush any buffers/content with immediate effect. |
|
51 | + */ |
|
52 | + abstract protected function flush(); |
|
53 | + |
|
54 | + /** |
|
55 | + * Add a StreamFilter to this InputByteStream. |
|
56 | + * |
|
57 | + * @param string $key |
|
58 | + */ |
|
59 | + public function addFilter(Swift_StreamFilter $filter, $key) |
|
60 | + { |
|
61 | + $this->filters[$key] = $filter; |
|
62 | + } |
|
63 | + |
|
64 | + /** |
|
65 | + * Remove an already present StreamFilter based on its $key. |
|
66 | + * |
|
67 | + * @param string $key |
|
68 | + */ |
|
69 | + public function removeFilter($key) |
|
70 | + { |
|
71 | + unset($this->filters[$key]); |
|
72 | + } |
|
73 | + |
|
74 | + /** |
|
75 | + * Writes $bytes to the end of the stream. |
|
76 | + * |
|
77 | + * @param string $bytes |
|
78 | + * |
|
79 | + * @throws Swift_IoException |
|
80 | + * |
|
81 | + * @return int |
|
82 | + */ |
|
83 | + public function write($bytes) |
|
84 | + { |
|
85 | + $this->writeBuffer .= $bytes; |
|
86 | + foreach ($this->filters as $filter) { |
|
87 | + if ($filter->shouldBuffer($this->writeBuffer)) { |
|
88 | + return; |
|
89 | + } |
|
90 | + } |
|
91 | + $this->doWrite($this->writeBuffer); |
|
92 | + |
|
93 | + return ++$this->sequence; |
|
94 | + } |
|
95 | + |
|
96 | + /** |
|
97 | + * For any bytes that are currently buffered inside the stream, force them |
|
98 | + * off the buffer. |
|
99 | + * |
|
100 | + * @throws Swift_IoException |
|
101 | + */ |
|
102 | + public function commit() |
|
103 | + { |
|
104 | + $this->doWrite($this->writeBuffer); |
|
105 | + } |
|
106 | + |
|
107 | + /** |
|
108 | + * Attach $is to this stream. |
|
109 | + * |
|
110 | + * The stream acts as an observer, receiving all data that is written. |
|
111 | + * All {@link write()} and {@link flushBuffers()} operations will be mirrored. |
|
112 | + */ |
|
113 | + public function bind(Swift_InputByteStream $is) |
|
114 | + { |
|
115 | + $this->mirrors[] = $is; |
|
116 | + } |
|
117 | + |
|
118 | + /** |
|
119 | + * Remove an already bound stream. |
|
120 | + * |
|
121 | + * If $is is not bound, no errors will be raised. |
|
122 | + * If the stream currently has any buffered data it will be written to $is |
|
123 | + * before unbinding occurs. |
|
124 | + */ |
|
125 | + public function unbind(Swift_InputByteStream $is) |
|
126 | + { |
|
127 | + foreach ($this->mirrors as $k => $stream) { |
|
128 | + if ($is === $stream) { |
|
129 | + if ('' !== $this->writeBuffer) { |
|
130 | + $stream->write($this->writeBuffer); |
|
131 | + } |
|
132 | + unset($this->mirrors[$k]); |
|
133 | + } |
|
134 | + } |
|
135 | + } |
|
136 | + |
|
137 | + /** |
|
138 | + * Flush the contents of the stream (empty it) and set the internal pointer |
|
139 | + * to the beginning. |
|
140 | + * |
|
141 | + * @throws Swift_IoException |
|
142 | + */ |
|
143 | + public function flushBuffers() |
|
144 | + { |
|
145 | + if ('' !== $this->writeBuffer) { |
|
146 | + $this->doWrite($this->writeBuffer); |
|
147 | + } |
|
148 | + $this->flush(); |
|
149 | + |
|
150 | + foreach ($this->mirrors as $stream) { |
|
151 | + $stream->flushBuffers(); |
|
152 | + } |
|
153 | + } |
|
154 | + |
|
155 | + /** Run $bytes through all filters */ |
|
156 | + private function filter($bytes) |
|
157 | + { |
|
158 | + foreach ($this->filters as $filter) { |
|
159 | + $bytes = $filter->filter($bytes); |
|
160 | + } |
|
161 | + |
|
162 | + return $bytes; |
|
163 | + } |
|
164 | + |
|
165 | + /** Just write the bytes to the stream */ |
|
166 | + private function doWrite($bytes) |
|
167 | + { |
|
168 | + $this->doCommit($this->filter($bytes)); |
|
169 | + |
|
170 | + foreach ($this->mirrors as $stream) { |
|
171 | + $stream->write($bytes); |
|
172 | + } |
|
173 | + |
|
174 | + $this->writeBuffer = ''; |
|
175 | + } |
|
176 | 176 | } |
@@ -15,164 +15,164 @@ |
||
15 | 15 | */ |
16 | 16 | class Swift_ByteStream_ArrayByteStream implements Swift_InputByteStream, Swift_OutputByteStream |
17 | 17 | { |
18 | - /** |
|
19 | - * The internal stack of bytes. |
|
20 | - * |
|
21 | - * @var string[] |
|
22 | - */ |
|
23 | - private $array = []; |
|
24 | - |
|
25 | - /** |
|
26 | - * The size of the stack. |
|
27 | - * |
|
28 | - * @var int |
|
29 | - */ |
|
30 | - private $arraySize = 0; |
|
31 | - |
|
32 | - /** |
|
33 | - * The internal pointer offset. |
|
34 | - * |
|
35 | - * @var int |
|
36 | - */ |
|
37 | - private $offset = 0; |
|
38 | - |
|
39 | - /** |
|
40 | - * Bound streams. |
|
41 | - * |
|
42 | - * @var Swift_InputByteStream[] |
|
43 | - */ |
|
44 | - private $mirrors = []; |
|
45 | - |
|
46 | - /** |
|
47 | - * Create a new ArrayByteStream. |
|
48 | - * |
|
49 | - * If $stack is given the stream will be populated with the bytes it contains. |
|
50 | - * |
|
51 | - * @param mixed $stack of bytes in string or array form, optional |
|
52 | - */ |
|
53 | - public function __construct($stack = null) |
|
54 | - { |
|
55 | - if (\is_array($stack)) { |
|
56 | - $this->array = $stack; |
|
57 | - $this->arraySize = \count($stack); |
|
58 | - } elseif (\is_string($stack)) { |
|
59 | - $this->write($stack); |
|
60 | - } else { |
|
61 | - $this->array = []; |
|
62 | - } |
|
63 | - } |
|
64 | - |
|
65 | - /** |
|
66 | - * Reads $length bytes from the stream into a string and moves the pointer |
|
67 | - * through the stream by $length. |
|
68 | - * |
|
69 | - * If less bytes exist than are requested the |
|
70 | - * remaining bytes are given instead. If no bytes are remaining at all, boolean |
|
71 | - * false is returned. |
|
72 | - * |
|
73 | - * @param int $length |
|
74 | - * |
|
75 | - * @return string |
|
76 | - */ |
|
77 | - public function read($length) |
|
78 | - { |
|
79 | - if ($this->offset == $this->arraySize) { |
|
80 | - return false; |
|
81 | - } |
|
82 | - |
|
83 | - // Don't use array slice |
|
84 | - $end = $length + $this->offset; |
|
85 | - $end = $this->arraySize < $end ? $this->arraySize : $end; |
|
86 | - $ret = ''; |
|
87 | - for (; $this->offset < $end; ++$this->offset) { |
|
88 | - $ret .= $this->array[$this->offset]; |
|
89 | - } |
|
90 | - |
|
91 | - return $ret; |
|
92 | - } |
|
93 | - |
|
94 | - /** |
|
95 | - * Writes $bytes to the end of the stream. |
|
96 | - * |
|
97 | - * @param string $bytes |
|
98 | - */ |
|
99 | - public function write($bytes) |
|
100 | - { |
|
101 | - $to_add = str_split($bytes); |
|
102 | - foreach ($to_add as $value) { |
|
103 | - $this->array[] = $value; |
|
104 | - } |
|
105 | - $this->arraySize = \count($this->array); |
|
106 | - |
|
107 | - foreach ($this->mirrors as $stream) { |
|
108 | - $stream->write($bytes); |
|
109 | - } |
|
110 | - } |
|
111 | - |
|
112 | - /** |
|
113 | - * Not used. |
|
114 | - */ |
|
115 | - public function commit() |
|
116 | - { |
|
117 | - } |
|
118 | - |
|
119 | - /** |
|
120 | - * Attach $is to this stream. |
|
121 | - * |
|
122 | - * The stream acts as an observer, receiving all data that is written. |
|
123 | - * All {@link write()} and {@link flushBuffers()} operations will be mirrored. |
|
124 | - */ |
|
125 | - public function bind(Swift_InputByteStream $is) |
|
126 | - { |
|
127 | - $this->mirrors[] = $is; |
|
128 | - } |
|
129 | - |
|
130 | - /** |
|
131 | - * Remove an already bound stream. |
|
132 | - * |
|
133 | - * If $is is not bound, no errors will be raised. |
|
134 | - * If the stream currently has any buffered data it will be written to $is |
|
135 | - * before unbinding occurs. |
|
136 | - */ |
|
137 | - public function unbind(Swift_InputByteStream $is) |
|
138 | - { |
|
139 | - foreach ($this->mirrors as $k => $stream) { |
|
140 | - if ($is === $stream) { |
|
141 | - unset($this->mirrors[$k]); |
|
142 | - } |
|
143 | - } |
|
144 | - } |
|
145 | - |
|
146 | - /** |
|
147 | - * Move the internal read pointer to $byteOffset in the stream. |
|
148 | - * |
|
149 | - * @param int $byteOffset |
|
150 | - * |
|
151 | - * @return bool |
|
152 | - */ |
|
153 | - public function setReadPointer($byteOffset) |
|
154 | - { |
|
155 | - if ($byteOffset > $this->arraySize) { |
|
156 | - $byteOffset = $this->arraySize; |
|
157 | - } elseif ($byteOffset < 0) { |
|
158 | - $byteOffset = 0; |
|
159 | - } |
|
160 | - |
|
161 | - $this->offset = $byteOffset; |
|
162 | - } |
|
163 | - |
|
164 | - /** |
|
165 | - * Flush the contents of the stream (empty it) and set the internal pointer |
|
166 | - * to the beginning. |
|
167 | - */ |
|
168 | - public function flushBuffers() |
|
169 | - { |
|
170 | - $this->offset = 0; |
|
171 | - $this->array = []; |
|
172 | - $this->arraySize = 0; |
|
173 | - |
|
174 | - foreach ($this->mirrors as $stream) { |
|
175 | - $stream->flushBuffers(); |
|
176 | - } |
|
177 | - } |
|
18 | + /** |
|
19 | + * The internal stack of bytes. |
|
20 | + * |
|
21 | + * @var string[] |
|
22 | + */ |
|
23 | + private $array = []; |
|
24 | + |
|
25 | + /** |
|
26 | + * The size of the stack. |
|
27 | + * |
|
28 | + * @var int |
|
29 | + */ |
|
30 | + private $arraySize = 0; |
|
31 | + |
|
32 | + /** |
|
33 | + * The internal pointer offset. |
|
34 | + * |
|
35 | + * @var int |
|
36 | + */ |
|
37 | + private $offset = 0; |
|
38 | + |
|
39 | + /** |
|
40 | + * Bound streams. |
|
41 | + * |
|
42 | + * @var Swift_InputByteStream[] |
|
43 | + */ |
|
44 | + private $mirrors = []; |
|
45 | + |
|
46 | + /** |
|
47 | + * Create a new ArrayByteStream. |
|
48 | + * |
|
49 | + * If $stack is given the stream will be populated with the bytes it contains. |
|
50 | + * |
|
51 | + * @param mixed $stack of bytes in string or array form, optional |
|
52 | + */ |
|
53 | + public function __construct($stack = null) |
|
54 | + { |
|
55 | + if (\is_array($stack)) { |
|
56 | + $this->array = $stack; |
|
57 | + $this->arraySize = \count($stack); |
|
58 | + } elseif (\is_string($stack)) { |
|
59 | + $this->write($stack); |
|
60 | + } else { |
|
61 | + $this->array = []; |
|
62 | + } |
|
63 | + } |
|
64 | + |
|
65 | + /** |
|
66 | + * Reads $length bytes from the stream into a string and moves the pointer |
|
67 | + * through the stream by $length. |
|
68 | + * |
|
69 | + * If less bytes exist than are requested the |
|
70 | + * remaining bytes are given instead. If no bytes are remaining at all, boolean |
|
71 | + * false is returned. |
|
72 | + * |
|
73 | + * @param int $length |
|
74 | + * |
|
75 | + * @return string |
|
76 | + */ |
|
77 | + public function read($length) |
|
78 | + { |
|
79 | + if ($this->offset == $this->arraySize) { |
|
80 | + return false; |
|
81 | + } |
|
82 | + |
|
83 | + // Don't use array slice |
|
84 | + $end = $length + $this->offset; |
|
85 | + $end = $this->arraySize < $end ? $this->arraySize : $end; |
|
86 | + $ret = ''; |
|
87 | + for (; $this->offset < $end; ++$this->offset) { |
|
88 | + $ret .= $this->array[$this->offset]; |
|
89 | + } |
|
90 | + |
|
91 | + return $ret; |
|
92 | + } |
|
93 | + |
|
94 | + /** |
|
95 | + * Writes $bytes to the end of the stream. |
|
96 | + * |
|
97 | + * @param string $bytes |
|
98 | + */ |
|
99 | + public function write($bytes) |
|
100 | + { |
|
101 | + $to_add = str_split($bytes); |
|
102 | + foreach ($to_add as $value) { |
|
103 | + $this->array[] = $value; |
|
104 | + } |
|
105 | + $this->arraySize = \count($this->array); |
|
106 | + |
|
107 | + foreach ($this->mirrors as $stream) { |
|
108 | + $stream->write($bytes); |
|
109 | + } |
|
110 | + } |
|
111 | + |
|
112 | + /** |
|
113 | + * Not used. |
|
114 | + */ |
|
115 | + public function commit() |
|
116 | + { |
|
117 | + } |
|
118 | + |
|
119 | + /** |
|
120 | + * Attach $is to this stream. |
|
121 | + * |
|
122 | + * The stream acts as an observer, receiving all data that is written. |
|
123 | + * All {@link write()} and {@link flushBuffers()} operations will be mirrored. |
|
124 | + */ |
|
125 | + public function bind(Swift_InputByteStream $is) |
|
126 | + { |
|
127 | + $this->mirrors[] = $is; |
|
128 | + } |
|
129 | + |
|
130 | + /** |
|
131 | + * Remove an already bound stream. |
|
132 | + * |
|
133 | + * If $is is not bound, no errors will be raised. |
|
134 | + * If the stream currently has any buffered data it will be written to $is |
|
135 | + * before unbinding occurs. |
|
136 | + */ |
|
137 | + public function unbind(Swift_InputByteStream $is) |
|
138 | + { |
|
139 | + foreach ($this->mirrors as $k => $stream) { |
|
140 | + if ($is === $stream) { |
|
141 | + unset($this->mirrors[$k]); |
|
142 | + } |
|
143 | + } |
|
144 | + } |
|
145 | + |
|
146 | + /** |
|
147 | + * Move the internal read pointer to $byteOffset in the stream. |
|
148 | + * |
|
149 | + * @param int $byteOffset |
|
150 | + * |
|
151 | + * @return bool |
|
152 | + */ |
|
153 | + public function setReadPointer($byteOffset) |
|
154 | + { |
|
155 | + if ($byteOffset > $this->arraySize) { |
|
156 | + $byteOffset = $this->arraySize; |
|
157 | + } elseif ($byteOffset < 0) { |
|
158 | + $byteOffset = 0; |
|
159 | + } |
|
160 | + |
|
161 | + $this->offset = $byteOffset; |
|
162 | + } |
|
163 | + |
|
164 | + /** |
|
165 | + * Flush the contents of the stream (empty it) and set the internal pointer |
|
166 | + * to the beginning. |
|
167 | + */ |
|
168 | + public function flushBuffers() |
|
169 | + { |
|
170 | + $this->offset = 0; |
|
171 | + $this->array = []; |
|
172 | + $this->arraySize = 0; |
|
173 | + |
|
174 | + foreach ($this->mirrors as $stream) { |
|
175 | + $stream->flushBuffers(); |
|
176 | + } |
|
177 | + } |
|
178 | 178 | } |
@@ -13,40 +13,40 @@ |
||
13 | 13 | */ |
14 | 14 | class Swift_ByteStream_TemporaryFileByteStream extends Swift_ByteStream_FileByteStream |
15 | 15 | { |
16 | - public function __construct() |
|
17 | - { |
|
18 | - $filePath = tempnam(sys_get_temp_dir(), 'FileByteStream'); |
|
19 | - |
|
20 | - if (false === $filePath) { |
|
21 | - throw new Swift_IoException('Failed to retrieve temporary file name.'); |
|
22 | - } |
|
23 | - |
|
24 | - parent::__construct($filePath, true); |
|
25 | - } |
|
26 | - |
|
27 | - public function getContent() |
|
28 | - { |
|
29 | - if (false === ($content = file_get_contents($this->getPath()))) { |
|
30 | - throw new Swift_IoException('Failed to get temporary file content.'); |
|
31 | - } |
|
32 | - |
|
33 | - return $content; |
|
34 | - } |
|
35 | - |
|
36 | - public function __destruct() |
|
37 | - { |
|
38 | - if (file_exists($this->getPath())) { |
|
39 | - @unlink($this->getPath()); |
|
40 | - } |
|
41 | - } |
|
42 | - |
|
43 | - public function __sleep() |
|
44 | - { |
|
45 | - throw new \BadMethodCallException('Cannot serialize '.__CLASS__); |
|
46 | - } |
|
47 | - |
|
48 | - public function __wakeup() |
|
49 | - { |
|
50 | - throw new \BadMethodCallException('Cannot unserialize '.__CLASS__); |
|
51 | - } |
|
16 | + public function __construct() |
|
17 | + { |
|
18 | + $filePath = tempnam(sys_get_temp_dir(), 'FileByteStream'); |
|
19 | + |
|
20 | + if (false === $filePath) { |
|
21 | + throw new Swift_IoException('Failed to retrieve temporary file name.'); |
|
22 | + } |
|
23 | + |
|
24 | + parent::__construct($filePath, true); |
|
25 | + } |
|
26 | + |
|
27 | + public function getContent() |
|
28 | + { |
|
29 | + if (false === ($content = file_get_contents($this->getPath()))) { |
|
30 | + throw new Swift_IoException('Failed to get temporary file content.'); |
|
31 | + } |
|
32 | + |
|
33 | + return $content; |
|
34 | + } |
|
35 | + |
|
36 | + public function __destruct() |
|
37 | + { |
|
38 | + if (file_exists($this->getPath())) { |
|
39 | + @unlink($this->getPath()); |
|
40 | + } |
|
41 | + } |
|
42 | + |
|
43 | + public function __sleep() |
|
44 | + { |
|
45 | + throw new \BadMethodCallException('Cannot serialize '.__CLASS__); |
|
46 | + } |
|
47 | + |
|
48 | + public function __wakeup() |
|
49 | + { |
|
50 | + throw new \BadMethodCallException('Cannot unserialize '.__CLASS__); |
|
51 | + } |
|
52 | 52 | } |
@@ -15,200 +15,200 @@ |
||
15 | 15 | */ |
16 | 16 | class Swift_ByteStream_FileByteStream extends Swift_ByteStream_AbstractFilterableInputStream implements Swift_FileStream |
17 | 17 | { |
18 | - /** The internal pointer offset */ |
|
19 | - private $offset = 0; |
|
20 | - |
|
21 | - /** The path to the file */ |
|
22 | - private $path; |
|
23 | - |
|
24 | - /** The mode this file is opened in for writing */ |
|
25 | - private $mode; |
|
26 | - |
|
27 | - /** A lazy-loaded resource handle for reading the file */ |
|
28 | - private $reader; |
|
29 | - |
|
30 | - /** A lazy-loaded resource handle for writing the file */ |
|
31 | - private $writer; |
|
32 | - |
|
33 | - /** If stream is seekable true/false, or null if not known */ |
|
34 | - private $seekable = null; |
|
35 | - |
|
36 | - /** |
|
37 | - * Create a new FileByteStream for $path. |
|
38 | - * |
|
39 | - * @param string $path |
|
40 | - * @param bool $writable if true |
|
41 | - */ |
|
42 | - public function __construct($path, $writable = false) |
|
43 | - { |
|
44 | - if (empty($path)) { |
|
45 | - throw new Swift_IoException('The path cannot be empty'); |
|
46 | - } |
|
47 | - $this->path = $path; |
|
48 | - $this->mode = $writable ? 'w+b' : 'rb'; |
|
49 | - } |
|
50 | - |
|
51 | - /** |
|
52 | - * Get the complete path to the file. |
|
53 | - * |
|
54 | - * @return string |
|
55 | - */ |
|
56 | - public function getPath() |
|
57 | - { |
|
58 | - return $this->path; |
|
59 | - } |
|
60 | - |
|
61 | - /** |
|
62 | - * Reads $length bytes from the stream into a string and moves the pointer |
|
63 | - * through the stream by $length. |
|
64 | - * |
|
65 | - * If less bytes exist than are requested the |
|
66 | - * remaining bytes are given instead. If no bytes are remaining at all, boolean |
|
67 | - * false is returned. |
|
68 | - * |
|
69 | - * @param int $length |
|
70 | - * |
|
71 | - * @return string|bool |
|
72 | - * |
|
73 | - * @throws Swift_IoException |
|
74 | - */ |
|
75 | - public function read($length) |
|
76 | - { |
|
77 | - $fp = $this->getReadHandle(); |
|
78 | - if (!feof($fp)) { |
|
79 | - $bytes = fread($fp, $length); |
|
80 | - $this->offset = ftell($fp); |
|
81 | - |
|
82 | - // If we read one byte after reaching the end of the file |
|
83 | - // feof() will return false and an empty string is returned |
|
84 | - if ((false === $bytes || '' === $bytes) && feof($fp)) { |
|
85 | - $this->resetReadHandle(); |
|
86 | - |
|
87 | - return false; |
|
88 | - } |
|
89 | - |
|
90 | - return $bytes; |
|
91 | - } |
|
92 | - |
|
93 | - $this->resetReadHandle(); |
|
94 | - |
|
95 | - return false; |
|
96 | - } |
|
97 | - |
|
98 | - /** |
|
99 | - * Move the internal read pointer to $byteOffset in the stream. |
|
100 | - * |
|
101 | - * @param int $byteOffset |
|
102 | - * |
|
103 | - * @return bool |
|
104 | - */ |
|
105 | - public function setReadPointer($byteOffset) |
|
106 | - { |
|
107 | - if (isset($this->reader)) { |
|
108 | - $this->seekReadStreamToPosition($byteOffset); |
|
109 | - } |
|
110 | - $this->offset = $byteOffset; |
|
111 | - } |
|
112 | - |
|
113 | - /** Just write the bytes to the file */ |
|
114 | - protected function doCommit($bytes) |
|
115 | - { |
|
116 | - fwrite($this->getWriteHandle(), $bytes); |
|
117 | - $this->resetReadHandle(); |
|
118 | - } |
|
119 | - |
|
120 | - /** Not used */ |
|
121 | - protected function flush() |
|
122 | - { |
|
123 | - } |
|
124 | - |
|
125 | - /** Get the resource for reading */ |
|
126 | - private function getReadHandle() |
|
127 | - { |
|
128 | - if (!isset($this->reader)) { |
|
129 | - $pointer = @fopen($this->path, 'rb'); |
|
130 | - if (!$pointer) { |
|
131 | - throw new Swift_IoException('Unable to open file for reading ['.$this->path.']'); |
|
132 | - } |
|
133 | - $this->reader = $pointer; |
|
134 | - if (0 != $this->offset) { |
|
135 | - $this->getReadStreamSeekableStatus(); |
|
136 | - $this->seekReadStreamToPosition($this->offset); |
|
137 | - } |
|
138 | - } |
|
139 | - |
|
140 | - return $this->reader; |
|
141 | - } |
|
142 | - |
|
143 | - /** Get the resource for writing */ |
|
144 | - private function getWriteHandle() |
|
145 | - { |
|
146 | - if (!isset($this->writer)) { |
|
147 | - if (!$this->writer = fopen($this->path, $this->mode)) { |
|
148 | - throw new Swift_IoException('Unable to open file for writing ['.$this->path.']'); |
|
149 | - } |
|
150 | - } |
|
151 | - |
|
152 | - return $this->writer; |
|
153 | - } |
|
154 | - |
|
155 | - /** Force a reload of the resource for reading */ |
|
156 | - private function resetReadHandle() |
|
157 | - { |
|
158 | - if (isset($this->reader)) { |
|
159 | - fclose($this->reader); |
|
160 | - $this->reader = null; |
|
161 | - } |
|
162 | - } |
|
163 | - |
|
164 | - /** Check if ReadOnly Stream is seekable */ |
|
165 | - private function getReadStreamSeekableStatus() |
|
166 | - { |
|
167 | - $metas = stream_get_meta_data($this->reader); |
|
168 | - $this->seekable = $metas['seekable']; |
|
169 | - } |
|
170 | - |
|
171 | - /** Streams in a readOnly stream ensuring copy if needed */ |
|
172 | - private function seekReadStreamToPosition($offset) |
|
173 | - { |
|
174 | - if (null === $this->seekable) { |
|
175 | - $this->getReadStreamSeekableStatus(); |
|
176 | - } |
|
177 | - if (false === $this->seekable) { |
|
178 | - $currentPos = ftell($this->reader); |
|
179 | - if ($currentPos < $offset) { |
|
180 | - $toDiscard = $offset - $currentPos; |
|
181 | - fread($this->reader, $toDiscard); |
|
182 | - |
|
183 | - return; |
|
184 | - } |
|
185 | - $this->copyReadStream(); |
|
186 | - } |
|
187 | - fseek($this->reader, $offset, SEEK_SET); |
|
188 | - } |
|
189 | - |
|
190 | - /** Copy a readOnly Stream to ensure seekability */ |
|
191 | - private function copyReadStream() |
|
192 | - { |
|
193 | - if ($tmpFile = fopen('php://temp/maxmemory:4096', 'w+b')) { |
|
194 | - /* We have opened a php:// Stream Should work without problem */ |
|
195 | - } elseif (\function_exists('sys_get_temp_dir') && is_writable(sys_get_temp_dir()) && ($tmpFile = tmpfile())) { |
|
196 | - /* We have opened a tmpfile */ |
|
197 | - } else { |
|
198 | - throw new Swift_IoException('Unable to copy the file to make it seekable, sys_temp_dir is not writable, php://memory not available'); |
|
199 | - } |
|
200 | - $currentPos = ftell($this->reader); |
|
201 | - fclose($this->reader); |
|
202 | - $source = fopen($this->path, 'rb'); |
|
203 | - if (!$source) { |
|
204 | - throw new Swift_IoException('Unable to open file for copying ['.$this->path.']'); |
|
205 | - } |
|
206 | - fseek($tmpFile, 0, SEEK_SET); |
|
207 | - while (!feof($source)) { |
|
208 | - fwrite($tmpFile, fread($source, 4096)); |
|
209 | - } |
|
210 | - fseek($tmpFile, $currentPos, SEEK_SET); |
|
211 | - fclose($source); |
|
212 | - $this->reader = $tmpFile; |
|
213 | - } |
|
18 | + /** The internal pointer offset */ |
|
19 | + private $offset = 0; |
|
20 | + |
|
21 | + /** The path to the file */ |
|
22 | + private $path; |
|
23 | + |
|
24 | + /** The mode this file is opened in for writing */ |
|
25 | + private $mode; |
|
26 | + |
|
27 | + /** A lazy-loaded resource handle for reading the file */ |
|
28 | + private $reader; |
|
29 | + |
|
30 | + /** A lazy-loaded resource handle for writing the file */ |
|
31 | + private $writer; |
|
32 | + |
|
33 | + /** If stream is seekable true/false, or null if not known */ |
|
34 | + private $seekable = null; |
|
35 | + |
|
36 | + /** |
|
37 | + * Create a new FileByteStream for $path. |
|
38 | + * |
|
39 | + * @param string $path |
|
40 | + * @param bool $writable if true |
|
41 | + */ |
|
42 | + public function __construct($path, $writable = false) |
|
43 | + { |
|
44 | + if (empty($path)) { |
|
45 | + throw new Swift_IoException('The path cannot be empty'); |
|
46 | + } |
|
47 | + $this->path = $path; |
|
48 | + $this->mode = $writable ? 'w+b' : 'rb'; |
|
49 | + } |
|
50 | + |
|
51 | + /** |
|
52 | + * Get the complete path to the file. |
|
53 | + * |
|
54 | + * @return string |
|
55 | + */ |
|
56 | + public function getPath() |
|
57 | + { |
|
58 | + return $this->path; |
|
59 | + } |
|
60 | + |
|
61 | + /** |
|
62 | + * Reads $length bytes from the stream into a string and moves the pointer |
|
63 | + * through the stream by $length. |
|
64 | + * |
|
65 | + * If less bytes exist than are requested the |
|
66 | + * remaining bytes are given instead. If no bytes are remaining at all, boolean |
|
67 | + * false is returned. |
|
68 | + * |
|
69 | + * @param int $length |
|
70 | + * |
|
71 | + * @return string|bool |
|
72 | + * |
|
73 | + * @throws Swift_IoException |
|
74 | + */ |
|
75 | + public function read($length) |
|
76 | + { |
|
77 | + $fp = $this->getReadHandle(); |
|
78 | + if (!feof($fp)) { |
|
79 | + $bytes = fread($fp, $length); |
|
80 | + $this->offset = ftell($fp); |
|
81 | + |
|
82 | + // If we read one byte after reaching the end of the file |
|
83 | + // feof() will return false and an empty string is returned |
|
84 | + if ((false === $bytes || '' === $bytes) && feof($fp)) { |
|
85 | + $this->resetReadHandle(); |
|
86 | + |
|
87 | + return false; |
|
88 | + } |
|
89 | + |
|
90 | + return $bytes; |
|
91 | + } |
|
92 | + |
|
93 | + $this->resetReadHandle(); |
|
94 | + |
|
95 | + return false; |
|
96 | + } |
|
97 | + |
|
98 | + /** |
|
99 | + * Move the internal read pointer to $byteOffset in the stream. |
|
100 | + * |
|
101 | + * @param int $byteOffset |
|
102 | + * |
|
103 | + * @return bool |
|
104 | + */ |
|
105 | + public function setReadPointer($byteOffset) |
|
106 | + { |
|
107 | + if (isset($this->reader)) { |
|
108 | + $this->seekReadStreamToPosition($byteOffset); |
|
109 | + } |
|
110 | + $this->offset = $byteOffset; |
|
111 | + } |
|
112 | + |
|
113 | + /** Just write the bytes to the file */ |
|
114 | + protected function doCommit($bytes) |
|
115 | + { |
|
116 | + fwrite($this->getWriteHandle(), $bytes); |
|
117 | + $this->resetReadHandle(); |
|
118 | + } |
|
119 | + |
|
120 | + /** Not used */ |
|
121 | + protected function flush() |
|
122 | + { |
|
123 | + } |
|
124 | + |
|
125 | + /** Get the resource for reading */ |
|
126 | + private function getReadHandle() |
|
127 | + { |
|
128 | + if (!isset($this->reader)) { |
|
129 | + $pointer = @fopen($this->path, 'rb'); |
|
130 | + if (!$pointer) { |
|
131 | + throw new Swift_IoException('Unable to open file for reading ['.$this->path.']'); |
|
132 | + } |
|
133 | + $this->reader = $pointer; |
|
134 | + if (0 != $this->offset) { |
|
135 | + $this->getReadStreamSeekableStatus(); |
|
136 | + $this->seekReadStreamToPosition($this->offset); |
|
137 | + } |
|
138 | + } |
|
139 | + |
|
140 | + return $this->reader; |
|
141 | + } |
|
142 | + |
|
143 | + /** Get the resource for writing */ |
|
144 | + private function getWriteHandle() |
|
145 | + { |
|
146 | + if (!isset($this->writer)) { |
|
147 | + if (!$this->writer = fopen($this->path, $this->mode)) { |
|
148 | + throw new Swift_IoException('Unable to open file for writing ['.$this->path.']'); |
|
149 | + } |
|
150 | + } |
|
151 | + |
|
152 | + return $this->writer; |
|
153 | + } |
|
154 | + |
|
155 | + /** Force a reload of the resource for reading */ |
|
156 | + private function resetReadHandle() |
|
157 | + { |
|
158 | + if (isset($this->reader)) { |
|
159 | + fclose($this->reader); |
|
160 | + $this->reader = null; |
|
161 | + } |
|
162 | + } |
|
163 | + |
|
164 | + /** Check if ReadOnly Stream is seekable */ |
|
165 | + private function getReadStreamSeekableStatus() |
|
166 | + { |
|
167 | + $metas = stream_get_meta_data($this->reader); |
|
168 | + $this->seekable = $metas['seekable']; |
|
169 | + } |
|
170 | + |
|
171 | + /** Streams in a readOnly stream ensuring copy if needed */ |
|
172 | + private function seekReadStreamToPosition($offset) |
|
173 | + { |
|
174 | + if (null === $this->seekable) { |
|
175 | + $this->getReadStreamSeekableStatus(); |
|
176 | + } |
|
177 | + if (false === $this->seekable) { |
|
178 | + $currentPos = ftell($this->reader); |
|
179 | + if ($currentPos < $offset) { |
|
180 | + $toDiscard = $offset - $currentPos; |
|
181 | + fread($this->reader, $toDiscard); |
|
182 | + |
|
183 | + return; |
|
184 | + } |
|
185 | + $this->copyReadStream(); |
|
186 | + } |
|
187 | + fseek($this->reader, $offset, SEEK_SET); |
|
188 | + } |
|
189 | + |
|
190 | + /** Copy a readOnly Stream to ensure seekability */ |
|
191 | + private function copyReadStream() |
|
192 | + { |
|
193 | + if ($tmpFile = fopen('php://temp/maxmemory:4096', 'w+b')) { |
|
194 | + /* We have opened a php:// Stream Should work without problem */ |
|
195 | + } elseif (\function_exists('sys_get_temp_dir') && is_writable(sys_get_temp_dir()) && ($tmpFile = tmpfile())) { |
|
196 | + /* We have opened a tmpfile */ |
|
197 | + } else { |
|
198 | + throw new Swift_IoException('Unable to copy the file to make it seekable, sys_temp_dir is not writable, php://memory not available'); |
|
199 | + } |
|
200 | + $currentPos = ftell($this->reader); |
|
201 | + fclose($this->reader); |
|
202 | + $source = fopen($this->path, 'rb'); |
|
203 | + if (!$source) { |
|
204 | + throw new Swift_IoException('Unable to open file for copying ['.$this->path.']'); |
|
205 | + } |
|
206 | + fseek($tmpFile, 0, SEEK_SET); |
|
207 | + while (!feof($source)) { |
|
208 | + fwrite($tmpFile, fread($source, 4096)); |
|
209 | + } |
|
210 | + fseek($tmpFile, $currentPos, SEEK_SET); |
|
211 | + fclose($source); |
|
212 | + $this->reader = $tmpFile; |
|
213 | + } |
|
214 | 214 | } |
@@ -15,56 +15,56 @@ |
||
15 | 15 | */ |
16 | 16 | class Swift_StreamFilters_StringReplacementFilter implements Swift_StreamFilter |
17 | 17 | { |
18 | - /** The needle(s) to search for */ |
|
19 | - private $search; |
|
18 | + /** The needle(s) to search for */ |
|
19 | + private $search; |
|
20 | 20 | |
21 | - /** The replacement(s) to make */ |
|
22 | - private $replace; |
|
21 | + /** The replacement(s) to make */ |
|
22 | + private $replace; |
|
23 | 23 | |
24 | - /** |
|
25 | - * Create a new StringReplacementFilter with $search and $replace. |
|
26 | - * |
|
27 | - * @param string|array $search |
|
28 | - * @param string|array $replace |
|
29 | - */ |
|
30 | - public function __construct($search, $replace) |
|
31 | - { |
|
32 | - $this->search = $search; |
|
33 | - $this->replace = $replace; |
|
34 | - } |
|
24 | + /** |
|
25 | + * Create a new StringReplacementFilter with $search and $replace. |
|
26 | + * |
|
27 | + * @param string|array $search |
|
28 | + * @param string|array $replace |
|
29 | + */ |
|
30 | + public function __construct($search, $replace) |
|
31 | + { |
|
32 | + $this->search = $search; |
|
33 | + $this->replace = $replace; |
|
34 | + } |
|
35 | 35 | |
36 | - /** |
|
37 | - * Returns true if based on the buffer passed more bytes should be buffered. |
|
38 | - * |
|
39 | - * @param string $buffer |
|
40 | - * |
|
41 | - * @return bool |
|
42 | - */ |
|
43 | - public function shouldBuffer($buffer) |
|
44 | - { |
|
45 | - if ('' === $buffer) { |
|
46 | - return false; |
|
47 | - } |
|
36 | + /** |
|
37 | + * Returns true if based on the buffer passed more bytes should be buffered. |
|
38 | + * |
|
39 | + * @param string $buffer |
|
40 | + * |
|
41 | + * @return bool |
|
42 | + */ |
|
43 | + public function shouldBuffer($buffer) |
|
44 | + { |
|
45 | + if ('' === $buffer) { |
|
46 | + return false; |
|
47 | + } |
|
48 | 48 | |
49 | - $endOfBuffer = substr($buffer, -1); |
|
50 | - foreach ((array) $this->search as $needle) { |
|
51 | - if (false !== strpos($needle, $endOfBuffer)) { |
|
52 | - return true; |
|
53 | - } |
|
54 | - } |
|
49 | + $endOfBuffer = substr($buffer, -1); |
|
50 | + foreach ((array) $this->search as $needle) { |
|
51 | + if (false !== strpos($needle, $endOfBuffer)) { |
|
52 | + return true; |
|
53 | + } |
|
54 | + } |
|
55 | 55 | |
56 | - return false; |
|
57 | - } |
|
56 | + return false; |
|
57 | + } |
|
58 | 58 | |
59 | - /** |
|
60 | - * Perform the actual replacements on $buffer and return the result. |
|
61 | - * |
|
62 | - * @param string $buffer |
|
63 | - * |
|
64 | - * @return string |
|
65 | - */ |
|
66 | - public function filter($buffer) |
|
67 | - { |
|
68 | - return str_replace($this->search, $this->replace, $buffer); |
|
69 | - } |
|
59 | + /** |
|
60 | + * Perform the actual replacements on $buffer and return the result. |
|
61 | + * |
|
62 | + * @param string $buffer |
|
63 | + * |
|
64 | + * @return string |
|
65 | + */ |
|
66 | + public function filter($buffer) |
|
67 | + { |
|
68 | + return str_replace($this->search, $this->replace, $buffer); |
|
69 | + } |
|
70 | 70 | } |
@@ -17,150 +17,150 @@ |
||
17 | 17 | */ |
18 | 18 | class Swift_StreamFilters_ByteArrayReplacementFilter implements Swift_StreamFilter |
19 | 19 | { |
20 | - /** The replacement(s) to make */ |
|
21 | - private $replace; |
|
20 | + /** The replacement(s) to make */ |
|
21 | + private $replace; |
|
22 | 22 | |
23 | - /** The Index for searching */ |
|
24 | - private $index; |
|
23 | + /** The Index for searching */ |
|
24 | + private $index; |
|
25 | 25 | |
26 | - /** The Search Tree */ |
|
27 | - private $tree = []; |
|
26 | + /** The Search Tree */ |
|
27 | + private $tree = []; |
|
28 | 28 | |
29 | - /** Gives the size of the largest search */ |
|
30 | - private $treeMaxLen = 0; |
|
29 | + /** Gives the size of the largest search */ |
|
30 | + private $treeMaxLen = 0; |
|
31 | 31 | |
32 | - private $repSize; |
|
32 | + private $repSize; |
|
33 | 33 | |
34 | - /** |
|
35 | - * Create a new ByteArrayReplacementFilter with $search and $replace. |
|
36 | - * |
|
37 | - * @param array $search |
|
38 | - * @param array $replace |
|
39 | - */ |
|
40 | - public function __construct($search, $replace) |
|
41 | - { |
|
42 | - $this->index = []; |
|
43 | - $this->tree = []; |
|
44 | - $this->replace = []; |
|
45 | - $this->repSize = []; |
|
34 | + /** |
|
35 | + * Create a new ByteArrayReplacementFilter with $search and $replace. |
|
36 | + * |
|
37 | + * @param array $search |
|
38 | + * @param array $replace |
|
39 | + */ |
|
40 | + public function __construct($search, $replace) |
|
41 | + { |
|
42 | + $this->index = []; |
|
43 | + $this->tree = []; |
|
44 | + $this->replace = []; |
|
45 | + $this->repSize = []; |
|
46 | 46 | |
47 | - $tree = null; |
|
48 | - $i = null; |
|
49 | - $last_size = $size = 0; |
|
50 | - foreach ($search as $i => $search_element) { |
|
51 | - if (null !== $tree) { |
|
52 | - $tree[-1] = min(\count($replace) - 1, $i - 1); |
|
53 | - $tree[-2] = $last_size; |
|
54 | - } |
|
55 | - $tree = &$this->tree; |
|
56 | - if (\is_array($search_element)) { |
|
57 | - foreach ($search_element as $k => $char) { |
|
58 | - $this->index[$char] = true; |
|
59 | - if (!isset($tree[$char])) { |
|
60 | - $tree[$char] = []; |
|
61 | - } |
|
62 | - $tree = &$tree[$char]; |
|
63 | - } |
|
64 | - $last_size = $k + 1; |
|
65 | - $size = max($size, $last_size); |
|
66 | - } else { |
|
67 | - $last_size = 1; |
|
68 | - if (!isset($tree[$search_element])) { |
|
69 | - $tree[$search_element] = []; |
|
70 | - } |
|
71 | - $tree = &$tree[$search_element]; |
|
72 | - $size = max($last_size, $size); |
|
73 | - $this->index[$search_element] = true; |
|
74 | - } |
|
75 | - } |
|
76 | - if (null !== $i) { |
|
77 | - $tree[-1] = min(\count($replace) - 1, $i); |
|
78 | - $tree[-2] = $last_size; |
|
79 | - $this->treeMaxLen = $size; |
|
80 | - } |
|
81 | - foreach ($replace as $rep) { |
|
82 | - if (!\is_array($rep)) { |
|
83 | - $rep = [$rep]; |
|
84 | - } |
|
85 | - $this->replace[] = $rep; |
|
86 | - } |
|
87 | - for ($i = \count($this->replace) - 1; $i >= 0; --$i) { |
|
88 | - $this->replace[$i] = $rep = $this->filter($this->replace[$i], $i); |
|
89 | - $this->repSize[$i] = \count($rep); |
|
90 | - } |
|
91 | - } |
|
47 | + $tree = null; |
|
48 | + $i = null; |
|
49 | + $last_size = $size = 0; |
|
50 | + foreach ($search as $i => $search_element) { |
|
51 | + if (null !== $tree) { |
|
52 | + $tree[-1] = min(\count($replace) - 1, $i - 1); |
|
53 | + $tree[-2] = $last_size; |
|
54 | + } |
|
55 | + $tree = &$this->tree; |
|
56 | + if (\is_array($search_element)) { |
|
57 | + foreach ($search_element as $k => $char) { |
|
58 | + $this->index[$char] = true; |
|
59 | + if (!isset($tree[$char])) { |
|
60 | + $tree[$char] = []; |
|
61 | + } |
|
62 | + $tree = &$tree[$char]; |
|
63 | + } |
|
64 | + $last_size = $k + 1; |
|
65 | + $size = max($size, $last_size); |
|
66 | + } else { |
|
67 | + $last_size = 1; |
|
68 | + if (!isset($tree[$search_element])) { |
|
69 | + $tree[$search_element] = []; |
|
70 | + } |
|
71 | + $tree = &$tree[$search_element]; |
|
72 | + $size = max($last_size, $size); |
|
73 | + $this->index[$search_element] = true; |
|
74 | + } |
|
75 | + } |
|
76 | + if (null !== $i) { |
|
77 | + $tree[-1] = min(\count($replace) - 1, $i); |
|
78 | + $tree[-2] = $last_size; |
|
79 | + $this->treeMaxLen = $size; |
|
80 | + } |
|
81 | + foreach ($replace as $rep) { |
|
82 | + if (!\is_array($rep)) { |
|
83 | + $rep = [$rep]; |
|
84 | + } |
|
85 | + $this->replace[] = $rep; |
|
86 | + } |
|
87 | + for ($i = \count($this->replace) - 1; $i >= 0; --$i) { |
|
88 | + $this->replace[$i] = $rep = $this->filter($this->replace[$i], $i); |
|
89 | + $this->repSize[$i] = \count($rep); |
|
90 | + } |
|
91 | + } |
|
92 | 92 | |
93 | - /** |
|
94 | - * Returns true if based on the buffer passed more bytes should be buffered. |
|
95 | - * |
|
96 | - * @param array $buffer |
|
97 | - * |
|
98 | - * @return bool |
|
99 | - */ |
|
100 | - public function shouldBuffer($buffer) |
|
101 | - { |
|
102 | - $endOfBuffer = end($buffer); |
|
93 | + /** |
|
94 | + * Returns true if based on the buffer passed more bytes should be buffered. |
|
95 | + * |
|
96 | + * @param array $buffer |
|
97 | + * |
|
98 | + * @return bool |
|
99 | + */ |
|
100 | + public function shouldBuffer($buffer) |
|
101 | + { |
|
102 | + $endOfBuffer = end($buffer); |
|
103 | 103 | |
104 | - return isset($this->index[$endOfBuffer]); |
|
105 | - } |
|
104 | + return isset($this->index[$endOfBuffer]); |
|
105 | + } |
|
106 | 106 | |
107 | - /** |
|
108 | - * Perform the actual replacements on $buffer and return the result. |
|
109 | - * |
|
110 | - * @param array $buffer |
|
111 | - * @param int $minReplaces |
|
112 | - * |
|
113 | - * @return array |
|
114 | - */ |
|
115 | - public function filter($buffer, $minReplaces = -1) |
|
116 | - { |
|
117 | - if (0 == $this->treeMaxLen) { |
|
118 | - return $buffer; |
|
119 | - } |
|
107 | + /** |
|
108 | + * Perform the actual replacements on $buffer and return the result. |
|
109 | + * |
|
110 | + * @param array $buffer |
|
111 | + * @param int $minReplaces |
|
112 | + * |
|
113 | + * @return array |
|
114 | + */ |
|
115 | + public function filter($buffer, $minReplaces = -1) |
|
116 | + { |
|
117 | + if (0 == $this->treeMaxLen) { |
|
118 | + return $buffer; |
|
119 | + } |
|
120 | 120 | |
121 | - $newBuffer = []; |
|
122 | - $buf_size = \count($buffer); |
|
123 | - $last_size = 0; |
|
124 | - for ($i = 0; $i < $buf_size; ++$i) { |
|
125 | - $search_pos = $this->tree; |
|
126 | - $last_found = PHP_INT_MAX; |
|
127 | - // We try to find if the next byte is part of a search pattern |
|
128 | - for ($j = 0; $j <= $this->treeMaxLen; ++$j) { |
|
129 | - // We have a new byte for a search pattern |
|
130 | - if (isset($buffer[$p = $i + $j]) && isset($search_pos[$buffer[$p]])) { |
|
131 | - $search_pos = $search_pos[$buffer[$p]]; |
|
132 | - // We have a complete pattern, save, in case we don't find a better match later |
|
133 | - if (isset($search_pos[-1]) && $search_pos[-1] < $last_found |
|
134 | - && $search_pos[-1] > $minReplaces) { |
|
135 | - $last_found = $search_pos[-1]; |
|
136 | - $last_size = $search_pos[-2]; |
|
137 | - } |
|
138 | - } |
|
139 | - // We got a complete pattern |
|
140 | - elseif (PHP_INT_MAX !== $last_found) { |
|
141 | - // Adding replacement datas to output buffer |
|
142 | - $rep_size = $this->repSize[$last_found]; |
|
143 | - for ($j = 0; $j < $rep_size; ++$j) { |
|
144 | - $newBuffer[] = $this->replace[$last_found][$j]; |
|
145 | - } |
|
146 | - // We Move cursor forward |
|
147 | - $i += $last_size - 1; |
|
148 | - // Edge Case, last position in buffer |
|
149 | - if ($i >= $buf_size) { |
|
150 | - $newBuffer[] = $buffer[$i]; |
|
151 | - } |
|
121 | + $newBuffer = []; |
|
122 | + $buf_size = \count($buffer); |
|
123 | + $last_size = 0; |
|
124 | + for ($i = 0; $i < $buf_size; ++$i) { |
|
125 | + $search_pos = $this->tree; |
|
126 | + $last_found = PHP_INT_MAX; |
|
127 | + // We try to find if the next byte is part of a search pattern |
|
128 | + for ($j = 0; $j <= $this->treeMaxLen; ++$j) { |
|
129 | + // We have a new byte for a search pattern |
|
130 | + if (isset($buffer[$p = $i + $j]) && isset($search_pos[$buffer[$p]])) { |
|
131 | + $search_pos = $search_pos[$buffer[$p]]; |
|
132 | + // We have a complete pattern, save, in case we don't find a better match later |
|
133 | + if (isset($search_pos[-1]) && $search_pos[-1] < $last_found |
|
134 | + && $search_pos[-1] > $minReplaces) { |
|
135 | + $last_found = $search_pos[-1]; |
|
136 | + $last_size = $search_pos[-2]; |
|
137 | + } |
|
138 | + } |
|
139 | + // We got a complete pattern |
|
140 | + elseif (PHP_INT_MAX !== $last_found) { |
|
141 | + // Adding replacement datas to output buffer |
|
142 | + $rep_size = $this->repSize[$last_found]; |
|
143 | + for ($j = 0; $j < $rep_size; ++$j) { |
|
144 | + $newBuffer[] = $this->replace[$last_found][$j]; |
|
145 | + } |
|
146 | + // We Move cursor forward |
|
147 | + $i += $last_size - 1; |
|
148 | + // Edge Case, last position in buffer |
|
149 | + if ($i >= $buf_size) { |
|
150 | + $newBuffer[] = $buffer[$i]; |
|
151 | + } |
|
152 | 152 | |
153 | - // We start the next loop |
|
154 | - continue 2; |
|
155 | - } else { |
|
156 | - // this byte is not in a pattern and we haven't found another pattern |
|
157 | - break; |
|
158 | - } |
|
159 | - } |
|
160 | - // Normal byte, move it to output buffer |
|
161 | - $newBuffer[] = $buffer[$i]; |
|
162 | - } |
|
153 | + // We start the next loop |
|
154 | + continue 2; |
|
155 | + } else { |
|
156 | + // this byte is not in a pattern and we haven't found another pattern |
|
157 | + break; |
|
158 | + } |
|
159 | + } |
|
160 | + // Normal byte, move it to output buffer |
|
161 | + $newBuffer[] = $buffer[$i]; |
|
162 | + } |
|
163 | 163 | |
164 | - return $newBuffer; |
|
165 | - } |
|
164 | + return $newBuffer; |
|
165 | + } |
|
166 | 166 | } |