1 | <?php |
||
13 | class Phantomjs { |
||
14 | |||
15 | /** |
||
16 | * Start a phantomjs server in the background. Set port, server js file, additional files and log file. |
||
17 | * |
||
18 | * @param string $file the server js file |
||
19 | * @param integer $port the port to start the server on |
||
20 | * @param string $additional additional file, passed to the js server |
||
21 | * @param string $log_file |
||
22 | * @return string the pid of the newly started process |
||
23 | */ |
||
24 | 3 | public static function start($file, $port, $additional = NULL, $log_file = '/dev/null') |
|
25 | { |
||
26 | 3 | if ( ! Network::is_port_open('localhost', $port)) |
|
27 | 1 | throw new Exception('Port :port is already taken', array(':port' => $port)); |
|
28 | |||
29 | 3 | if ($log_file !== '/dev/null' AND ! is_file($log_file)) |
|
30 | throw new Exception('Log file (:log_file) must be a file or /dev/null', array(':log_file' => $log_file)); |
||
31 | |||
32 | 3 | return shell_exec(strtr('nohup :command > :log 2> :log & echo $!', array( |
|
33 | 3 | ':command' => self::command($file, $port, $additional), |
|
34 | 3 | ':log' => $log_file, |
|
35 | ))); |
||
36 | } |
||
37 | |||
38 | /** |
||
39 | * kill a server on a given pid |
||
40 | * @param string $pid |
||
41 | */ |
||
42 | 2 | public static function kill($pid) |
|
46 | |||
47 | /** |
||
48 | * Return the command to start the phantomjs server |
||
49 | * |
||
50 | * @param string $file the server js file |
||
51 | * @param integer $port |
||
52 | * @param string $additional additional js file |
||
53 | * @return string |
||
54 | */ |
||
55 | 3 | public static function command($file, $port, $additional = NULL) |
|
74 | } |
||
75 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: