Overview

Namespaces

  • Peridot
    • WebDriverManager
      • Binary
        • Decompression
        • Request
      • Console
      • Event
      • OS
      • Process
      • Test
  • PHP

Classes

  • SeleniumProcess

Interfaces

  • SeleniumProcessInterface
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php
  2: namespace Peridot\WebDriverManager\Process;
  3: 
  4: use Peridot\WebDriverManager\Binary\BinaryInterface;
  5: use Peridot\WebDriverManager\Binary\DriverInterface;
  6: 
  7: /**
  8:  * SeleniumProcess is responsible for controlling Selenium Server processes.
  9:  *
 10:  * @package Peridot\WebDriverManager\Process
 11:  */
 12: class SeleniumProcess implements SeleniumProcessInterface
 13: {
 14:     /**
 15:      * @var array
 16:      */
 17:     protected $args = [];
 18: 
 19:     /**
 20:      * @var resource
 21:      */
 22:     protected $process = null;
 23: 
 24:     /**
 25:      * @var array
 26:      */
 27:     protected $pipes = [];
 28: 
 29:     public function __construct()
 30:     {
 31:         $this->addArg('-jar');
 32:     }
 33: 
 34:     /**
 35:      * {@inheritdoc}
 36:      *
 37:      * @param BinaryInterface $binary
 38:      * @param string $directory
 39:      * @return void
 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:      * {@inheritdoc}
 57:      *
 58:      * @return array
 59:      */
 60:     public function getArgs()
 61:     {
 62:         return $this->args;
 63:     }
 64: 
 65:     /**
 66:      * {@inheritdoc}
 67:      *
 68:      * @param string $arg
 69:      * @return void
 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:      * {@inheritdoc}
 82:      *
 83:      * @param array $args
 84:      * @return void
 85:      */
 86:     public function addArgs(array $args)
 87:     {
 88:         foreach ($args as $arg) {
 89:             $this->args[] = $arg;
 90:         }
 91:     }
 92: 
 93:     /**
 94:      * {@inheritdoc}
 95:      *
 96:      * @return bool
 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:      * {@inheritdoc}
111:      *
112:      * @return $this
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:      * {@inheritdoc}
129:      *
130:      * @return string
131:      */
132:     public function getCommand()
133:     {
134:         return 'java ' . implode(' ', $this->args);
135:     }
136: 
137:     /**
138:      * {@inheritdoc}
139:      *
140:      * @param bool $loop
141:      * @return array
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:      * {@inheritdoc}
155:      *
156:      * @return bool
157:      */
158:     public function isRunning()
159:     {
160:         $status = $this->getStatus();
161:         return $status['running'];
162:     }
163: 
164:     /**
165:      * {@inheritdoc}
166:      *
167:      * @return string
168:      */
169:     public function getError()
170:     {
171:         return stream_get_contents($this->pipes[2]);
172:     }
173: 
174:     /**
175:      * {@inheritdoc}
176:      *
177:      * @return int
178:      */
179:     public function close()
180:     {
181:         proc_terminate($this->process);
182:         return proc_close($this->process);
183:     }
184: 
185:     /**
186:      * Helper to create an array suitable as a descriptor for process streams.
187:      *
188:      * @return array
189:      */
190:     private function getDescriptorSpec()
191:     {
192:         return [
193:             0 => ['pipe', 'r'],
194:             1 => ['pipe', 'w'],
195:             2 => ['pipe', 'w']
196:         ];
197:     }
198: }
199: 
WebDriver Manager API documentation generated by ApiGen