‪TYPO3CMS  10.4
FileDeclaration.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 
19 
20 use Psr\Http\Message\ResponseInterface;
21 
28 {
29  public const ‪FLAG_BUILD_HTML = 1;
30  public const ‪FLAG_BUILD_PHP = 2;
31  public const ‪FLAG_BUILD_SVG = 4;
32  public const ‪FLAG_BUILD_HTML_DOCUMENT = 64;
33  public const ‪FLAG_BUILD_SVG_DOCUMENT = 128;
34 
38  protected ‪$fileLocation;
39 
43  protected ‪$fileName;
44 
48  protected ‪$fail;
49 
54 
58  protected ‪$unexpectedContentType;
59 
63  protected ‪$expectedContent;
64 
68  protected ‪$unexpectedContent;
69 
73  protected ‪$handler;
74 
78  protected ‪$buildFlags = self::FLAG_BUILD_HTML | ‪self::FLAG_BUILD_HTML_DOCUMENT;
79 
80  public function ‪__construct(‪FileLocation ‪$fileLocation, string ‪$fileName, bool ‪$fail = false)
81  {
82  $this->fileLocation = ‪$fileLocation;
83  $this->fileName = ‪$fileName;
84  $this->fail = ‪$fail;
85  }
86 
87  public function ‪buildContent(): string
88  {
89  $content = '';
90  if ($this->buildFlags & self::FLAG_BUILD_HTML) {
91  $content .= '<div>HTML content</div>';
92  }
93  if ($this->buildFlags & self::FLAG_BUILD_PHP) {
94  // base64 encoded representation of 'PHP content'
95  $content .= '<div><?php echo base64_decode(\'UEhQIGNvbnRlbnQ=\');?></div>';
96  }
97  if ($this->buildFlags & self::FLAG_BUILD_SVG) {
98  $content .= '<text id="test" x="0" y="0">SVG content</text>';
99  }
100  if ($this->buildFlags & self::FLAG_BUILD_SVG_DOCUMENT) {
101  return sprintf(
102  '<svg xmlns="http://www.w3.org/2000/svg">%s</svg>',
103  $content
104  );
105  }
106  return sprintf(
107  '<!DOCTYPE html><html lang="en"><body>%s</body></html>',
108  $content
109  );
110  }
111 
112  public function ‪matches(ResponseInterface $response): bool
113  {
114  return $this->‪getMismatches($response) === [];
115  }
116 
121  public function ‪getMismatches(ResponseInterface $response): array
122  {
123  $mismatches = [];
124  if ($this->handler instanceof \Closure) {
125  $result = $this->handler->call($this, $response);
126  if ($result !== null) {
127  $mismatches[] = $result;
128  }
129  return $mismatches;
130  }
131 
132  $body = (string)$response->getBody();
133  $contentType = $response->getHeaderLine('content-type');
134  if ($this->expectedContent !== null && strpos($body, $this->expectedContent) === false) {
135  $mismatches[] = new StatusMessage(
136  'content mismatch %s',
137  $this->expectedContent,
138  $body
139  );
140  }
141  if ($this->unexpectedContent !== null && strpos($body, $this->unexpectedContent) !== false) {
142  $mismatches[] = new StatusMessage(
143  'unexpected content %s',
144  $this->unexpectedContent,
145  $body
146  );
147  }
148  if ($this->expectedContentType !== null
149  && strpos($contentType . ';', $this->expectedContentType . ';') !== 0) {
150  $mismatches[] = new StatusMessage(
151  'content-type mismatch %s, got %s',
152  $this->expectedContentType,
153  $contentType
154  );
155  }
156  if ($this->unexpectedContentType !== null
157  && strpos($contentType . ';', $this->unexpectedContentType . ';') === 0) {
158  $mismatches[] = new ‪StatusMessage(
159  'unexpected content-type %s',
160  $this->unexpectedContentType,
161  $contentType
162  );
163  }
164  return $mismatches;
165  }
166 
167  public function ‪withExpectedContentType(string $contentType): self
168  {
169  $target = clone $this;
170  $target->expectedContentType = $contentType;
171  return $target;
172  }
173 
174  public function ‪withUnexpectedContentType(string $contentType): self
175  {
176  $target = clone $this;
177  $target->unexpectedContentType = $contentType;
178  return $target;
179  }
180 
181  public function ‪withExpectedContent(string $content): self
182  {
183  $target = clone $this;
184  $target->expectedContent = $content;
185  return $target;
186  }
187 
188  public function ‪withUnexpectedContent(string $content): self
189  {
190  $target = clone $this;
191  $target->unexpectedContent = $content;
192  return $target;
193  }
194 
195  public function ‪withHandler(\Closure ‪$handler): self
196  {
197  $target = clone $this;
198  $target->handler = ‪$handler;
199  return $target;
200  }
201 
202  public function ‪withBuildFlags(int ‪$buildFlags): self
203  {
204  $target = clone $this;
205  $target->buildFlags = ‪$buildFlags;
206  return $target;
207  }
208 
212  public function ‪getFileLocation(): ‪FileLocation
213  {
214  return ‪$this->fileLocation;
215  }
216 
220  public function ‪getFileName(): string
221  {
222  return ‪$this->fileName;
223  }
224 
228  public function ‪getUrl(): string
229  {
230  return $this->fileLocation->getBaseUrl() . ‪$this->fileName;
231  }
232 
236  public function ‪shallFail(): bool
237  {
238  return ‪$this->fail;
239  }
240 
244  public function ‪getExpectedContentType(): ?string
245  {
247  }
248 
252  public function ‪getUnexpectedContentType(): ?string
253  {
255  }
256 
260  public function ‪getExpectedContent(): ?string
261  {
263  }
264 
268  public function ‪getUnexpectedContent(): ?string
269  {
271  }
272 }
‪TYPO3\CMS\Install\SystemEnvironment\ServerResponse\FileDeclaration\getUnexpectedContentType
‪string null getUnexpectedContentType()
Definition: FileDeclaration.php:243
‪TYPO3\CMS\Install\SystemEnvironment\ServerResponse\FileDeclaration\withUnexpectedContent
‪withUnexpectedContent(string $content)
Definition: FileDeclaration.php:179
‪TYPO3\CMS\Install\SystemEnvironment\ServerResponse\FileDeclaration\FLAG_BUILD_SVG
‪const FLAG_BUILD_SVG
Definition: FileDeclaration.php:31
‪TYPO3\CMS\Install\SystemEnvironment\ServerResponse\FileDeclaration\$buildFlags
‪int $buildFlags
Definition: FileDeclaration.php:69
‪TYPO3\CMS\Install\SystemEnvironment\ServerResponse\FileDeclaration\$unexpectedContentType
‪string null $unexpectedContentType
Definition: FileDeclaration.php:53
‪TYPO3\CMS\Install\SystemEnvironment\ServerResponse\FileDeclaration\withExpectedContent
‪withExpectedContent(string $content)
Definition: FileDeclaration.php:172
‪TYPO3\CMS\Install\SystemEnvironment\ServerResponse\FileDeclaration\buildContent
‪buildContent()
Definition: FileDeclaration.php:78
‪TYPO3\CMS\Install\SystemEnvironment\ServerResponse\StatusMessage
Definition: StatusMessage.php:24
‪TYPO3\CMS\Install\SystemEnvironment\ServerResponse\FileDeclaration\getMismatches
‪StatusMessage[] getMismatches(ResponseInterface $response)
Definition: FileDeclaration.php:112
‪TYPO3\CMS\Install\SystemEnvironment\ServerResponse\FileDeclaration\$handler
‪Closure $handler
Definition: FileDeclaration.php:65
‪TYPO3\CMS\Install\SystemEnvironment\ServerResponse\FileDeclaration\shallFail
‪bool shallFail()
Definition: FileDeclaration.php:227
‪TYPO3\CMS\Install\SystemEnvironment\ServerResponse\FileDeclaration\withBuildFlags
‪withBuildFlags(int $buildFlags)
Definition: FileDeclaration.php:193
‪TYPO3\CMS\Install\SystemEnvironment\ServerResponse\FileDeclaration\getUnexpectedContent
‪string null getUnexpectedContent()
Definition: FileDeclaration.php:259
‪TYPO3\CMS\Install\SystemEnvironment\ServerResponse\FileDeclaration\getExpectedContentType
‪string null getExpectedContentType()
Definition: FileDeclaration.php:235
‪TYPO3\CMS\Install\SystemEnvironment\ServerResponse\FileDeclaration\$unexpectedContent
‪string null $unexpectedContent
Definition: FileDeclaration.php:61
‪TYPO3\CMS\Install\SystemEnvironment\ServerResponse
Definition: ContentSecurityPolicyDirective.php:18
‪TYPO3\CMS\Install\SystemEnvironment\ServerResponse\FileDeclaration\matches
‪matches(ResponseInterface $response)
Definition: FileDeclaration.php:103
‪TYPO3\CMS\Install\SystemEnvironment\ServerResponse\FileDeclaration\getUrl
‪string getUrl()
Definition: FileDeclaration.php:219
‪TYPO3\CMS\Install\SystemEnvironment\ServerResponse\FileDeclaration\getFileLocation
‪FileLocation getFileLocation()
Definition: FileDeclaration.php:203
‪TYPO3\CMS\Install\SystemEnvironment\ServerResponse\FileLocation
Definition: FileLocation.php:30
‪TYPO3\CMS\Install\SystemEnvironment\ServerResponse\FileDeclaration\FLAG_BUILD_SVG_DOCUMENT
‪const FLAG_BUILD_SVG_DOCUMENT
Definition: FileDeclaration.php:33
‪TYPO3\CMS\Install\SystemEnvironment\ServerResponse\FileDeclaration\withHandler
‪withHandler(\Closure $handler)
Definition: FileDeclaration.php:186
‪TYPO3\CMS\Install\SystemEnvironment\ServerResponse\FileDeclaration\$expectedContentType
‪string null $expectedContentType
Definition: FileDeclaration.php:49
‪TYPO3\CMS\Install\SystemEnvironment\ServerResponse\FileDeclaration\FLAG_BUILD_PHP
‪const FLAG_BUILD_PHP
Definition: FileDeclaration.php:30
‪TYPO3\CMS\Install\SystemEnvironment\ServerResponse\FileDeclaration\$fileName
‪string $fileName
Definition: FileDeclaration.php:41
‪TYPO3\CMS\Install\SystemEnvironment\ServerResponse\FileDeclaration\withUnexpectedContentType
‪withUnexpectedContentType(string $contentType)
Definition: FileDeclaration.php:165
‪TYPO3\CMS\Install\SystemEnvironment\ServerResponse\FileDeclaration
Definition: FileDeclaration.php:28
‪TYPO3\CMS\Install\SystemEnvironment\ServerResponse\FileDeclaration\$fail
‪bool $fail
Definition: FileDeclaration.php:45
‪TYPO3\CMS\Install\SystemEnvironment\ServerResponse\FileDeclaration\__construct
‪__construct(FileLocation $fileLocation, string $fileName, bool $fail=false)
Definition: FileDeclaration.php:71
‪TYPO3\CMS\Install\SystemEnvironment\ServerResponse\FileDeclaration\withExpectedContentType
‪withExpectedContentType(string $contentType)
Definition: FileDeclaration.php:158
‪TYPO3\CMS\Install\SystemEnvironment\ServerResponse\FileDeclaration\FLAG_BUILD_HTML
‪const FLAG_BUILD_HTML
Definition: FileDeclaration.php:29
‪TYPO3\CMS\Install\SystemEnvironment\ServerResponse\FileDeclaration\FLAG_BUILD_HTML_DOCUMENT
‪const FLAG_BUILD_HTML_DOCUMENT
Definition: FileDeclaration.php:32
‪TYPO3\CMS\Install\SystemEnvironment\ServerResponse\FileDeclaration\getExpectedContent
‪string null getExpectedContent()
Definition: FileDeclaration.php:251
‪TYPO3\CMS\Install\SystemEnvironment\ServerResponse\FileDeclaration\$expectedContent
‪string null $expectedContent
Definition: FileDeclaration.php:57
‪TYPO3\CMS\Install\SystemEnvironment\ServerResponse\FileDeclaration\$fileLocation
‪FileLocation $fileLocation
Definition: FileDeclaration.php:37
‪TYPO3\CMS\Install\SystemEnvironment\ServerResponse\FileDeclaration\getFileName
‪string getFileName()
Definition: FileDeclaration.php:211