Debian Codegraph @

classes/Config.php

raw symbols SCIP
1 <?php
2
3 /**
4 * dmarc-srg - A php parser, viewer and summary report generator for incoming DMARC reports.
5 * Copyright (C) 2022-2024 Aleksey Andreev (liuch)
6 *
7 * Available at:
8 * https://github.com/liuch/dmarc-srg
9 *
10 * This program is free software: you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the Free
12 * Software Foundation, either version 3 of the License.
13 *
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 * more details.
18 *
19 * You should have received a copy of the GNU General Public License along with
20 * this program. If not, see <http://www.gnu.org/licenses/>.
21 *
22 * =========================
23 *
24 * This file contains the class Config
25 *
26 * @category API
27 * @package DmarcSrg
28 * @author Aleksey Andreev (liuch)
29 * @license https://www.gnu.org/licenses/gpl-3.0.html GNU/GPLv3
30 */
31
32 namespace Liuch\DmarcSrg;
33
34 use Liuch\DmarcSrg\Exception\LogicException;
35
36 /**
37 * This class is for storing configuration data to avoid using globas variables
38 */
39 class Config
40 {
41 private $data = [];
42
43 /**
44 * The constructor
45 *
46 * @param string $config_file A php config file to load.
47 */
48 public function __construct(string $config_file)
49 {
50 require($config_file);
51 foreach ([
52 'debug', 'database', 'mailboxes', 'directories', 'remote_filesystems',
53 'admin', 'users', 'mailer', 'fetcher', 'cleaner', 'custom_css'
54 ] as $key
55 ) {
56 $this->data[$key] = $$key ?? null;
57 }
58 }
59
60 /**
61 * Returns config value by its name
62 *
63 * @param string $name Setting name. Hierarchy supported via '/'
64 * @param mixed $default Value to be returned if the required config item is missing or null
65 *
66 * @return mixed
67 */
68 public function get(string $name, $default = null)
69 {
70 $nm_i = 0;
71 $path = explode('/', $name);
72 $data = $this->data;
73 do {
74 $key = $path[$nm_i++];
75 if (empty($key)) {
76 throw new LogicException('Incorrect setting name: ' .$name);
77 }
78 if (!isset($data[$key])) {
79 return $default;
80 }
81 $data = $data[$key];
82 if (!isset($path[$nm_i])) {
83 return $data;
84 }
85 } while (gettype($data) === 'array');
86
87 return $default;
88 }
89 }