123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- <?php
- /**
- * Php-Gtk MySQL administrator - Main application data storage
- *
- * This is just an active wrapper around a plain INI file named the
- * same as the main program. INI information is made available as
- * a two-level array in the $properties member.
- *
- *
- * Configuration is loaded upon construction, and saved if changed
- * upon destruction. Changes are detected by a hash.
- *
- * @license CeCILL 2.0
- * @copyright 2008 Ouest Systemes Informatiques
- * @author Frederic G. MARAND
- * @version $Id: Pgma_Model.php,v 1.2 2008-05-25 20:31:40 cvs Exp $
- */
- class Pgma_Model
- {
- /**
- * Program properties. This is an array of sections and value
- * from the <program>.ini file
- * Sections include:
- * - Layout
- *
- * @var array
- */
- public $properties;
- /**
- * Sign properties to detect any change
- *
- * @var string
- */
- protected $hash;
- /**
- * Obtain the file name of the configuration
- * file, without the directory
- *
- * Why static ? Because it is instance-independent
- *
- * @return string
- */
- static protected function getConfigFileName()
- {
- /**
- * Why not just use basename ? Because in PHP-GTK
- * the PHP filename extension can be any .php*,
- */
- $name = $_SERVER['SCRIPT_NAME'];
- $name = pathinfo($name, PATHINFO_FILENAME);
- $name .= '.ini';
- return $name;
- }
-
- /**
- * Auto-load configuration
- *
- * @return void
- */
- function __construct()
- {
- $name = self::getConfigFileName();
- $this->properties = file_exists($name) ? parse_ini_file($name, TRUE) : NULL;
- $this->hash = md5(var_export($this->properties, TRUE));
- }
-
- /**
- * Auto-save configuration
- *
- * @return void
- */
- function __destruct()
- {
- /**
- * No need to save if properties haven't been changed
- */
- if ($this->hash == md5(var_export($this->properties, TRUE)))
- {
- return;
- }
- else
- {
- echo "Saving application settings\n";
- }
-
- $name = self::getConfigFileName();
- $s = '';
- foreach ($this->properties as $sectionKey => $sectionValue)
- {
- $s .= "[$sectionKey]" . PHP_EOL;
- foreach ($sectionValue as $key => $value)
- {
- $s .= $key . ' = ';
- if (is_bool($value))
- {
- $s .= $value ? 'TRUE' : 'FALSE';
- }
- elseif (is_numeric($value))
- {
- $s .= $value;
- }
- elseif (is_string($value))
- {
- $s .= '"' . $value . '"';
- }
- else
- {
- die('Unsupported property type for '
- . "$sectionKey/$key: " . gettype($value)
- );
- }
- $s .= PHP_EOL;
- }
- }
- file_put_contents($name, $s);
- }
- }
|