1: <?php
2: namespace Peridot\WebDriverManager\Process;
3:
4: use Peridot\WebDriverManager\Binary\BinaryInterface;
5: use Peridot\WebDriverManager\Binary\DriverInterface;
6:
7: 8: 9: 10: 11:
12: class SeleniumProcess implements SeleniumProcessInterface
13: {
14: 15: 16:
17: protected $args = [];
18:
19: 20: 21:
22: protected $process = null;
23:
24: 25: 26:
27: protected $pipes = [];
28:
29: public function __construct()
30: {
31: $this->addArg('-jar');
32: }
33:
34: 35: 36: 37: 38: 39: 40:
41: public function addBinary(BinaryInterface $binary, $directory)
42: {
43: if (! $binary->exists($directory)) {
44: return;
45: }
46:
47: if ($binary instanceof DriverInterface) {
48: $this->addArg('-D' . $binary->getDriverPath($directory));
49: return;
50: }
51:
52: $this->addArg(realpath($directory . '/' . $binary->getFileName()));
53: }
54:
55: 56: 57: 58: 59:
60: public function getArgs()
61: {
62: return $this->args;
63: }
64:
65: 66: 67: 68: 69: 70:
71: public function addArg($arg)
72: {
73: $this->args[] = $arg;
74: $rest = array_slice(func_get_args(), 1);
75: foreach ($rest as $arg) {
76: $this->args[] = $arg;
77: }
78: }
79:
80: 81: 82: 83: 84: 85:
86: public function addArgs(array $args)
87: {
88: foreach ($args as $arg) {
89: $this->args[] = $arg;
90: }
91: }
92:
93: 94: 95: 96: 97:
98: public function isAvailable()
99: {
100: $command = 'java -version';
101: $descriptors = $this->getDescriptorSpec();
102: $this->process = proc_open($command, $descriptors, $this->pipes);
103: $status = $this->getStatus(true);
104: $available = $status['exitcode'] == 0;
105: proc_close($this->process);
106: return $available;
107: }
108:
109: 110: 111: 112: 113:
114: public function start($background = false)
115: {
116: $command = $this->getCommand();
117: if (! $background) {
118: exec($command);
119: return $this;
120: }
121:
122: $descriptors = $this->getDescriptorSpec();
123: $this->process = proc_open($command, $descriptors, $this->pipes);
124: return $this;
125: }
126:
127: 128: 129: 130: 131:
132: public function getCommand()
133: {
134: return 'java ' . implode(' ', $this->args);
135: }
136:
137: 138: 139: 140: 141: 142:
143: public function getStatus($loop = false)
144: {
145: $status = proc_get_status($this->process);
146: while ($loop && $status['running']) {
147: usleep(20000);
148: $status = proc_get_status($this->process);
149: }
150: return $status;
151: }
152:
153: 154: 155: 156: 157:
158: public function isRunning()
159: {
160: $status = $this->getStatus();
161: return $status['running'];
162: }
163:
164: 165: 166: 167: 168:
169: public function getError()
170: {
171: return stream_get_contents($this->pipes[2]);
172: }
173:
174: 175: 176: 177: 178:
179: public function close()
180: {
181: proc_terminate($this->process);
182: return proc_close($this->process);
183: }
184:
185: 186: 187: 188: 189:
190: private function getDescriptorSpec()
191: {
192: return [
193: 0 => ['pipe', 'r'],
194: 1 => ['pipe', 'w'],
195: 2 => ['pipe', 'w']
196: ];
197: }
198: }
199: