‪TYPO3CMS  10.4
StreamFactory.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
18 namespace ‪TYPO3\CMS\Core\Http;
19 
20 use Psr\Http\Message\StreamFactoryInterface;
21 use Psr\Http\Message\StreamInterface;
22 
26 class ‪StreamFactory implements StreamFactoryInterface
27 {
34  public function ‪createStream(string $content = ''): StreamInterface
35  {
36  $stream = new ‪Stream('php://temp', 'r+');
37  if ($content !== '') {
38  $stream->write($content);
39  }
40  return $stream;
41  }
42 
54  public function ‪createStreamFromFile(string $filename, string $mode = 'r'): StreamInterface
55  {
56  $resource = @fopen($filename, $mode);
57  if ($resource === false) {
58  if ($mode === '' || in_array($mode[0], ['r', 'w', 'a', 'x', 'c'], true) === false) {
59  throw new \InvalidArgumentException('The mode ' . $mode . ' is invalid.', 1566823434);
60  }
61 
62  throw new \RuntimeException('The file ' . $filename . ' cannot be opened.', 1566823435);
63  }
64 
65  return new ‪Stream($resource);
66  }
67 
77  public function ‪createStreamFromResource($resource): StreamInterface
78  {
79  if (!is_resource($resource) || get_resource_type($resource) !== 'stream') {
80  throw new \InvalidArgumentException('Invalid stream provided; must be a stream resource', 1566853697);
81  }
82  return new ‪Stream($resource);
83  }
84 }
‪TYPO3\CMS\Core\Http\StreamFactory\createStreamFromResource
‪StreamInterface createStreamFromResource($resource)
Definition: StreamFactory.php:77
‪TYPO3\CMS\Core\Http\StreamFactory\createStreamFromFile
‪StreamInterface createStreamFromFile(string $filename, string $mode='r')
Definition: StreamFactory.php:54
‪TYPO3\CMS\Core\Http\StreamFactory\createStream
‪StreamInterface createStream(string $content='')
Definition: StreamFactory.php:34
‪TYPO3\CMS\Core\Http\Stream
Definition: Stream.php:29
‪TYPO3\CMS\Core\Http\StreamFactory
Definition: StreamFactory.php:27
‪TYPO3\CMS\Core\Http
Definition: AbstractApplication.php:18