Chris@17
|
1 <?php
|
Chris@17
|
2 namespace Consolidation\AnnotatedCommand;
|
Chris@17
|
3
|
Chris@17
|
4 /**
|
Chris@17
|
5 * Return a CommandResult as the result of a command to pass both an exit
|
Chris@17
|
6 * code and result data from a command.
|
Chris@17
|
7 *
|
Chris@17
|
8 * Usage:
|
Chris@17
|
9 *
|
Chris@17
|
10 * return CommandResult::dataWithExitCode(new RowsOfFields($rows), 1);
|
Chris@17
|
11 *
|
Chris@17
|
12 * The CommandResult can also be used to unambiguously return just
|
Chris@17
|
13 * an exit code or just output data.
|
Chris@17
|
14 *
|
Chris@17
|
15 * Exit code only:
|
Chris@17
|
16 *
|
Chris@17
|
17 * return CommandResult::dataWithExitCode(1);
|
Chris@17
|
18 *
|
Chris@17
|
19 * Data only:
|
Chris@17
|
20 *
|
Chris@17
|
21 * return CommandResult::data(new RowsOfFields($rows));
|
Chris@17
|
22 *
|
Chris@17
|
23 * Historically, it has always been possible to return an integer to indicate
|
Chris@17
|
24 * that the result is an exit code, and other return types (typically array
|
Chris@17
|
25 * / ArrayObjects) indicating actual data with an implicit exit code of 0.
|
Chris@17
|
26 * Using a CommandResult is preferred, though, as it allows the result of the
|
Chris@17
|
27 * function to be unambiguously specified without type-based interpretation.
|
Chris@17
|
28 *
|
Chris@17
|
29 * @package Consolidation\AnnotatedCommand
|
Chris@17
|
30 */
|
Chris@17
|
31 class CommandResult implements ExitCodeInterface, OutputDataInterface
|
Chris@17
|
32 {
|
Chris@17
|
33 protected $data;
|
Chris@17
|
34 protected $exitCode;
|
Chris@17
|
35
|
Chris@17
|
36 protected function __construct($data = null, $exitCode = 0)
|
Chris@17
|
37 {
|
Chris@17
|
38 $this->data = $data;
|
Chris@17
|
39 $this->exitCode = $exitCode;
|
Chris@17
|
40 }
|
Chris@17
|
41
|
Chris@17
|
42 public static function exitCode($exitCode)
|
Chris@17
|
43 {
|
Chris@17
|
44 return new self(null, $exitCode);
|
Chris@17
|
45 }
|
Chris@17
|
46
|
Chris@17
|
47 public static function data($data)
|
Chris@17
|
48 {
|
Chris@17
|
49 return new self($data);
|
Chris@17
|
50 }
|
Chris@17
|
51
|
Chris@17
|
52 public static function dataWithExitCode($data, $exitCode)
|
Chris@17
|
53 {
|
Chris@17
|
54 return new self($data, $exitCode);
|
Chris@17
|
55 }
|
Chris@17
|
56
|
Chris@17
|
57 public function getExitCode()
|
Chris@17
|
58 {
|
Chris@17
|
59 return $this->exitCode;
|
Chris@17
|
60 }
|
Chris@17
|
61
|
Chris@17
|
62 public function getOutputData()
|
Chris@17
|
63 {
|
Chris@17
|
64 return $this->data;
|
Chris@17
|
65 }
|
Chris@17
|
66
|
Chris@17
|
67 public function setOutputData($data)
|
Chris@17
|
68 {
|
Chris@17
|
69 $this->data = $data;
|
Chris@17
|
70 }
|
Chris@17
|
71 }
|