123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- <?php
- namespace Facebook\WebDriver\Remote\Service;
- use Exception;
- use Facebook\WebDriver\Net\URLChecker;
- use Symfony\Component\Process\Process;
- use Symfony\Component\Process\ProcessBuilder;
- class DriverService
- {
-
- private $executable;
-
- private $url;
-
- private $args;
-
- private $environment;
-
- private $process;
-
- public function __construct($executable, $port, $args = [], $environment = null)
- {
- $this->executable = self::checkExecutable($executable);
- $this->url = sprintf('http://localhost:%d', $port);
- $this->args = $args;
- $this->environment = $environment ?: $_ENV;
- }
-
- public function getURL()
- {
- return $this->url;
- }
-
- public function start()
- {
- if ($this->process !== null) {
- return $this;
- }
- $this->process = $this->createProcess();
- $this->process->start();
- $checker = new URLChecker();
- $checker->waitUntilAvailable(20 * 1000, $this->url . '/status');
- return $this;
- }
-
- public function stop()
- {
- if ($this->process === null) {
- return $this;
- }
- $this->process->stop();
- $this->process = null;
- $checker = new URLChecker();
- $checker->waitUntilUnavailable(3 * 1000, $this->url . '/shutdown');
- return $this;
- }
-
- public function isRunning()
- {
- if ($this->process === null) {
- return false;
- }
- return $this->process->isRunning();
- }
-
- protected static function checkExecutable($executable)
- {
- if (!is_file($executable)) {
- throw new Exception("'$executable' is not a file.");
- }
- if (!is_executable($executable)) {
- throw new Exception("'$executable' is not executable.");
- }
- return $executable;
- }
-
- private function createProcess()
- {
-
- if (class_exists(ProcessBuilder::class)
- && false === mb_strpos('@deprecated', (new \ReflectionClass(ProcessBuilder::class))->getDocComment())
- ) {
- $processBuilder = (new ProcessBuilder())
- ->setPrefix($this->executable)
- ->setArguments($this->args)
- ->addEnvironmentVariables($this->environment);
- return $processBuilder->getProcess();
- }
-
- $commandLine = array_merge([$this->executable], $this->args);
- return new Process($commandLine, null, $this->environment);
- }
- }
|