forked from acquia/blt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWebServerHook.php
73 lines (62 loc) · 2.15 KB
/
WebServerHook.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
<?php
namespace Acquia\Blt\Robo\Hooks;
use Acquia\Blt\Robo\Common\IO;
use Acquia\Blt\Robo\Config\ConfigAwareTrait;
use Acquia\Blt\Robo\Exceptions\BltException;
use Acquia\Blt\Robo\Inspector\InspectorAwareInterface;
use Acquia\Blt\Robo\Inspector\InspectorAwareTrait;
use League\Container\ContainerAwareInterface;
use League\Container\ContainerAwareTrait;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
use Robo\Contract\ConfigAwareInterface;
use Symfony\Component\Filesystem\Filesystem;
/**
*
*/
class WebServerHook implements ConfigAwareInterface, ContainerAwareInterface, LoggerAwareInterface, InspectorAwareInterface {
use ConfigAwareTrait;
use ContainerAwareTrait;
use LoggerAwareTrait;
use InspectorAwareTrait;
use IO;
protected $serverUrl;
protected $serverPort;
/**
* @hook pre-command @launchWebServer
*/
public function launchWebServer() {
if ($this->getConfigValue('behat.run-server')) {
$this->serverUrl = $this->getConfigValue('behat.server.url');
$this->killWebServer();
$this->say("Launching PHP's internal web server via drush.");
$this->logger->info("Running server at $this->serverUrl...");
$fs = new Filesystem();
$fs->mkdir([$this->getConfigValue('repo.root') . '/tmp']);
$log_file = $this->getConfigValue('repo.root') . '/tmp/runserver.log';
/** @var \Acquia\Blt\Robo\Common\Executor $executor */
$executor = $this->getContainer()->get('executor');
$result = $executor
->drush("runserver $this->serverUrl")
->background(TRUE)
->run();
try {
$executor->waitForUrlAvailable($this->serverUrl);
}
catch (\Exception $e) {
if (!$result->wasSuccessful() && file_exists($log_file)) {
$output = file_get_contents($log_file);
unlink($log_file);
throw new BltException($output);
}
}
}
}
/**
* @hook post-command @launchWebServer
*/
public function killWebServer() {
$this->getContainer()->get('executor')->killProcessByName('runserver');
$this->getContainer()->get('executor')->killProcessByPort($this->getConfigValue('behat.server.port'));
}
}