‪TYPO3CMS  ‪main
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 {
33  public function ‪createStream(string $content = ''): StreamInterface
34  {
35  $stream = new ‪Stream('php://temp', 'r+');
36  if ($content !== '') {
37  $stream->write($content);
38  }
39  return $stream;
40  }
41 
52  public function ‪createStreamFromFile(string $filename, string $mode = 'r'): StreamInterface
53  {
54  $resource = @fopen($filename, $mode);
55  if ($resource === false) {
56  if ($mode === '' || in_array($mode[0], ['r', 'w', 'a', 'x', 'c'], true) === false) {
57  throw new \InvalidArgumentException('The mode ' . $mode . ' is invalid.', 1566823434);
58  }
59 
60  throw new \RuntimeException('The file ' . $filename . ' cannot be opened.', 1566823435);
61  }
62 
63  return new ‪Stream($resource);
64  }
65 
74  public function ‪createStreamFromResource($resource): StreamInterface
75  {
76  if (!is_resource($resource) || get_resource_type($resource) !== 'stream') {
77  throw new \InvalidArgumentException('Invalid stream provided; must be a stream resource', 1566853697);
78  }
79  return new ‪Stream($resource);
80  }
81 }
‪TYPO3\CMS\Core\Http\StreamFactory\createStream
‪createStream(string $content='')
Definition: StreamFactory.php:33
‪TYPO3\CMS\Core\Http\StreamFactory\createStreamFromResource
‪createStreamFromResource($resource)
Definition: StreamFactory.php:74
‪TYPO3\CMS\Core\Http\Stream
Definition: Stream.php:31
‪TYPO3\CMS\Core\Http\StreamFactory
Definition: StreamFactory.php:27
‪TYPO3\CMS\Core\Http\StreamFactory\createStreamFromFile
‪createStreamFromFile(string $filename, string $mode='r')
Definition: StreamFactory.php:52
‪TYPO3\CMS\Core\Http
Definition: AbstractApplication.php:18