[TASK] Adapt FAL dumpFile to use PSR-7 response objects
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Http / SelfEmittableLazyOpenStream.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\LazyOpenStream;
20
21 /**
22 * This class implements a stream that can be used like a usual PSR-7 stream
23 * but is additionally able to provide a file-serving fastpath using readfile().
24 * The file this stream refers to is opened on demand.
25 *
26 * @internal
27 */
28 class SelfEmittableLazyOpenStream extends LazyOpenStream implements SelfEmittableStreamInterface
29 {
30 /**
31 * @var string
32 */
33 protected $filename;
34
35 /**
36 * Constructor setting up the PHP resource
37 *
38 * @param string $filename
39 */
40 public function __construct($filename)
41 {
42 parent::__construct($filename, 'r');
43 $this->filename = $filename;
44 }
45
46 /**
47 * Output the contents of the file to the output buffer
48 */
49 public function emit()
50 {
51 readfile($this->filename, false);
52 }
53
54 /**
55 * @return bool
56 */
57 public function isWritable(): bool
58 {
59 return false;
60 }
61
62 /**
63 * @param string $string
64 * @throws \RuntimeException on failure.
65 */
66 public function write($string)
67 {
68 throw new \RuntimeException('Cannot write to a ' . self::class, 1538331833);
69 }
70 }