[TASK] Adapt FAL dumpFile to use PSR-7 response objects
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Http / FalDumpFileContentsDecoratorStream.php
1 <?php
2 declare(strict_types = 1);
3
4 namespace TYPO3\CMS\Core\Http;
5
6 /*
7 * This file is part of the TYPO3 CMS project.
8 *
9 * It is free software; you can redistribute it and/or modify it under
10 * the terms of the GNU General Public License, either version 2
11 * of the License, or any later version.
12 *
13 * For the full copyright and license information, please read the
14 * LICENSE.txt file that was distributed with this source code.
15 *
16 * The TYPO3 project - inspiring people to share!
17 */
18
19 use GuzzleHttp\Psr7\StreamDecoratorTrait;
20 use Psr\Http\Message\StreamInterface;
21 use TYPO3\CMS\Core\Resource\Driver\DriverInterface;
22
23 /**
24 * A lazy stream, that wraps the FAL dumpFileContents() method to send file contents
25 * using emit(), as defined in SelfEmittableStreamInterface.
26 * This call will fall back to the FAL getFileContents() method if the fastpath possibility
27 * using SelfEmittableStreamInterface is not used.
28 *
29 * @internal
30 */
31 class FalDumpFileContentsDecoratorStream implements StreamInterface, SelfEmittableStreamInterface
32 {
33 use StreamDecoratorTrait;
34
35 /**
36 * @var string
37 */
38 protected $identifier;
39
40 /**
41 * @var DriverInterface
42 */
43 protected $driver;
44
45 /**
46 * @var int
47 */
48 protected $size;
49
50 /**
51 * @param string $identifier
52 * @param DriverInterface $driver
53 * @param int $size
54 */
55 public function __construct(string $identifier, DriverInterface $driver, int $size)
56 {
57 $this->identifier = $identifier;
58 $this->driver = $driver;
59 $this->size = $size;
60 }
61
62 /**
63 * Emit the response to stdout, as specified in SelfEmittableStreamInterface.
64 * Offload to the driver method dumpFileContents.
65 */
66 public function emit()
67 {
68 $this->driver->dumpFileContents($this->identifier);
69 }
70
71 /**
72 * Creates a stream (on demand). This method is consumed by the guzzle StreamDecoratorTrait
73 * and is used when this stream is used without the emit() fastpath.
74 *
75 * @return StreamInterface
76 */
77 protected function createStream(): StreamInterface
78 {
79 $stream = new Stream('php://temp', 'rw');
80 $stream->write($this->driver->getFileContents($this->identifier));
81 return $stream;
82 }
83
84 /**
85 * @return int
86 */
87 public function getSize(): int
88 {
89 return $this->size;
90 }
91
92 /**
93 * @return bool
94 */
95 public function isWritable(): bool
96 {
97 return false;
98 }
99
100 /**
101 * @param string $string
102 * @throws \RuntimeException on failure.
103 */
104 public function write($string)
105 {
106 throw new \RuntimeException('Cannot write to a ' . self::class, 1538331852);
107 }
108 }