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