1 | <?php |
||
37 | class AppConfig { |
||
38 | private $appName = 'files_antivirus'; |
||
39 | |||
40 | /** @var IConfig */ |
||
41 | private $config; |
||
42 | |||
43 | private $defaults = [ |
||
44 | 'av_mode' => 'executable', |
||
45 | 'av_socket' => '/var/run/clamav/clamd.ctl', |
||
46 | 'av_host' => '', |
||
47 | 'av_port' => '', |
||
48 | 5 | 'av_cmd_options' => '', |
|
49 | 5 | 'av_chunk_size' => '8192', |
|
50 | 5 | 'av_path' => '/usr/bin/clamscan', |
|
51 | 'av_max_file_size' => -1, |
||
52 | 'av_stream_max_length' => '26214400', |
||
53 | 'av_infected_action' => 'only_log', |
||
54 | ]; |
||
55 | |||
56 | /** |
||
57 | * AppConfig constructor. |
||
58 | * |
||
59 | * @param IConfig $config |
||
60 | */ |
||
61 | public function __construct(IConfig $config) { |
||
64 | |||
65 | /** |
||
66 | * Get full commandline |
||
67 | * @return string |
||
68 | */ |
||
69 | public function getCmdline(){ |
||
70 | $avCmdOptions = $this->getAvCmdOptions(); |
||
71 | |||
72 | $shellArgs = []; |
||
73 | if ($avCmdOptions) { |
||
74 | $shellArgs = explode(',', $avCmdOptions); |
||
75 | $shellArgs = array_map(function($i){ |
||
76 | return escapeshellarg($i); |
||
77 | }, |
||
78 | $shellArgs |
||
79 | ); |
||
80 | } |
||
81 | |||
82 | $preparedArgs = ''; |
||
83 | if (count($shellArgs)){ |
||
84 | $preparedArgs = implode(' ', $shellArgs); |
||
85 | } |
||
86 | return $preparedArgs; |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * Get all setting values as an array |
||
91 | * @return array |
||
92 | 5 | */ |
|
93 | 5 | public function getAllValues() { |
|
94 | 5 | $keys = array_keys($this->defaults); |
|
95 | 5 | $values = array_map(array($this, 'getAppValue'), $keys); |
|
96 | 5 | $preparedKeys = array_map(array($this, 'camelCase'), $keys); |
|
97 | 5 | return array_combine($preparedKeys, $values); |
|
98 | } |
||
99 | |||
100 | /** |
||
101 | * Get a value by key |
||
102 | * @param string $key |
||
103 | * @return string |
||
104 | */ |
||
105 | public function getAppValue($key) { |
||
106 | $defaultValue = null; |
||
107 | if (array_key_exists($key, $this->defaults)){ |
||
108 | $defaultValue = $this->defaults[$key]; |
||
109 | } |
||
110 | return $this->config->getAppValue($this->appName, $key, $defaultValue); |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * Set a value by key |
||
115 | * @param string $key |
||
116 | * @param string $value |
||
117 | * @return string |
||
118 | */ |
||
119 | public function setAppValue($key, $value) { |
||
122 | |||
123 | /** |
||
124 | * Set a value with magic __call invocation |
||
125 | * @param string $key |
||
126 | * @param array $args |
||
127 | * @throws \BadFunctionCallException |
||
128 | */ |
||
129 | protected function setter($key, $args) { |
||
136 | |||
137 | /** |
||
138 | * Get a value with magic __call invocation |
||
139 | * @param string $key |
||
140 | * @return string |
||
141 | * @throws \BadFunctionCallException |
||
142 | */ |
||
143 | protected function getter($key) { |
||
150 | |||
151 | /** |
||
152 | * Translates property_name into propertyName |
||
153 | * @param string $property |
||
154 | * @return string |
||
155 | 5 | */ |
|
156 | 5 | protected function camelCase($property){ |
|
162 | 5 | ||
163 | 5 | /** |
|
164 | * Does all the someConfig to some_config magic |
||
165 | 5 | * @param string $property |
|
166 | * @return string |
||
167 | 5 | */ |
|
168 | protected function propertyToKey($property){ |
||
182 | 5 | ||
183 | 5 | /** |
|
184 | * Get/set an option value by calling getSomeOption method |
||
185 | * @param string $methodName |
||
186 | * @param array $args |
||
187 | * @return string|null |
||
188 | * @throws \BadFunctionCallException |
||
189 | */ |
||
190 | public function __call($methodName, $args){ |
||
202 | } |
||
203 |