-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStarter.php
More file actions
82 lines (73 loc) · 2.6 KB
/
Copy pathStarter.php
File metadata and controls
82 lines (73 loc) · 2.6 KB
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
74
75
76
77
78
79
80
81
82
<?php
/**
* Mazelab\Phantomjs\Starter
*
* @author Jens Klose <jens.klose@googlemail.com>
* @license http://opensource.org/licenses/MIT MIT
*/
namespace Mazelab\Phantomjs;
use Symfony\Component\EventDispatcher\GenericEvent;
use Symfony\Component\Process\Exception\RuntimeException;
use Symfony\Component\Process\Process;
class Starter
{
private $port = 8643;
private $options = '--proxy-type=none --ignore-ssl-errors=true';
/** @var string */
private $phantomJsPath;
private $process;
/**
* Starter constructor.
*
* @param int $port webdriver port number which is passed to the --webdriver option
* @param string $options other additional options. Defaults to '--proxy-type=none --ignore-ssl-errors=true'
* @param string $phantomJsPath path to the phantomjs executable. Defaults to global 'phantomjs'
*/
public function __construct($port = null, $options = null, $phantomJsPath = 'phantomjs')
{
!isset ($port) ?: $this->port = $port;
!isset ($options) ?: $this->options .= ' ' . $options;
$this->phantomJsPath = $phantomJsPath;
}
/**
* Starts a new phantomjs process in background
*
* @throws \Symfony\Component\Process\Exception\RuntimeException
*/
public function up()
{
$this->killAllRunning();
$this->process = new Process($this->phantomJsPath . ' --webdriver=' . $this->port . ' ' . $this->options);
$process = $this->process;
$output = new GenericEvent();
$process->setTimeout(null);
$process->start(function () use ($process, $output) {
$output->setArgument('output', $process->getIncrementalOutput());
});
$phantomjsOnline = false;
$portScan = false;
while (! $phantomjsOnline) {
if ($output->hasArgument('output')) {
$portScan = strpos($output->getArgument('output'), 'running on port ' . $this->port);
}
if ($portScan) {
echo $output->getArgument('output');
}
$phantomjsOnline = $process->isStarted() && $process->isRunning() && $portScan;
if ($process->isTerminated()) {
throw new RuntimeException('Phantomjs could not be started with webdriver on port ' . $this->port);
}
}
}
/**
* Search and destroy all running phantoms on the same port
*/
public function killAllRunning()
{
exec("pkill -f '" . $this->phantomJsPath . " --webdriver=" . $this->port . " '");
}
public function __destruct()
{
$this->killAllRunning();
}
}