1: <?php
2:
3: /**
4: * Project sberp
5: *
6: * @file Parser
7: * @author Jaromír Polášek
8: * @version 0.5.1
9: * Encoding UTF-8
10: */
11:
12: namespace App\Lib\Files;
13:
14: use Nette\Object,
15: App\Lib\Files;
16:
17: /**
18: * @deprecated since version 0.6.5.ACL
19: * Třída pro parsovánií ini souboru. Vytváří stromovou strukturu ve tvaru
20: * $var['Soubor']['Sekce']['klíč']
21: * Je li vytvořena, není potřeba ji vytvářet znovu. stačí přidávat další soubory
22: * Zdroj: http://php.net/manual/en/function.parse-ini-file.php
23: */
24: class Parser extends Object {
25:
26: /* const EXP a DIR přípona souboru a výchozí složka */
27: const EXP = '.ini',
28: DIR = '../';
29:
30: /** jméno parsovaného souboru. Default '*'
31: * var $name */
32: private $name;
33:
34: /** cesta k parsovanému souboru(souborům). Default '../'
35: * var $dir */
36: private $dir;
37:
38: /** pole zdrojů (souborů)
39: * var $source */
40: private $source;
41:
42: /** pole proměných v parsovaných souborech
43: * var $setup */
44: private $setup = array();
45:
46: /** True, pokud se soubor úspěšně sparsuje
47: * var $state */
48: private $state;
49:
50: /* Public */
51:
52: /**
53: * Konstruktor. vstupem je zdroj,cesta a název ini a souboru. Pokud není zadán systém
54: * hledájí se soubory v kořenovém adresáři modulu
55: * @param string $dir (optional)
56: * @param string $file (optional)
57: */
58: public function __construct($dir = null, $file = null) {
59: $this->setParams($dir,$file);
60: $this->source = (new Files($this->dir,$this->name.self::EXP))->source;
61: if ($this->getState()){
62: $this->parseArray();
63: }
64: }
65:
66: /**
67: * Vrací obsah parsovaných souborů
68: * @return array
69: */
70: public function getContent() {
71: return $this->setup;
72: }
73:
74:
75: /**
76: * nastaví obsah parsovaných souborů
77: * @param string $dir
78: * @param string $file
79: */
80: public function setContent($dir = null, $file = null) {
81: $this->params($dir,$file);
82: (!$this->state)? :$this->source->source($dir,$this->name);
83: $this->parseArray();
84: }
85:
86: /* Ukládání konfiguračních souborů*/
87:
88: /**
89: * Uloží všechny načtené kongigurační soubory
90: * @return boolean
91: */
92: public function saveAll(){
93: $res = 0;
94: foreach ($this->source as $name) {
95: $res +=$this->save($name);
96: }
97: return ($res == 0)?true:false;
98: }
99:
100: /**
101: * Uloží vybraný konfigutační soubor
102: * @param string $name
103: * @return boolean
104: */
105: public function save($name){
106: return $this->source->save($name,$this->setup[$name]);
107: }
108:
109: /* Práce s parametry */
110:
111: /**
112: * Vrací pole parametrů souboru $name
113: * @param string $name
114: * @return string
115: */
116: public function getFile($name) {
117: return $this->setup[$name];
118: }
119:
120: /**
121: * Vrací pole parametrů sekce $name
122: * @param string $name
123: * @param string $key
124: * @return array or string
125: */
126: public function getSection($name, $key) {
127: return $this->setup[$name][$key];
128: }
129:
130: /**
131: * Vrací hodnotu ze souboru $name.ini, sekce $section a klíče $key
132: * @param string $name
133: * @param string $section
134: * @param string $key
135: * @return $value or false
136: */
137: public function getValue($name, $section, $key) {
138: return (isset($this->setup[$name][$section])) ? ($this->setup[$name][$section][$key]) : false;
139: }
140:
141: /**
142: * Nastaví hodnoty $array pro soubor $name (neuloží je)
143: * @param string $name
144: * @param array $array
145: */
146: public function setFile($name, $array) {
147: (is_array($array)) ? ($this->setup[$name] = $array) : false;
148: }
149:
150: /**
151: * Nastaví hodnoty $array pro soubor $name a sekci $section (neuloží je)
152: * @param string $name
153: * @param string $section
154: * @param array $array
155: */
156: public function setSection($name, $section, $array) {
157: (is_array($array)) ? ($this->setup[$name][$section] = $array) : false;
158: }
159:
160: /**
161: * Nastaví hodnotu $value pro soubor $name a sekci $section a klíč $key (neuloží je)
162: * @param string $name
163: * @param string $section
164: * @param string $key
165: * @param any $value
166: */
167: public function setValue($name, $section, $key, $value) {
168: ($this->setup[$name][$section][$key] = $value) ? true : false;
169: }
170:
171: /**
172: * Nastaví požadované nebo výhozí parametry pro vyhledávání souborů, pokud nejsou udány.
173: * @param string $dir
174: * @param string $file
175: */
176: public function setParams($dir = null, $file = null){
177: (!is_null($dir)) ? $this->dir = $dir : $this->dir = self::DIR;
178: (!is_null($file)) ? $this->name = $file : $this->name = '*';
179: }
180:
181: /* Protected */
182:
183: /**
184: * Nastavuje state na true:false a vrací jeho hodnotu
185: * @return bool
186: */
187: protected function getState(){
188: return is_null($this->source)? $this->state = false:$this->state = true;
189: }
190:
191:
192: /**
193: * Přidá do pole "setup" obsahy z parsovaných souborů pod klíči $name
194: */
195: protected function parseArray() {
196: foreach ($this->source as $name => $file) {
197: $this->parse($name, $file);
198: }
199: }
200:
201: /**
202: * Přidá do pole hodnot obsah parsovaného souboru $file pod klíčem $name
203: * @param string $name
204: * @param \SplFileInfo $file
205: */
206: protected function parse($name, $file){
207: if ($file->isReadable()) {
208: $this->setup[$file->getBasename(self::EXP)] = parse_ini_file($name, true);
209: }
210: }
211: }
212: