-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFakeProcess.php
188 lines (153 loc) · 4.05 KB
/
FakeProcess.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?php
/**
* @author Krystian Kuczek <[email protected]>
* @copyright 2013-2016 Hexmedia.pl
* @license @see LICENSE
*/
namespace Hexmedia\Symfony\FakeProcess;
use Symfony\Component\Process\Exception\RuntimeException;
use Symfony\Component\Process\Process as BaseProcess;
/**
* Class FakeProcess
*
* @package Hexmedia\Symfony\FakeProcess
*/
class FakeProcess extends BaseProcess
{
private $commandline;
private $callback;
private $command;
private $response;
/**
* @var array
*/
private $commands;
/**
* @var array
*/
private $commandsRuns = array();
/**
* Process constructor.
*
* @param string $commandline
* @param null|string $cwd
* @param array|null $env
* @param null|string $input
* @param float|int|null $timeout
* @param array $options
*/
public function __construct($commandline, $cwd = null, array $env = null, $input = null, $timeout = 60, array $options = array())
{
$this->commandline = $commandline;
$this->commands = array();
parent::__construct($commandline, $cwd, $env, $input, $timeout, $options);
}
/**
* @param string $command
* @param callable $callback
* @param int $exitCode
*/
public function addCommand($command, $callback, $exitCode)
{
$this->commands[] = array('command' => $command, 'callback' => $callback, 'exitCode' => $exitCode);
}
/**
* @param array $commands
*
* @return $this
*/
public function setCommands(array $commands)
{
$this->commands = $commands;
return $this;
}
/**
* @param string $commandName
*
* @return callable
*/
public function getCommand($commandName)
{
$command = '';
$possible = array();
foreach ($this->commands as $values) {
$command = $values['command'];
if (preg_match("#" . $command . "#", $commandName)) {
$possible[] = $values;
}
}
if (sizeof($possible) == 1) {
return $possible[0];
}
if (sizeof($possible)) {
$index = 0;
if (isset($this->commandsRuns[$command])) {
if (sizeof($possible) <= $this->commandsRuns[$command]) {
$this->commandsRuns[$command] = 0;
}
$index = $this->commandsRuns[$command];
}
return $possible[$index];
}
return false;
}
/**
* {@inheritdoc}
*/
public function start(callable $callback = null)
{
if ($this->isRunning()) {
throw new RuntimeException('Process is already running');
}
$this->callback = $this->buildCallback($callback);
$commandline = $this->commandline;
$this->command = $this->getCommand($commandline);
if (!isset($this->commandsRuns[$this->command['command']])) {
$this->commandsRuns[$this->command['command']] = 0;
}
$this->commandsRuns[$this->command['command']]++;
if (false === $this->command) {
throw new \Exception(sprintf('No command "%s"!', $commandline));
}
$this->updateStatus(false);
$this->checkTimeout();
}
/**
* {@inheritdoc}
*/
public function wait(callable $callback = null)
{
$commandCallback = $this->command['callback'];
$exitCode = $this->command['exitCode'];
$this->response = $commandCallback($this->commandline);
return (int) $exitCode;
}
/**
* {@inheritdoc}
*/
protected function updateStatus($blocking)
{
//Do nothing:)
}
/**
* @return string
*/
public function getErrorOutput()
{
return '';
}
/**
* @return int
*/
public function getExitCode()
{
return $this->command['exitCode'];
}
/**
* @return string
*/
public function getOutput()
{
return $this->response;
}
}