|
1
|
|
|
<?php |
|
2
|
|
|
namespace PjbServer\Tools; |
|
3
|
|
|
|
|
4
|
|
|
class StandaloneServer |
|
5
|
|
|
{ |
|
6
|
|
|
/** |
|
7
|
|
|
* @var int |
|
8
|
|
|
*/ |
|
9
|
|
|
protected $port; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* |
|
13
|
|
|
* @var array |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $config; |
|
16
|
|
|
|
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Tells whether the standalone server is started |
|
20
|
|
|
* @var boolean |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $started = false; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var array |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $required_arguments = array( |
|
28
|
|
|
'server_port' => FILTER_VALIDATE_INT, |
|
29
|
|
|
); |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Constructor |
|
33
|
|
|
* |
|
34
|
|
|
* @throws Exception\InvalidArgumentException |
|
35
|
|
|
* @param array $config |
|
36
|
|
|
*/ |
|
37
|
4 |
|
public function __construct(array $config) |
|
38
|
|
|
{ |
|
39
|
4 |
|
$this->checkConfig($config); |
|
40
|
2 |
|
$this->setServerPort($config['server_port']); |
|
41
|
|
|
|
|
42
|
2 |
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Check configuration parameters |
|
46
|
|
|
* @throws Exception\InvalidArgumentException |
|
47
|
|
|
* @param array $config |
|
48
|
|
|
*/ |
|
49
|
4 |
|
protected function checkConfig(array $config) |
|
50
|
|
|
{ |
|
51
|
4 |
|
foreach ($this->required_arguments as $name => $type) { |
|
52
|
4 |
|
if (!isset($config[$name])) { |
|
53
|
1 |
|
$msg = "Missing option '$name' in Standalone server configuration"; |
|
54
|
1 |
|
throw new Exception\InvalidArgumentException($msg); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
3 |
|
if ($type !== null && !filter_var($config[$name], $type)) { |
|
58
|
1 |
|
$msg = "Unsupported type in option '$name' in Standalone server configuration (required $type)"; |
|
59
|
1 |
|
throw new Exception\InvalidArgumentException($msg); |
|
60
|
|
|
} |
|
61
|
2 |
|
} |
|
62
|
2 |
|
} |
|
63
|
|
|
|
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Start the standalone server |
|
67
|
|
|
* @return void |
|
68
|
|
|
*/ |
|
69
|
1 |
|
public function start() |
|
70
|
|
|
{ |
|
71
|
1 |
|
$port = $this->getServerPort(); |
|
|
|
|
|
|
72
|
1 |
|
$this->started = true; |
|
73
|
|
|
|
|
74
|
|
|
/* |
|
|
|
|
|
|
75
|
|
|
$command = "java -cp $jar_dir/mysql-connector-java-5.1.36-bin.jar:$jar_file php.java.bridge.Standalone SERVLET:$port"; |
|
76
|
|
|
$error_file = "$test_dir/logs/pjb-standalone-error.log"; |
|
77
|
|
|
$pid_file = "$test_dir/logs/pjb-standalone.pid"; |
|
78
|
|
|
|
|
79
|
|
|
if (!self::isStandaloneServerRunning($pid_file)) { |
|
80
|
|
|
echo "\nStarting standalone pjb server:\n $command\n"; |
|
81
|
|
|
echo "@see logs in : $error_file\n"; |
|
82
|
|
|
echo "@see pid file in : $pid_file\n"; |
|
83
|
|
|
|
|
84
|
|
|
$cmd = sprintf("%s > %s 2>&1 & echo $! > %s", $command, $error_file, $pid_file); |
|
85
|
|
|
exec($cmd); |
|
86
|
|
|
|
|
87
|
|
|
// let time for server to start |
|
88
|
|
|
if (preg_match('/travis/', dirname(__FILE__))) { |
|
89
|
|
|
sleep(8); |
|
90
|
|
|
} else { |
|
91
|
|
|
sleep(1); |
|
92
|
|
|
} |
|
93
|
|
|
} else { |
|
94
|
|
|
echo "Standalone server already running, skipping start\n"; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
register_shutdown_function(array(__CLASS__, 'killStandaloneServer')); |
|
98
|
|
|
*/ |
|
99
|
1 |
|
} |
|
100
|
|
|
|
|
101
|
|
|
|
|
102
|
1 |
|
public function stop() |
|
103
|
|
|
{ |
|
104
|
1 |
|
$this->started = false; |
|
105
|
1 |
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Tells whether the standalone server is started |
|
109
|
|
|
* @return boolean |
|
110
|
|
|
*/ |
|
111
|
1 |
|
public function isStarted() |
|
112
|
|
|
{ |
|
113
|
1 |
|
return $this->started; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Restart the standalone server |
|
118
|
|
|
*/ |
|
119
|
|
|
public function restart() |
|
120
|
|
|
{ |
|
121
|
|
|
$this->stop(); |
|
122
|
|
|
$this->start(); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Return port on which standalone server listens |
|
127
|
|
|
* @return int |
|
128
|
|
|
*/ |
|
129
|
2 |
|
public function getServerPort() |
|
130
|
|
|
{ |
|
131
|
2 |
|
return $this->port; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Set port on which standalone server listens. |
|
136
|
|
|
* |
|
137
|
|
|
* @param int $port |
|
138
|
|
|
* @return void |
|
139
|
|
|
*/ |
|
140
|
2 |
|
protected function setServerPort($port) |
|
141
|
|
|
{ |
|
142
|
2 |
|
$this->port = $port; |
|
143
|
2 |
|
} |
|
144
|
|
|
|
|
145
|
|
|
} |
|
|
|
|
|
|
146
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.