1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Openbuildings\Spiderling; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* A class for starting and stopping phantomjs service, using Spiderling assets |
7
|
|
|
* |
8
|
|
|
* @package Openbuildings\Spiderling |
9
|
|
|
* @author Ivan Kerin |
10
|
|
|
* @copyright (c) 2013 OpenBuildings Ltd. |
11
|
|
|
* @license http://spdx.org/licenses/BSD-3-Clause |
12
|
|
|
*/ |
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) |
43
|
|
|
{ |
44
|
2 |
|
shell_exec('kill '.$pid); |
45
|
2 |
|
} |
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) |
56
|
|
|
{ |
57
|
3 |
|
$dir = realpath(__DIR__.'/../../../assets').'/'; |
58
|
|
|
|
59
|
3 |
|
$file = $dir.$file; |
60
|
|
|
|
61
|
3 |
|
if ( ! is_file($file)) |
62
|
|
|
throw new Exception('Cannot start phantomjs: file :file is not found', array(':file' => $file)); |
63
|
|
|
|
64
|
3 |
|
if ($additional) |
|
|
|
|
65
|
|
|
{ |
66
|
2 |
|
if ( ! is_file($file)) |
67
|
|
|
throw new Exception('Cannot start phantomjs: file :additional is not found', array(':additional' => $additional)); |
68
|
|
|
|
69
|
2 |
|
$additional = $dir.$additional; |
70
|
|
|
} |
71
|
|
|
|
72
|
3 |
|
return "phantomjs --ssl-protocol=any --ignore-ssl-errors=true {$file} {$port} {$additional}"; |
73
|
|
|
} |
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: