1<?php
2
3namespace Facebook\WebDriver\Remote\Service;
4
5use Facebook\WebDriver\Exception\DriverServerDiedException;
6use Facebook\WebDriver\Exception\WebDriverException;
7use Facebook\WebDriver\Remote\DriverCommand;
8use Facebook\WebDriver\Remote\HttpCommandExecutor;
9use Facebook\WebDriver\Remote\WebDriverCommand;
10use Facebook\WebDriver\Remote\WebDriverResponse;
11
12/**
13 * A HttpCommandExecutor that talks to a local driver service instead of a remote server.
14 */
15class DriverCommandExecutor extends HttpCommandExecutor
16{
17    /**
18     * @var DriverService
19     */
20    private $service;
21
22    public function __construct(DriverService $service)
23    {
24        parent::__construct($service->getURL());
25        $this->service = $service;
26    }
27
28    /**
29     * @param WebDriverCommand $command
30     *
31     * @throws \Exception
32     * @throws WebDriverException
33     * @return WebDriverResponse
34     */
35    public function execute(WebDriverCommand $command)
36    {
37        if ($command->getName() === DriverCommand::NEW_SESSION) {
38            $this->service->start();
39        }
40
41        try {
42            $value = parent::execute($command);
43            if ($command->getName() === DriverCommand::QUIT) {
44                $this->service->stop();
45            }
46
47            return $value;
48        } catch (\Exception $e) {
49            if (!$this->service->isRunning()) {
50                throw new DriverServerDiedException($e);
51            }
52            throw $e;
53        }
54    }
55}
56