Verzeichnisstruktur phpBB-3.2.0
- Veröffentlicht
- 06.01.2017
So funktioniert es
|
Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück |
Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis. |
|
(Beispiel Datei-Icons)
|
Auf das Icon klicken um den Quellcode anzuzeigen |
Utils.php
001 <?php
002 namespace GuzzleHttp\Stream;
003
004 use GuzzleHttp\Stream\Exception\SeekException;
005
006 /**
007 * Static utility class because PHP's autoloaders don't support the concept
008 * of namespaced function autoloading.
009 */
010 class Utils
011 {
012 /**
013 * Safely opens a PHP stream resource using a filename.
014 *
015 * When fopen fails, PHP normally raises a warning. This function adds an
016 * error handler that checks for errors and throws an exception instead.
017 *
018 * @param string $filename File to open
019 * @param string $mode Mode used to open the file
020 *
021 * @return resource
022 * @throws \RuntimeException if the file cannot be opened
023 */
024 public static function open($filename, $mode)
025 {
026 $ex = null;
027 set_error_handler(function () use ($filename, $mode, &$ex) {
028 $ex = new \RuntimeException(sprintf(
029 'Unable to open %s using mode %s: %s',
030 $filename,
031 $mode,
032 func_get_args()[1]
033 ));
034 });
035
036 $handle = fopen($filename, $mode);
037 restore_error_handler();
038
039 if ($ex) {
040 /** @var $ex \RuntimeException */
041 throw $ex;
042 }
043
044 return $handle;
045 }
046
047 /**
048 * Copy the contents of a stream into a string until the given number of
049 * bytes have been read.
050 *
051 * @param StreamInterface $stream Stream to read
052 * @param int $maxLen Maximum number of bytes to read. Pass -1
053 * to read the entire stream.
054 * @return string
055 */
056 public static function copyToString(StreamInterface $stream, $maxLen = -1)
057 {
058 $buffer = '';
059
060 if ($maxLen === -1) {
061 while (!$stream->eof()) {
062 $buf = $stream->read(1048576);
063 if ($buf === false) {
064 break;
065 }
066 $buffer .= $buf;
067 }
068 return $buffer;
069 }
070
071 $len = 0;
072 while (!$stream->eof() && $len < $maxLen) {
073 $buf = $stream->read($maxLen - $len);
074 if ($buf === false) {
075 break;
076 }
077 $buffer .= $buf;
078 $len = strlen($buffer);
079 }
080
081 return $buffer;
082 }
083
084 /**
085 * Copy the contents of a stream into another stream until the given number
086 * of bytes have been read.
087 *
088 * @param StreamInterface $source Stream to read from
089 * @param StreamInterface $dest Stream to write to
090 * @param int $maxLen Maximum number of bytes to read. Pass -1
091 * to read the entire stream.
092 */
093 public static function copyToStream(
094 StreamInterface $source,
095 StreamInterface $dest,
096 $maxLen = -1
097 ) {
098 if ($maxLen === -1) {
099 while (!$source->eof()) {
100 if (!$dest->write($source->read(1048576))) {
101 break;
102 }
103 }
104 return;
105 }
106
107 $bytes = 0;
108 while (!$source->eof()) {
109 $buf = $source->read($maxLen - $bytes);
110 if (!($len = strlen($buf))) {
111 break;
112 }
113 $bytes += $len;
114 $dest->write($buf);
115 if ($bytes == $maxLen) {
116 break;
117 }
118 }
119 }
120
121 /**
122 * Calculate a hash of a Stream
123 *
124 * @param StreamInterface $stream Stream to calculate the hash for
125 * @param string $algo Hash algorithm (e.g. md5, crc32, etc)
126 * @param bool $rawOutput Whether or not to use raw output
127 *
128 * @return string Returns the hash of the stream
129 * @throws SeekException
130 */
131 public static function hash(
132 StreamInterface $stream,
133 $algo,
134 $rawOutput = false
135 ) {
136 $pos = $stream->tell();
137
138 if ($pos > 0 && !$stream->seek(0)) {
139 throw new SeekException($stream);
140 }
141
142 $ctx = hash_init($algo);
143 while (!$stream->eof()) {
144 hash_update($ctx, $stream->read(1048576));
145 }
146
147 $out = hash_final($ctx, (bool) $rawOutput);
148 $stream->seek($pos);
149
150 return $out;
151 }
152
153 /**
154 * Read a line from the stream up to the maximum allowed buffer length
155 *
156 * @param StreamInterface $stream Stream to read from
157 * @param int $maxLength Maximum buffer length
158 *
159 * @return string|bool
160 */
161 public static function readline(StreamInterface $stream, $maxLength = null)
162 {
163 $buffer = '';
164 $size = 0;
165
166 while (!$stream->eof()) {
167 if (false === ($byte = $stream->read(1))) {
168 return $buffer;
169 }
170 $buffer .= $byte;
171 // Break when a new line is found or the max length - 1 is reached
172 if ($byte == PHP_EOL || ++$size == $maxLength - 1) {
173 break;
174 }
175 }
176
177 return $buffer;
178 }
179
180 /**
181 * Alias of GuzzleHttp\Stream\Stream::factory.
182 *
183 * @param mixed $resource Resource to create
184 * @param array $options Associative array of stream options defined in
185 * {@see \GuzzleHttp\Stream\Stream::__construct}
186 *
187 * @return StreamInterface
188 *
189 * @see GuzzleHttp\Stream\Stream::factory
190 * @see GuzzleHttp\Stream\Stream::__construct
191 */
192 public static function create($resource, array $options = [])
193 {
194 return Stream::factory($resource, $options);
195 }
196 }
197