1 | <?php |
||
35 | class AppConfig { |
||
36 | private $appName = 'files_antivirus'; |
||
37 | |||
38 | /** @var IConfig */ |
||
39 | private $config; |
||
40 | |||
41 | private $defaults = [ |
||
42 | 'av_mode' => 'executable', |
||
43 | 'av_socket' => '/var/run/clamav/clamd.ctl', |
||
44 | 'av_host' => '', |
||
45 | 'av_port' => '', |
||
46 | 'av_cmd_options' => '', |
||
47 | 'av_chunk_size' => '1024', |
||
48 | 'av_path' => '/usr/bin/clamscan', |
||
49 | 'av_max_file_size' => -1, |
||
50 | 'av_infected_action' => 'only_log', |
||
51 | ]; |
||
52 | |||
53 | /** |
||
54 | * AppConfig constructor. |
||
55 | * |
||
56 | * @param IConfig $config |
||
57 | */ |
||
58 | 5 | public function __construct(IConfig $config) { |
|
61 | |||
62 | /** |
||
63 | * Get full commandline |
||
64 | * @return string |
||
65 | */ |
||
66 | public function getCmdline(){ |
||
67 | $avCmdOptions = $this->getAvCmdOptions(); |
||
68 | |||
69 | $shellArgs = []; |
||
70 | if ($avCmdOptions) { |
||
71 | $shellArgs = explode(',', $avCmdOptions); |
||
72 | $shellArgs = array_map(function($i){ |
||
73 | return escapeshellarg($i); |
||
74 | }, |
||
75 | $shellArgs |
||
76 | ); |
||
77 | } |
||
78 | |||
79 | $preparedArgs = ''; |
||
80 | if (count($shellArgs)){ |
||
81 | $preparedArgs = implode(' ', $shellArgs); |
||
82 | } |
||
83 | return $preparedArgs; |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * Get all setting values as an array |
||
88 | * @return array |
||
89 | */ |
||
90 | public function getAllValues() { |
||
96 | |||
97 | /** |
||
98 | * Get a value by key |
||
99 | * @param string $key |
||
100 | * @return string |
||
101 | */ |
||
102 | 5 | public function getAppValue($key) { |
|
103 | 5 | $defaultValue = null; |
|
104 | 5 | if (array_key_exists($key, $this->defaults)){ |
|
105 | 5 | $defaultValue = $this->defaults[$key]; |
|
106 | 5 | } |
|
107 | 5 | return $this->config->getAppValue($this->appName, $key, $defaultValue); |
|
108 | } |
||
109 | |||
110 | /** |
||
111 | * Set a value by key |
||
112 | * @param string $key |
||
113 | * @param string $value |
||
114 | * @return string |
||
115 | */ |
||
116 | public function setAppValue($key, $value) { |
||
119 | |||
120 | /** |
||
121 | * Set a value with magic __call invocation |
||
122 | * @param string $key |
||
123 | * @param array $args |
||
124 | * @throws \BadFunctionCallException |
||
125 | */ |
||
126 | protected function setter($key, $args) { |
||
127 | if (array_key_exists($key, $this->defaults)) { |
||
128 | $this->setAppValue($key, $args[0]); |
||
129 | } else { |
||
130 | throw new \BadFunctionCallException($key . ' is not a valid key'); |
||
131 | } |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * Get a value with magic __call invocation |
||
136 | * @param string $key |
||
137 | * @return string |
||
138 | * @throws \BadFunctionCallException |
||
139 | */ |
||
140 | 5 | protected function getter($key) { |
|
147 | |||
148 | /** |
||
149 | * Translates property_name into propertyName |
||
150 | * @param string $property |
||
151 | * @return string |
||
152 | */ |
||
153 | protected function camelCase($property){ |
||
159 | |||
160 | /** |
||
161 | * Does all the someConfig to some_config magic |
||
162 | * @param string $property |
||
163 | * @return string |
||
164 | */ |
||
165 | 5 | protected function propertyToKey($property){ |
|
166 | 5 | $parts = preg_split('/(?=[A-Z])/', $property); |
|
167 | 5 | $column = null; |
|
168 | |||
169 | 5 | foreach($parts as $part){ |
|
170 | 5 | if($column === null){ |
|
171 | 5 | $column = $part; |
|
172 | 5 | } else { |
|
173 | 5 | $column .= '_' . lcfirst($part); |
|
174 | } |
||
175 | 5 | } |
|
176 | |||
177 | 5 | return $column; |
|
178 | } |
||
179 | |||
180 | /** |
||
181 | * Get/set an option value by calling getSomeOption method |
||
182 | * @param string $methodName |
||
183 | * @param array $args |
||
184 | * @return string|null |
||
185 | * @throws \BadFunctionCallException |
||
186 | */ |
||
187 | 5 | public function __call($methodName, $args){ |
|
199 | } |
||
200 |