1 | <?php |
||
15 | class settings |
||
16 | { |
||
17 | /** @var \phpbb\filesystem\filesystem */ |
||
18 | protected $filesystem; |
||
19 | |||
20 | /** @var string */ |
||
21 | protected $config_path; |
||
22 | |||
23 | /** @var string phpEx */ |
||
24 | protected $php_ext; |
||
25 | |||
26 | /** @var string */ |
||
27 | protected $config_template; |
||
28 | |||
29 | /** |
||
30 | * Constructor |
||
31 | * |
||
32 | * @param \phpbb\filesystem\filesystem $filesystem File system |
||
33 | * @param string $config_path Filemanager config path |
||
34 | * @param string $php_ext phpEx |
||
35 | */ |
||
36 | 2 | public function __construct(\phpbb\filesystem\filesystem $filesystem, $config_path, $php_ext) |
|
44 | |||
45 | /** |
||
46 | * @return array|void |
||
47 | */ |
||
48 | 2 | public function get_settings() |
|
49 | { |
||
50 | 2 | $config_file = $this->get_config_file(); |
|
51 | |||
52 | 2 | if (!$this->filesystem->exists($config_file)) |
|
53 | 2 | { |
|
54 | $this->filesystem->copy($this->config_template, $config_file, true); |
||
55 | } |
||
56 | |||
57 | 2 | return include($config_file); |
|
58 | } |
||
59 | |||
60 | /** |
||
61 | * @param string $file |
||
62 | * @return void |
||
63 | */ |
||
64 | 2 | public function set_config_template($file) |
|
68 | |||
69 | /** |
||
70 | * @param array $settings |
||
71 | * @return void |
||
72 | */ |
||
73 | 1 | public function save(array $settings) |
|
74 | { |
||
75 | 1 | $curr_settings = $this->get_settings(); |
|
76 | 1 | $config_file = $this->get_config_file(); |
|
77 | 1 | $config_str = file_get_contents($config_file); |
|
78 | |||
79 | 1 | foreach ($settings as $prop => $value) |
|
80 | { |
||
81 | 1 | $this->type_cast_config_value($curr_settings[$prop], $value); |
|
82 | 1 | $config_str = preg_replace("/\s'$prop'(\s+)=>\s+(.*?),/i", " '$prop'$1=> $value,", $config_str); |
|
83 | 1 | } |
|
84 | |||
85 | 1 | $this->filesystem->dump_file($config_file, $config_str); |
|
86 | 1 | } |
|
87 | |||
88 | /** |
||
89 | * @param mixed $curr_val |
||
90 | * @param mixed $value |
||
91 | * @return void |
||
92 | */ |
||
93 | 1 | protected function type_cast_config_value($curr_val, &$value) |
|
94 | { |
||
95 | 1 | $type = gettype($curr_val); |
|
96 | switch ($type) |
||
97 | { |
||
98 | 1 | case 'string': |
|
99 | 1 | $value = "'$value'"; |
|
100 | 1 | break; |
|
101 | 1 | case 'integer': |
|
102 | 1 | $value = (int) $value; |
|
103 | 1 | break; |
|
104 | } |
||
105 | 1 | } |
|
106 | |||
107 | /** |
||
108 | * @return string |
||
109 | */ |
||
110 | 2 | protected function get_config_file() |
|
114 | } |
||
115 |