Complex classes like StandaloneServer often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use StandaloneServer, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | class StandaloneServer |
||
18 | { |
||
19 | |||
20 | /** |
||
21 | * @var int |
||
22 | */ |
||
23 | protected $port; |
||
24 | |||
25 | /** |
||
26 | * |
||
27 | * @var array |
||
28 | */ |
||
29 | protected $config; |
||
30 | |||
31 | /** |
||
32 | * Tells whether the standalone server is started |
||
33 | * @var boolean |
||
34 | */ |
||
35 | protected $started = false; |
||
36 | |||
37 | /** |
||
38 | * @var array |
||
39 | */ |
||
40 | protected $required_arguments = array( |
||
41 | 'port' => 'FILTER_VALIDATE_INT', |
||
42 | 'server_jar' => 'existing_file', |
||
43 | 'log_file' => 'file_with_existing_directory', |
||
44 | 'pid_file' => 'file_with_existing_directory', |
||
45 | ); |
||
46 | |||
47 | /** |
||
48 | * Default configuration options |
||
49 | * @var array |
||
50 | */ |
||
51 | protected $default_config = array( |
||
52 | 'server_jar' => '{base_dir}/resources/pjb621_standalone/JavaBridge.jar', |
||
53 | 'java_bin' => 'java', |
||
54 | 'log_file' => '{base_dir}/resources/pjb621_standalone/logs/pjbserver-port{tcp_port}.log', |
||
55 | 'pid_file' => '{base_dir}/resources/pjb621_standalone/var/run/pjbserver-port{tcp_port}.pid' |
||
56 | ); |
||
57 | |||
58 | /** |
||
59 | * Constructor |
||
60 | * |
||
61 | * <code> |
||
62 | * |
||
63 | * $config = array( |
||
64 | * 'port' => 8089, |
||
65 | * |
||
66 | * // optionally |
||
67 | * 'server_jar' => 'path/to/JavaBridge.jar' |
||
68 | * 'java_bin' => 'path/to/java' |
||
69 | * ); |
||
70 | * $server = new StandaloneServer($config); |
||
71 | * |
||
72 | * </code> |
||
73 | * |
||
74 | * @throws Exception\InvalidArgumentException |
||
75 | * @param array $config |
||
76 | */ |
||
77 | 10 | public function __construct(array $config) |
|
89 | |||
90 | /** |
||
91 | * Start the standalone server |
||
92 | * |
||
93 | * @throws Exception\RuntimeException |
||
94 | * |
||
95 | * @return void |
||
96 | */ |
||
97 | 3 | public function start() |
|
98 | { |
||
99 | |||
100 | 3 | if ($this->isStarted()) { |
|
101 | return; |
||
102 | } |
||
103 | |||
104 | 3 | $port = $this->getServerPort(); |
|
105 | |||
106 | 3 | if (!$this->isPortAvailable('localhost', $port)) { |
|
107 | $msg = "Cannot start server on port '$port', it's already in use."; |
||
108 | throw new Exception\RuntimeException($msg); |
||
109 | } |
||
110 | |||
111 | 3 | $command = $this->getCommand(); |
|
112 | |||
113 | 3 | $log_file = $this->config['log_file']; |
|
114 | 3 | $pid_file = $this->config['pid_file']; |
|
115 | 3 | $cmd = sprintf("%s > %s 2>&1 & echo $! > %s", $command, $log_file, $pid_file); |
|
116 | |||
117 | 3 | exec($cmd); |
|
118 | |||
119 | 3 | if (!file_exists($pid_file)) { |
|
120 | $msg = "Server not started, pid file was not created in '$pid_file'"; |
||
121 | throw new Exception\RuntimeException($msg); |
||
122 | } |
||
123 | 3 | if (!file_exists($log_file)) { |
|
124 | $msg = "Server not started, log file was not created in '$log_file'"; |
||
125 | throw new Exception\RuntimeException($msg); |
||
126 | } |
||
127 | |||
128 | // Loop for waiting correct start of phpjavabridge |
||
129 | 3 | $started = false; |
|
130 | 3 | $refresh_ms = 200; // 200ms |
|
131 | 3 | while (!$started) { |
|
132 | 3 | usleep($refresh_ms); |
|
133 | 3 | $log_file_content = file_get_contents($log_file); |
|
134 | 3 | if (preg_match('/Exception/', $log_file_content)) { |
|
135 | $msg = "Cannot start standalone server on port '$port', reason:\n"; |
||
136 | $msg .= $log_file_content; |
||
137 | throw new Exception\RuntimeException($msg); |
||
138 | } |
||
139 | |||
140 | 3 | clearstatcache($clear_realpath_cache = false, $log_file); |
|
141 | 3 | $log_file_content = file_get_contents($log_file); |
|
142 | 3 | if (preg_match('/JavaBridgeRunner started on/', $log_file_content)) { |
|
143 | 3 | $started = true; |
|
144 | 3 | } |
|
145 | |||
146 | 3 | } |
|
147 | 3 | $this->started = true; |
|
148 | 3 | } |
|
149 | |||
150 | /** |
||
151 | * Check if TCP port is available for binding |
||
152 | * |
||
153 | * @param int $port |
||
154 | * @param int $timeout |
||
155 | */ |
||
156 | 3 | protected function isPortAvailable($host, $port, $timeout = 1) |
|
157 | { |
||
158 | 3 | $available = false; |
|
159 | 3 | $fp = @stream_socket_client("tcp://$host:$port", $errno, $errstr, $timeout); |
|
160 | 3 | if (!$fp) { |
|
161 | 3 | $available = true; |
|
162 | 3 | } else { |
|
163 | fclose($fp); |
||
164 | } |
||
165 | 3 | return $available; |
|
166 | } |
||
167 | |||
168 | /** |
||
169 | * Return command used to start the standalone server |
||
170 | * @return string |
||
171 | */ |
||
172 | 3 | public function getCommand() |
|
190 | |||
191 | /** |
||
192 | * Stop the standalone server |
||
193 | * |
||
194 | * @throws Exception\RuntimeException |
||
195 | * @param boolean $throws_exception wether to throw exception if pid or process cannot be found or killed. |
||
196 | * @return void |
||
197 | */ |
||
198 | 3 | public function stop($throws_exception = false) |
|
235 | |||
236 | /** |
||
237 | * Tells whether the standalone server is started |
||
238 | * @return boolean |
||
239 | */ |
||
240 | 3 | public function isStarted() |
|
244 | |||
245 | /** |
||
246 | * Get runnin standalone server pid number |
||
247 | * |
||
248 | * @throws Exception\RuntimeException |
||
249 | * @return int |
||
250 | */ |
||
251 | 3 | public function getPid() |
|
265 | |||
266 | /** |
||
267 | * Restart the standalone server |
||
268 | */ |
||
269 | public function restart() |
||
274 | |||
275 | /** |
||
276 | * Return port on which standalone server listens |
||
277 | * @return int |
||
278 | */ |
||
279 | 4 | public function getServerPort() |
|
283 | |||
284 | /** |
||
285 | * Set port on which standalone server listens. |
||
286 | * |
||
287 | * @param int $port |
||
288 | * @return void |
||
289 | */ |
||
290 | 5 | protected function setServerPort($port) |
|
294 | |||
295 | /** |
||
296 | * Return standalone configuration |
||
297 | * |
||
298 | * @return array |
||
299 | */ |
||
300 | 3 | public function getConfig() |
|
304 | |||
305 | /** |
||
306 | * Return default configuration options |
||
307 | * @param int $port |
||
308 | * @return array |
||
309 | */ |
||
310 | 6 | protected function getDefaultConfig($port) |
|
321 | |||
322 | /** |
||
323 | * Check configuration parameters |
||
324 | * @throws Exception\InvalidArgumentException |
||
325 | * @param array $config |
||
326 | */ |
||
327 | 6 | protected function checkConfigRequiredArgs(array $config) |
|
367 | } |
||
368 |