1 | <?php |
||
19 | class Config |
||
20 | { |
||
21 | |||
22 | /** |
||
23 | * Default configuration options |
||
24 | * @var array |
||
25 | */ |
||
26 | protected $default_config = [ |
||
27 | 'java_bin' => 'java', |
||
28 | 'server_jar' => '{base_dir}/resources/pjb621_standalone/JavaBridge.jar', |
||
29 | 'log_file' => '{base_dir}/var/pjbserver-port{tcp_port}.log', |
||
30 | 'pid_file' => '{base_dir}/var/pjbserver-port{tcp_port}.pid', |
||
31 | 'classpaths' => [] |
||
32 | ]; |
||
33 | |||
34 | /** |
||
35 | * Internal configuration array |
||
36 | * @var array |
||
37 | */ |
||
38 | protected $config; |
||
39 | |||
40 | |||
41 | /** |
||
42 | * Constructor |
||
43 | * |
||
44 | * <code> |
||
45 | * |
||
46 | * $params = [ |
||
47 | * // Port (required) |
||
48 | * 'port' => 8089, |
||
49 | * |
||
50 | * // Classpath autoloads (optional) |
||
51 | * 'classpaths' => [ |
||
52 | * '/my/path/to_specific/jar_file.jar', |
||
53 | * '/my/path/to_all_jars/*.jar' |
||
54 | * ], |
||
55 | * |
||
56 | * // Defaults (optional) |
||
57 | * 'java_bin' => 'java', |
||
58 | * 'server_jar' => '{base_dir}/resources/pjb621_standalone/JavaBridge.jar', |
||
59 | * 'log_file' => '{base_dir}/var/pjbserver-port{tcp_port}.log', |
||
60 | * 'pid_file' => '{base_dir}/var/pjbserver-port{tcp_port}.pid' |
||
61 | * |
||
62 | * ]; |
||
63 | * $config = new StandaloneServer\Config($params); |
||
64 | * </code> |
||
65 | * |
||
66 | * @throws Exception\InvalidArgumentException |
||
67 | * @param array $config |
||
68 | * |
||
69 | */ |
||
70 | 24 | public function __construct(array $config) |
|
71 | { |
||
72 | 24 | if (!isset($config['port'])) { |
|
73 | 3 | throw new Exception\InvalidArgumentException("Error missing required 'port' in config"); |
|
74 | 21 | } elseif (!filter_var($config['port'], FILTER_VALIDATE_INT) || $config['port'] < 1) { |
|
75 | 1 | throw new Exception\InvalidArgumentException("Option 'port' must be numeric greater than 0"); |
|
76 | } |
||
77 | 20 | $config = array_merge($this->getDefaultConfig($config['port']), $config); |
|
78 | 20 | $this->checkConfig($config); |
|
79 | 19 | $this->config = $config; |
|
80 | 19 | } |
|
81 | |||
82 | /** |
||
83 | * Return port on which standalone server listens |
||
84 | * @return int |
||
85 | */ |
||
86 | 15 | public function getPort() |
|
90 | |||
91 | /** |
||
92 | * Return jar file of the server |
||
93 | * @return string |
||
94 | */ |
||
95 | 13 | public function getServerJar() |
|
96 | { |
||
97 | 13 | return $this->config['server_jar']; |
|
98 | } |
||
99 | |||
100 | |||
101 | /** |
||
102 | * Return java binary |
||
103 | * @return string |
||
104 | */ |
||
105 | 13 | public function getJavaBin() |
|
106 | { |
||
107 | 13 | return $this->config['java_bin']; |
|
108 | } |
||
109 | |||
110 | /** |
||
111 | * Return log file |
||
112 | * @return string |
||
113 | */ |
||
114 | 13 | public function getLogFile() |
|
115 | { |
||
116 | 13 | return $this->config['log_file']; |
|
117 | } |
||
118 | |||
119 | /** |
||
120 | * Return an array containing the java classpath(s) for the server |
||
121 | * @return array |
||
122 | */ |
||
123 | 13 | public function getClasspaths() |
|
124 | { |
||
125 | 13 | return $this->config['classpaths']; |
|
126 | } |
||
127 | |||
128 | /** |
||
129 | * Return pid file where to store process id |
||
130 | * @return string |
||
131 | */ |
||
132 | 17 | public function getPidFile() |
|
133 | { |
||
134 | 17 | return $this->config['pid_file']; |
|
135 | } |
||
136 | |||
137 | /** |
||
138 | * Return standalone configuration |
||
139 | * |
||
140 | * @return array |
||
141 | */ |
||
142 | 2 | public function getConfig() |
|
143 | { |
||
144 | 2 | return $this->config; |
|
145 | } |
||
146 | |||
147 | /** |
||
148 | * Return default configuration options |
||
149 | * @param int $port |
||
150 | * @return array |
||
151 | */ |
||
152 | 20 | protected function getDefaultConfig($port) |
|
163 | |||
164 | /** |
||
165 | * Check configuration parameters |
||
166 | * @throws Exception\InvalidArgumentException |
||
167 | * @param array $config |
||
168 | */ |
||
169 | 20 | protected function checkConfig(array $config) |
|
170 | { |
||
233 | } |
||
234 |