Debian Codegraph @

vendor/wikimedia/shellbox/src/Command/BoxedCommand.php

raw
1 <?php
2
3 namespace Shellbox\Command;
4
5 use Psr\Http\Message\StreamInterface;
6 use Psr\Http\Message\UriInterface;
7 use Shellbox\Shellbox;
8 use Shellbox\ShellboxError;
9
10 /**
11 * A command with input and output files in an otherwise empty working directory.
12 */
13 class BoxedCommand extends Command {
14 /** @var string|null */
15 private $routeName;
16 /** @var InputFile[] */
17 private $inputFiles = [];
18 /** @var OutputFile[] */
19 private $outputFiles = [];
20 /** @var OutputGlob[] */
21 private $outputGlobs = [];
22 /** @var BoxedExecutor */
23 private $executor;
24
25 /**
26 * @internal Use BoxedExecutor::createCommand()
27 * @param BoxedExecutor $boxedExecutor
28 */
29 public function __construct( BoxedExecutor $boxedExecutor ) {
30 $this->executor = $boxedExecutor;
31 }
32
33 /**
34 * Set the route name. This should be a short string used by system
35 * administrators to identify the command being run, in order to route it
36 * to the correct container.
37 *
38 * @param string $routeName
39 * @return $this
40 */
41 public function routeName( string $routeName ) {
42 $this->routeName = $routeName;
43 return $this;
44 }
45
46 /**
47 * Add an input file, with the contents given by a string.
48 *
49 * @param string $boxedName The file name relative to the working directory
50 * @param string $contents The file contents
51 * @return $this
52 */
53 public function inputFileFromString( string $boxedName, string $contents ) {
54 return $this->inputFile( $boxedName, $this->newInputFileFromString( $contents ) );
55 }
56
57 /**
58 * Add an input file, with the contents copied from another file.
59 *
60 * @param string $boxedName The destination file name relative to the
61 * working directory
62 * @param string $sourcePath The path of the source file
63 * @return $this
64 */
65 public function inputFileFromFile( string $boxedName, string $sourcePath ) {
66 return $this->inputFile( $boxedName, $this->newInputFileFromFile( $sourcePath ) );
67 }
68
69 /**
70 * Add an input file, with the contents copied from a stream.
71 *
72 * @param string $boxedName The destination file name relative to the
73 * working directory
74 * @param StreamInterface $stream The source stream
75 * @return $this
76 */
77 public function inputFileFromStream( string $boxedName, StreamInterface $stream ) {
78 return $this->inputFile( $boxedName, $this->newInputFileFromStream( $stream ) );
79 }
80
81 /**
82 * Add an input file, with the contents fetched from a URL. The fetch may
83 * be done on the remote side.
84 *
85 * If you need to set options such as headers, use methods on the object
86 * returned from newInputFileFromUrl().
87 *
88 * @since 4.1.0
89 * @param string $boxedName The destination file name relative to the
90 * working directory
91 * @param UriInterface|string $url
92 * @return $this
93 */
94 public function inputFileFromUrl( string $boxedName, $url ) {
95 return $this->inputFile( $boxedName, $this->newInputFileFromUrl( $url ) );
96 }
97
98 /**
99 * Add an input file of any type. Use a factory function to create an
100 * InputFile object.
101 *
102 * @see newInputFileFromString
103 * @see newInputFileFromFile
104 * @see newInputFileFromStream
105 * @see newInputFileFromUrl
106 *
107 * @since 4.1.0
108 * @param string $boxedName The destination file name relative to the
109 * working directory
110 * @param InputFile $file
111 * @return $this
112 */
113 public function inputFile( string $boxedName, InputFile $file ) {
114 $boxedName = $this->normalizeBoxedPath( $boxedName );
115 $this->inputFiles[$boxedName] = $file;
116 return $this;
117 }
118
119 /**
120 * @see inputFileFromString
121 * @since 4.1.0
122 * @param string $contents
123 * @return InputFileFromString
124 */
125 public function newInputFileFromString( string $contents ) {
126 return new InputFileFromString( $contents );
127 }
128
129 /**
130 * @see inputFileFromFile
131 * @since 4.1.0
132 * @param string $sourcePath
133 * @return InputFileFromFile
134 */
135 public function newInputFileFromFile( string $sourcePath ) {
136 return new InputFileFromFile( $sourcePath );
137 }
138
139 /**
140 * @see inputFileFromStream
141 * @since 4.1.0
142 * @param StreamInterface $stream
143 * @return InputFileFromStream
144 */
145 public function newInputFileFromStream( StreamInterface $stream ) {
146 return new InputFileFromStream( $stream );
147 }
148
149 /**
150 * @see inputFileFromUrl
151 * @since 4.1.0
152 * @param string|UriInterface $url
153 * @return InputFileFromUrl
154 */
155 public function newInputFileFromUrl( $url ) {
156 if ( $this->areUrlFilesAllowed() ) {
157 return new InputFileFromUrl( $url );
158 } else {
159 throw new ShellboxError( "Download from URL is disabled" );
160 }
161 }
162
163 /**
164 * Register an output file. If the command creates it, the contents will
165 * be read into memory.
166 *
167 * @param string $boxedName The expected location of the file relative to
168 * the working directory.
169 * @return $this
170 */
171 public function outputFileToString( string $boxedName ) {
172 return $this->outputFile( $boxedName, $this->newOutputFileToString() );
173 }
174
175 /**
176 * Register an output file. If the command creates it, the contents will
177 * be copied to a specified location.
178 *
179 * @param string $boxedName The expected location of the file relative to
180 * the working directory.
181 * @param string $destPath The place where the file will be copied to
182 * @return $this
183 */
184 public function outputFileToFile( string $boxedName, string $destPath ) {
185 return $this->outputFile( $boxedName, $this->newOutputFileToFile( $destPath ) );
186 }
187
188 /**
189 * Register an output file. If the command creates it, the contents will be
190 * copied to a stream.
191 *
192 * @param string $boxedName The expected location of the file relative to
193 * the working directory.
194 * @param StreamInterface $stream
195 * @return $this
196 */
197 public function outputFileToStream( string $boxedName, StreamInterface $stream ) {
198 return $this->outputFile( $boxedName, $this->newOutputFileToStream( $stream ) );
199 }
200
201 /**
202 * Add an output file. If the command creates it, it will be sent to the
203 * given URL as a PUT request.
204 *
205 * If you need to set options such as headers, use methods on the object
206 * returned from newOutputFileToUrl().
207 *
208 * @since 4.1.0
209 * @param string $boxedName
210 * @param UriInterface|string $url
211 * @return $this
212 */
213 public function outputFileToUrl( string $boxedName, $url ) {
214 return $this->outputFile( $boxedName, $this->newOutputFileToUrl( $url ) );
215 }
216
217 /**
218 * Add an output file of any type. Use a factory function to create an
219 * OutputFile.
220 *
221 * @see newOutputFileToString
222 * @see newOutputFileToFile
223 * @see newOutputFileToStream
224 * @see newOutputFileToUrl
225 *
226 * @param string $boxedName The expected location of the file relative to
227 * the working directory.
228 * @param OutputFile $file
229 * @return $this
230 */
231 public function outputFile( string $boxedName, OutputFile $file ) {
232 $boxedName = $this->normalizeBoxedPath( $boxedName );
233 $this->outputFiles[$boxedName] = $file;
234 return $this;
235 }
236
237 /**
238 * @see outputFileToString
239 * @since 4.1.0
240 * @return OutputFileToString
241 */
242 public function newOutputFileToString() {
243 return new OutputFileToString;
244 }
245
246 /**
247 * @see outputFileToFile
248 * @since 4.1.0
249 * @param string $destPath
250 * @return OutputFileToFile
251 */
252 public function newOutputFileToFile( string $destPath ) {
253 return new OutputFileToFile( $destPath );
254 }
255
256 /**
257 * @see outputFileToStream
258 * @since 4.1.0
259 * @param StreamInterface $stream
260 * @return OutputFileToStream
261 */
262 public function newOutputFileToStream( StreamInterface $stream ) {
263 return new OutputFileToStream( $stream );
264 }
265
266 /**
267 * @see outputFileToUrl
268 * @since 4.1.0
269 * @param string|UriInterface $url
270 * @return OutputFileToUrl
271 */
272 public function newOutputFileToUrl( $url ) {
273 if ( $this->areUrlFilesAllowed() ) {
274 return new OutputFileToUrl( $url );
275 } else {
276 throw new ShellboxError( "Upload to URL is disabled" );
277 }
278 }
279
280 /**
281 * Register a series of expected output files identified by the pattern
282 * <prefix>*.<extension>
283 *
284 * Each file that appears in the working directory which matches the
285 * specified pattern will be read into memory.
286 *
287 * @param string $prefix The prefix, potentially including a subdirectory
288 * relative to the working directory.
289 * @param string $extension The file extension, not including the dot.
290 * @return $this
291 */
292 public function outputGlobToString( string $prefix, string $extension ) {
293 return $this->outputGlob( $this->newOutputGlobToString( $prefix, $extension ) );
294 }
295
296 /**
297 * Register a series of expected output files identified by the pattern
298 * <prefix>*.<extension>
299 *
300 * Each file that appears in the working directory which matches the
301 * specified pattern will be copied to the specified destination directory
302 *
303 * @param string $prefix The prefix, potentially including a subdirectory
304 * relative to the working directory.
305 * @param string $extension The file extension, not including the dot.
306 * @param string $destDir The destination directory, which must already
307 * exist.
308 * @return $this
309 */
310 public function outputGlobToFile( string $prefix, string $extension, string $destDir ) {
311 return $this->outputGlob( $this->newOutputGlobToFile( $prefix, $extension, $destDir ) );
312 }
313
314 /**
315 * Register a series of expected output files identified by a pattern
316 * <prefix>*.<extension>
317 *
318 * Each file that appears in the working directory which matches the
319 * specified pattern will be sent to the specified URL with a PUT request.
320 * The base name of the matched file will be appended to the path part of
321 * the URL.
322 *
323 * @param string $prefix The prefix, potentially including a subdirectory
324 * relative to the working directory.
325 * @param string $extension The file extension, not including the dot.
326 * @param string|UriInterface $destUrl The destination directory as a URL.
327 * The URL should end with a slash or some other application-dependent
328 * separator, such as %2F.
329 * @return $this
330 */
331 public function outputGlobToUrl( string $prefix, string $extension, $destUrl ) {
332 return $this->outputGlob( $this->newOutputGlobToUrl( $prefix, $extension, $destUrl ) );
333 }
334
335 /**
336 * Register a previously constructed output glob.
337 *
338 * @since 4.1.0
339 * @param OutputGlob $glob
340 * @return $this
341 */
342 public function outputGlob( OutputGlob $glob ) {
343 $this->outputGlobs[$glob->getId()] = $glob;
344 return $this;
345 }
346
347 /**
348 * @see outputGlobToString
349 * @param string $prefix
350 * @param string $extension
351 * @return OutputGlobToString
352 */
353 public function newOutputGlobToString( string $prefix, string $extension ) {
354 return new OutputGlobToString( $prefix, $extension );
355 }
356
357 /**
358 * @see outputGlobToFile
359 * @since 4.1.0
360 * @param string $prefix
361 * @param string $extension
362 * @param string $destDir
363 * @return OutputGlobToFile
364 */
365 public function newOutputGlobToFile( string $prefix, string $extension, string $destDir ) {
366 return new OutputGlobToFile( $prefix, $extension, $destDir );
367 }
368
369 /**
370 * @see outputGlobToUrl
371 * @since 4.1.0
372 * @param string $prefix
373 * @param string $extension
374 * @param string|UriInterface $destUrl
375 * @return OutputGlobToUrl
376 */
377 public function newOutputGlobToUrl( string $prefix, string $extension, $destUrl ) {
378 if ( $this->areUrlFilesAllowed() ) {
379 return new OutputGlobToUrl( $prefix, $extension, $destUrl );
380 } else {
381 throw new ShellboxError( "Upload to URL is disabled" );
382 }
383 }
384
385 /**
386 * @since 4.1.0
387 * Determine whether URL input/output files are supported by the executor.
388 * @return bool
389 */
390 public function areUrlFilesAllowed() {
391 return $this->executor->areUrlFilesAllowed();
392 }
393
394 /**
395 * Execute the command
396 * @return BoxedResult
397 */
398 public function execute(): BoxedResult {
399 if ( !$this->executor ) {
400 throw new ShellboxError( __METHOD__ .
401 ' cannot be called unless the executor is set' );
402 }
403 if ( $this->routeName === null ) {
404 throw new ShellboxError( __CLASS__ . ': the route name must be set' );
405 }
406 return $this->executor->execute( $this );
407 }
408
409 /**
410 * Get command parameters for JSON serialization by the client.
411 *
412 * @internal
413 * @return array
414 */
415 public function getClientData() {
416 $inputFiles = [];
417 foreach ( $this->inputFiles as $name => $file ) {
418 $inputFiles[$name] = $file->getClientData();
419 }
420 $outputFiles = [];
421 foreach ( $this->outputFiles as $name => $file ) {
422 $outputFiles[$name] = $file->getClientData();
423 }
424 $outputGlobs = [];
425 foreach ( $this->outputGlobs as $name => $glob ) {
426 $outputGlobs[$name] = $glob->getClientData();
427 }
428 // phpcs:ignore Generic.WhiteSpace.LanguageConstructSpacing.IncorrectSingle
429 return
430 [
431 'routeName' => $this->routeName,
432 'inputFiles' => $inputFiles,
433 'outputFiles' => $outputFiles,
434 'outputGlobs' => $outputGlobs
435 ] + parent::getClientData();
436 }
437
438 /**
439 * Set command parameters using a data array created by getClientData()
440 *
441 * @internal
442 * @param array $data
443 */
444 public function setClientData( $data ) {
445 foreach ( $data as $name => $value ) {
446 switch ( $name ) {
447 case 'routeName':
448 $this->routeName = $value;
449 break;
450
451 case 'inputFiles':
452 $this->inputFiles = [];
453 foreach ( $value as $fileName => $fileData ) {
454 $this->inputFiles[$fileName] =
455 InputFile::newFromClientData( $fileData );
456 }
457 break;
458
459 case 'outputFiles':
460 $this->outputFiles = [];
461 foreach ( $value as $fileName => $fileData ) {
462 $this->outputFiles[$fileName] =
463 OutputFile::newFromClientData( $fileData );
464 }
465 break;
466
467 case 'outputGlobs':
468 $this->outputGlobs = [];
469 foreach ( $value as $fileName => $globData ) {
470 $this->outputGlobs[$fileName] =
471 OutputGlob::newFromClientData( $globData );
472 }
473 break;
474 }
475 }
476 parent::setClientData( $data );
477 }
478
479 /**
480 * Validate a path within the working directory for path traversal safety
481 * and cross-platform file name compliance. Under Windows, the path may
482 * contain backslashes, which will be replaced with slashes.
483 *
484 * @param string $path
485 * @return string
486 * @throws ShellboxError
487 */
488 private function normalizeBoxedPath( $path ) {
489 return Shellbox::normalizePath( $path );
490 }
491
492 /**
493 * Get the route name
494 *
495 * @return string|null
496 */
497 public function getRouteName() {
498 return $this->routeName;
499 }
500
501 /**
502 * Get InputFile objects describing the registered input files.
503 *
504 * @internal
505 * @return InputFile[]
506 */
507 public function getInputFiles() {
508 return $this->inputFiles;
509 }
510
511 /**
512 * Get OutputFile objects describing the registered output files.
513 *
514 * @internal
515 * @return OutputFile[]
516 */
517 public function getOutputFiles() {
518 return $this->outputFiles;
519 }
520
521 /**
522 * Get OutputGlob objects describing the registered output globs.
523 *
524 * @internal
525 * @return OutputGlob[]
526 */
527 public function getOutputGlobs() {
528 return $this->outputGlobs;
529 }
530 }