-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPhantomJs.php
More file actions
99 lines (80 loc) · 2.02 KB
/
Copy pathPhantomJs.php
File metadata and controls
99 lines (80 loc) · 2.02 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
namespace WebDriverJs;
class PhantomJs {
/**
* Path to phantomjs.
*
* @var string
*/
protected $bin;
/**
* Function consruct.
*
* @param string $bin
* @param boolean $debug
*/
public function __construct($bin = "phantomjs")
{
$this->bin = $bin;
}
/**
* Function execute script.
*
* @param string $pathToScript
* @param array $runOptions
* @param array $options
*
* @return array or string
*/
public function execute($pathToScript, $runOptions = array(), $options = array())
{
$cmd = $this->bin
. " " . $this->runOptionsAsString($runOptions)
. " " . $pathToScript
. " " . $this->optionsAsString($options);
$response = shell_exec(escapeshellcmd($cmd));
if($this->isJsonFormat($response)) {
$response = json_decode($response, true);
}
return $response;
}
/**
* Convert array of run options to options string.
*
* @param array $runOptions
*
* @return string
*/
protected function runOptionsAsString($runOptions = array())
{
$runOptionsStr = "";
if(is_array($runOptions)) {
$options = array();
foreach($runOptions as $name => $value)
$options[] = "--{$name}={$value}";
if(!empty($options)) {
$runOptionsStr = implode(" ", $options);
}
}
return $runOptionsStr;
}
/**
* Convert array of options to options string
*
* @param array $options
* @return string
*/
protected function optionsAsString($options = array())
{
$optionsStr = "";
if((is_array($options)) && (!empty($options))) {
$optionsStr = implode(" ", $options);
}
return $optionsStr;
}
protected function isJsonFormat($data)
{
$json = json_decode($data);
return is_null($json) ? false: true;
}
}