1: <?php
2:
3: namespace HZSystem\Core;
4:
5: /*
6: * Copyright (C) 2016 Luca
7: *
8: * This program is free software: you can redistribute it and/or modify
9: * it under the terms of the GNU Affero General Public License as published by
10: * the Free Software Foundation, either version 3 of the License, or
11: * (at your option) any later version.
12: *
13: * This program is distributed in the hope that it will be useful,
14: * but WITHOUT ANY WARRANTY; without even the implied warranty of
15: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16: * GNU Affero General Public License for more details.
17: *
18: * You should have received a copy of the GNU Affero General Public License
19: * along with this program. If not, see <http://www.gnu.org/licenses/agpl-3.0.html>.
20: */
21:
22: /**
23: * Gestore della configurazione. Il file di configurazione deve essere di tipo JSon
24: *
25: * @author Luca Liscio <hzkight@h0model.org>
26: * @author Marco Lettieri
27: * @version v 1.1 2016/09/26 10:56:20
28: * @copyright ©2016 Luca Liscio
29: * @copyright ©2013 Luca Liscio & Marco Lettieri
30: * @license http://www.gnu.org/licenses/agpl-3.0.html GNU/AGPL3
31: *
32: * @package hzSystem
33: * @subpackage Core
34: * @filesource
35: */
36: class HZConfig{
37: private $cfg;
38: private $cfgfile;
39:
40: /**
41: * Costruttore della classe Config, prende in input il path del file di configurazione
42: * e ne carica il contenuto. Il file deve essere in formato json.
43: *
44: * @param string $cfile path del file di configurazione
45: * @throws ConfigException
46: */
47: public function __construct($cfile){
48: $json;
49:
50: if (!file_exists($cfile)){
51: throw new ConfigException(CFG_FILE_NOT_EXIST,103);
52: } else if (($this->cfg=json_decode(file_get_contents($cfile), true))==null){
53: throw new ConfigException(CFG_FILE_CORRUPTED,113);
54: }
55:
56: $this->cfgfile = $cfile;
57: }
58:
59: /**
60: * Restituisce l'intera configurazione
61: *
62: * @return array
63: */
64: public function get_cfg(){
65: return $this->cfg;
66: }
67:
68: /**
69: * Restituisce il contenuto di una voce della configuarazione
70: *
71: * @param string $param nome del parametro
72: * @return mixed
73: */
74: public function get_param($param){
75: return $this->cfg[$param];
76: }
77:
78:
79:
80: /**
81: * Aggiorna il valore di una voce della configurazione
82: *
83: *
84: */
85: public function set_param(){
86: $numArgs = func_num_args() ;
87: $args = func_get_args() ;
88: call_user_func_array( array(&$this, 'set_param'.$numArgs), $args ) ;
89: }
90:
91: private function set_param2($param,$val){
92: if (array_key_exists($param, $this->cfg)){
93: $this->cfg[$param] = $val;
94: $this->save_cfg();
95: }
96: }
97:
98: private function set_param3($section,$param,$val){
99: if (array_key_exists($section, $this->cfg)){
100: if(array_key_exists($param, $this->cfg[$section])){
101: $this->cfg[$section][$param] = $val;
102: }
103: $this->save_cfg();
104: }
105: }
106:
107: private function save_cfg(){
108: $status = file_put_contents($this->cfgfile, json_encode($this->cfg));
109: if(!$status)throw new ConfigException(CFG_FILE_NOTWRITABLE,123);
110: }
111: }
112: ?>