|
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
|
2 |
|
public static function start($file, $port, $additional = NULL, $log_file = '/dev/null') |
|
25
|
|
|
{ |
|
26
|
|
|
if ( ! Network::is_port_open('localhost', $port)) |
|
27
|
|
|
throw new Exception('Port :port is already taken', array(':port' => $port)); |
|
28
|
|
|
|
|
29
|
2 |
|
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
|
2 |
|
return shell_exec(strtr('nohup :command > :log 2> :log & echo $!', array( |
|
33
|
2 |
|
':command' => self::command($file, $port, $additional), |
|
34
|
|
|
':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
|
|
|
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
|
2 |
|
public static function command($file, $port, $additional = NULL) |
|
56
|
|
|
{ |
|
57
|
|
|
$dir = realpath(__DIR__.'/../../../assets').'/'; |
|
58
|
|
|
|
|
59
|
2 |
|
$file = $dir.$file; |
|
60
|
|
|
|
|
61
|
|
|
if ( ! is_file($file)) |
|
62
|
|
|
throw new Exception('Cannot start phantomjs: file :file is not found', array(':file' => $file)); |
|
63
|
|
|
|
|
64
|
2 |
|
if ($additional) |
|
|
|
|
|
|
65
|
|
|
{ |
|
66
|
|
|
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
|
2 |
|
return "phantomjs --ssl-protocol=any --ignore-ssl-errors=true {$file} {$port} {$additional}"; |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: