1 | <?php |
||
20 | class ProcessBase extends Process |
||
21 | { |
||
22 | /** |
||
23 | * @var OutputStyle |
||
24 | */ |
||
25 | protected $output; |
||
26 | |||
27 | /** |
||
28 | * @var OutputInterface |
||
29 | */ |
||
30 | protected $stderr; |
||
31 | |||
32 | private $simulated = false; |
||
33 | |||
34 | private $verbose = false; |
||
35 | |||
36 | /** |
||
37 | * @var LoggerInterface |
||
38 | */ |
||
39 | private $logger; |
||
40 | |||
41 | /** |
||
42 | * Symfony 4 style constructor for creating Process instances from strings. |
||
43 | * @param string $command The commandline string to run |
||
44 | * @param string|null $cwd The working directory or null to use the working dir of the current PHP process |
||
45 | * @param array|null $env The environment variables or null to use the same environment as the current PHP process |
||
46 | * @param mixed|null $input The input as stream resource, scalar or \Traversable, or null for no input |
||
47 | * @param int|float|null $timeout The timeout in seconds or null to disable |
||
48 | * @return Process |
||
49 | */ |
||
50 | public static function fromShellCommandline($command, $cwd = null, array $env = null, $input = null, $timeout = 60) |
||
57 | |||
58 | /** |
||
59 | * realtimeStdout returns the output stream that realtime output |
||
60 | * should be sent to (if applicable) |
||
61 | * |
||
62 | * @return OutputStyle $output |
||
63 | */ |
||
64 | public function realtimeStdout() |
||
68 | |||
69 | protected function realtimeStderr() |
||
80 | |||
81 | /** |
||
82 | * setRealtimeOutput allows the caller to inject an OutputStyle object |
||
83 | * that will be used to stream realtime output if applicable. |
||
84 | * |
||
85 | * @param OutputStyle $output |
||
86 | */ |
||
87 | public function setRealtimeOutput(OutputInterface $output, $stderr = null) |
||
92 | |||
93 | /** |
||
94 | * @return bool |
||
95 | */ |
||
96 | public function isVerbose() |
||
97 | { |
||
98 | return $this->verbose; |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * @param bool $verbose |
||
103 | */ |
||
104 | public function setVerbose($verbose) |
||
108 | |||
109 | /** |
||
110 | * @return bool |
||
111 | */ |
||
112 | public function isSimulated() |
||
113 | { |
||
114 | return $this->simulated; |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * @param bool $simulated |
||
119 | */ |
||
120 | public function setSimulated($simulated) |
||
124 | |||
125 | /** |
||
126 | * @return LoggerInterface |
||
127 | */ |
||
128 | public function getLogger() |
||
132 | |||
133 | /** |
||
134 | * @param LoggerInterface $logger |
||
135 | */ |
||
136 | public function setLogger($logger) |
||
140 | |||
141 | /** |
||
142 | * @inheritDoc |
||
143 | */ |
||
144 | public function start(callable $callback = null, $env = array()) |
||
145 | { |
||
146 | $cmd = $this->getCommandLine(); |
||
147 | if ($this->isSimulated()) { |
||
148 | $this->getLogger()->notice('Simulating: ' . $cmd); |
||
149 | // Run a command that always succeeds. |
||
150 | $this->setCommandLine('exit 0'); |
||
151 | } elseif ($this->isVerbose()) { |
||
152 | $this->getLogger()->info('Executing: ' . $cmd); |
||
153 | } |
||
154 | parent::start($callback, $env); |
||
155 | // Set command back to original value in case anyone asks. |
||
156 | if ($this->isSimulated()) { |
||
157 | $this->setCommandLine($cmd); |
||
158 | } |
||
159 | } |
||
160 | |||
161 | /** |
||
162 | * Get Process output and decode its JSON. |
||
163 | * |
||
164 | * @return array |
||
165 | * An associative array. |
||
166 | */ |
||
167 | public function getOutputAsJson() |
||
180 | |||
181 | /** |
||
182 | * Return a realTime output object. |
||
183 | * |
||
184 | * @return callable |
||
185 | */ |
||
186 | public function showRealtime() |
||
192 | } |
||
193 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: