Debian Codegraph @

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

raw
1 <?php
2
3 namespace Shellbox\Command;
4
5 use Shellbox\ShellboxError;
6
7 /**
8 * Class representing the result of running a BoxedCommand
9 */
10 class BoxedResult extends UnboxedResult {
11 /**
12 * On the server and after local execution, the uploaded files are
13 * represented here as OutputFileToUrl objects. On the client, they are
14 * OutputFilePlaceholder objects.
15 *
16 * @var OutputFile[]
17 */
18 private $files = [];
19
20 /**
21 * Add an output file to the result
22 *
23 * @internal For use by BoxedExecutor
24 * @param string $name The file name relative to the working directory
25 * @param OutputFile $outputFile
26 */
27 public function addOutputFile( $name, OutputFile $outputFile ) {
28 $this->files[$name] = $outputFile;
29 }
30
31 /**
32 * Get the contents of an output file as a string
33 *
34 * @param string $name The file name relative to the working directory
35 * @return string|null The contents, or null if the command did not create
36 * a file of that name, or if the output file was not registered in the
37 * BoxedCommand.
38 */
39 public function getFileContents( $name ) {
40 if ( !isset( $this->files[$name] ) ) {
41 return null;
42 }
43 $file = $this->files[$name];
44 if ( $file instanceof OutputFileWithContents ) {
45 return $file->getContents();
46 } else {
47 throw new ShellboxError(
48 "Can't provide contents of a file that was sent to a server" );
49 }
50 }
51
52 /**
53 * Query whether an output file was received. Files that are uploaded to a
54 * URL are not included in this.
55 *
56 * @param string $name
57 * @return bool
58 */
59 public function wasReceived( $name ) {
60 return isset( $this->files[$name] )
61 && ( $this->files[$name] instanceof OutputFileWithContents );
62 }
63
64 /**
65 * Query whether an output file was generated by the command
66 *
67 * @since 4.1.0
68 * @param string $name
69 * @return bool
70 */
71 public function wasGenerated( $name ) {
72 return isset( $this->files[$name] );
73 }
74
75 /**
76 * Get the names of all files which were registered and were created by the
77 * command.
78 *
79 * @return string[]
80 */
81 public function getFileNames() {
82 return array_keys( $this->files );
83 }
84
85 /**
86 * Get the names of all files which were received. This is all generated
87 * files except for uploaded files.
88 *
89 * @since 4.1.0
90 * @return string[]
91 */
92 public function getReceivedFileNames() {
93 $names = [];
94 foreach ( $this->files as $name => $file ) {
95 if ( $file instanceof OutputFileWithContents ) {
96 $names[] = $name;
97 }
98 }
99 return $names;
100 }
101
102 /**
103 * Get the names of all files which were uploaded.
104 *
105 * @since 4.1.0
106 * @return string[]
107 */
108 public function getUploadedFileNames() {
109 return array_diff( $this->getFileNames(), $this->getReceivedFileNames() );
110 }
111 }