‪TYPO3CMS  ‪main
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 
120  public function ‪getMismatches(ResponseInterface $response): array
121  {
122  $mismatches = [];
123  if ($this->handler instanceof \Closure) {
124  $result = $this->handler->call($this, $this, $response);
125  if ($result !== null) {
126  $mismatches[] = $result;
127  }
128  return $mismatches;
129  }
130 
131  $body = (string)$response->getBody();
132  $contentType = $response->getHeaderLine('content-type');
133  if ($this->expectedContent !== null && !str_contains($body, $this->expectedContent)) {
134  $mismatches[] = new StatusMessage(
135  'content mismatch %s',
136  $this->expectedContent,
137  $body
138  );
139  }
140  if ($this->unexpectedContent !== null && str_contains($body, $this->unexpectedContent)) {
141  $mismatches[] = new StatusMessage(
142  'unexpected content %s',
143  $this->unexpectedContent,
144  $body
145  );
146  }
147  if ($this->expectedContentType !== null
148  && !str_starts_with($contentType . ';', $this->expectedContentType . ';')) {
149  $mismatches[] = new StatusMessage(
150  'content-type mismatch %s, got %s',
151  $this->expectedContentType,
152  $contentType
153  );
154  }
155  if ($this->unexpectedContentType !== null
156  && str_starts_with($contentType . ';', $this->unexpectedContentType . ';')) {
157  $mismatches[] = new ‪StatusMessage(
158  'unexpected content-type %s',
159  $this->unexpectedContentType,
160  $contentType
161  );
162  }
163  return $mismatches;
164  }
165 
166  public function ‪withExpectedContentType(string $contentType): self
167  {
168  $target = clone $this;
169  $target->expectedContentType = $contentType;
170  return $target;
171  }
172 
173  public function ‪withUnexpectedContentType(string $contentType): self
174  {
175  $target = clone $this;
176  $target->unexpectedContentType = $contentType;
177  return $target;
178  }
179 
180  public function ‪withExpectedContent(string $content): self
181  {
182  $target = clone $this;
183  $target->expectedContent = $content;
184  return $target;
185  }
186 
187  public function ‪withUnexpectedContent(string $content): self
188  {
189  $target = clone $this;
190  $target->unexpectedContent = $content;
191  return $target;
192  }
193 
194  public function ‪withHandler(\Closure ‪$handler): self
195  {
196  $target = clone $this;
197  $target->handler = ‪$handler;
198  return $target;
199  }
200 
201  public function ‪withBuildFlags(int ‪$buildFlags): self
202  {
203  $target = clone $this;
204  $target->buildFlags = ‪$buildFlags;
205  return $target;
206  }
207 
208  public function ‪getFileLocation(): ‪FileLocation
209  {
210  return ‪$this->fileLocation;
211  }
212 
213  public function ‪getFileName(): string
214  {
215  return ‪$this->fileName;
216  }
217 
218  public function ‪getUrl(): string
219  {
220  return $this->fileLocation->getBaseUrl() . ‪$this->fileName;
221  }
222 
223  public function ‪shallFail(): bool
224  {
225  return ‪$this->fail;
226  }
227 
228  public function ‪getExpectedContentType(): ?string
229  {
231  }
232 
233  public function ‪getUnexpectedContentType(): ?string
234  {
236  }
237 
238  public function ‪getExpectedContent(): ?string
239  {
241  }
242 
243  public function ‪getUnexpectedContent(): ?string
244  {
246  }
247 }
‪TYPO3\CMS\Install\SystemEnvironment\ServerResponse\FileDeclaration\withUnexpectedContent
‪withUnexpectedContent(string $content)
Definition: FileDeclaration.php:178
‪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:171
‪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\getExpectedContent
‪getExpectedContent()
Definition: FileDeclaration.php:229
‪TYPO3\CMS\Install\SystemEnvironment\ServerResponse\FileDeclaration\getMismatches
‪StatusMessage[] getMismatches(ResponseInterface $response)
Definition: FileDeclaration.php:111
‪TYPO3\CMS\Install\SystemEnvironment\ServerResponse\FileDeclaration\withBuildFlags
‪withBuildFlags(int $buildFlags)
Definition: FileDeclaration.php:192
‪TYPO3\CMS\Install\SystemEnvironment\ServerResponse\FileDeclaration\$unexpectedContent
‪string null $unexpectedContent
Definition: FileDeclaration.php:61
‪TYPO3\CMS\Install\SystemEnvironment\ServerResponse\FileDeclaration\getFileLocation
‪getFileLocation()
Definition: FileDeclaration.php:199
‪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\$handler
‪Closure null $handler
Definition: FileDeclaration.php:65
‪TYPO3\CMS\Install\SystemEnvironment\ServerResponse\FileLocation
Definition: FileLocation.php:30
‪TYPO3\CMS\Install\SystemEnvironment\ServerResponse\FileDeclaration\getFileName
‪getFileName()
Definition: FileDeclaration.php:204
‪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\getUnexpectedContentType
‪getUnexpectedContentType()
Definition: FileDeclaration.php:224
‪TYPO3\CMS\Install\SystemEnvironment\ServerResponse\FileDeclaration\shallFail
‪shallFail()
Definition: FileDeclaration.php:214
‪TYPO3\CMS\Install\SystemEnvironment\ServerResponse\FileDeclaration\getUrl
‪getUrl()
Definition: FileDeclaration.php:209
‪TYPO3\CMS\Install\SystemEnvironment\ServerResponse\FileDeclaration\withHandler
‪withHandler(\Closure $handler)
Definition: FileDeclaration.php:185
‪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\getExpectedContentType
‪getExpectedContentType()
Definition: FileDeclaration.php:219
‪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:164
‪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\getUnexpectedContent
‪getUnexpectedContent()
Definition: FileDeclaration.php:234
‪TYPO3\CMS\Install\SystemEnvironment\ServerResponse\FileDeclaration\withExpectedContentType
‪withExpectedContentType(string $contentType)
Definition: FileDeclaration.php:157
‪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\$expectedContent
‪string null $expectedContent
Definition: FileDeclaration.php:57
‪TYPO3\CMS\Install\SystemEnvironment\ServerResponse\FileDeclaration\$fileLocation
‪FileLocation $fileLocation
Definition: FileDeclaration.php:37