
<?php /** * (c) Copyright by Sławek 'Levabul' Pruchnik * * @author Sławek 'Levabul' Pruchnik * @copyright Sławek 'Levabul' Pruchnik 2006 * @license <a href="http://creativecommons.org/licenses/by-nc-sa/2.0/" target="_blank">http://creativecommons.org/licenses/by-nc-sa/2.0/</a> * @version 0.9 */ class IniParser { /** * Przechowuje sciezke do pliku * * @var string * @access private */ private $handle; /** * Przechowuje sparsowany plik ini * * @var array * @access private */ private $ini; /** * Okresla czy plik podczas wykonywania metody __destruct() ma zostac zapisany * * @var bool * @access private */ private $save = 0; /** * Okresla czy podczas wykonywania metody __destruct() ma zostac utworzony cache pliku * * @var bool * @access private */ private $cache = 0; /** * Parsuje plik ini * jezeli plik nieistnieje tworzy go * * @param string $handle * @return void * @access public */ public function __construct ($handle) { $this -> handle = $handle; if ((file_exists ($handle.'.cache') && filectime ($handle) > filectime ($handle.'.cache')) || !file_exists ($handle.'.cache')) { $this -> cache = 1; } } else } /** * Sprawdza czy istanieje dana opcja * * @param string $name * @param string $section (optional) okresla w jakiej sekcji ma sprawdzac czy znajduje sie opcja * @return bool * @access public */ public function optionExists ($name, $section = 0) { if ($section) else { foreach ($this -> ini as $key => $value) { } } } /** * Pobiera pojedyncza opcje * Jezeli nie zostal podany parametr $section funkcja zwroci pierwsza napotkana o
pcje o nazwie $name * * @param string $name * @param string $section (optional) okresla w jakiej sekcji znajduje sie opcja * @return mixed * @access public */ public function getOption ($name, $section = 0) { if ($this -> optionExists ($name, $section)) { return $this -> ini [$section][$name]; else { foreach ($this -> ini as $key => $value) { return $value[$name]; } } } else return false; } /** * Dodaje (lub edytuje) opcję * * @param string $name * @param string $value * @param string $section (optional) okresla w jakiej sekcji znajduje sie opcja * @return void * @access public */ public function setOption ($name, $value, $section = '') { $this -> save = 1; $this -> ini [$section][$name] = $value; } /** * Destructor klasy * Jeżeli nastapila zmiana jakiejs opcji zapisuje opcje do pliku $this -> handle * Jeżeli istnieje taka potrzeba tworzy cache pliku $this -> handle * * @return void * @access public */ public function __destruct () { if ($this -> save) { foreach ($this -> ini as $section_name => $section) { $content .= '['.$section_name."]n"; foreach ($section as $option_name => $option) $content .= $option_name.' = '.$option."n"; } file_put_contents ($this -> handle, $content); } elseif ($this -> cache) } } ?>
PS. Mój 100-ny post
