Pgma_Model.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /**
  3. * Php-Gtk MySQL administrator - Main application data storage
  4. *
  5. * This is just an active wrapper around a plain INI file named the
  6. * same as the main program. INI information is made available as
  7. * a two-level array in the $properties member.
  8. *
  9. *
  10. * Configuration is loaded upon construction, and saved if changed
  11. * upon destruction. Changes are detected by a hash.
  12. *
  13. * @license CeCILL 2.0
  14. * @copyright 2008 Ouest Systemes Informatiques
  15. * @author Frederic G. MARAND
  16. * @version $Id: Pgma_Model.php,v 1.2 2008-05-25 20:31:40 cvs Exp $
  17. */
  18. class Pgma_Model
  19. {
  20. /**
  21. * Program properties. This is an array of sections and value
  22. * from the <program>.ini file
  23. * Sections include:
  24. * - Layout
  25. *
  26. * @var array
  27. */
  28. public $properties;
  29. /**
  30. * Sign properties to detect any change
  31. *
  32. * @var string
  33. */
  34. protected $hash;
  35. /**
  36. * Obtain the file name of the configuration
  37. * file, without the directory
  38. *
  39. * Why static ? Because it is instance-independent
  40. *
  41. * @return string
  42. */
  43. static protected function getConfigFileName()
  44. {
  45. /**
  46. * Why not just use basename ? Because in PHP-GTK
  47. * the PHP filename extension can be any .php*,
  48. */
  49. $name = $_SERVER['SCRIPT_NAME'];
  50. $name = pathinfo($name, PATHINFO_FILENAME);
  51. $name .= '.ini';
  52. return $name;
  53. }
  54. /**
  55. * Auto-load configuration
  56. *
  57. * @return void
  58. */
  59. function __construct()
  60. {
  61. $name = self::getConfigFileName();
  62. $this->properties = file_exists($name) ? parse_ini_file($name, TRUE) : NULL;
  63. $this->hash = md5(var_export($this->properties, TRUE));
  64. }
  65. /**
  66. * Auto-save configuration
  67. *
  68. * @return void
  69. */
  70. function __destruct()
  71. {
  72. /**
  73. * No need to save if properties haven't been changed
  74. */
  75. if ($this->hash == md5(var_export($this->properties, TRUE)))
  76. {
  77. return;
  78. }
  79. else
  80. {
  81. echo "Saving application settings\n";
  82. }
  83. $name = self::getConfigFileName();
  84. $s = '';
  85. foreach ($this->properties as $sectionKey => $sectionValue)
  86. {
  87. $s .= "[$sectionKey]" . PHP_EOL;
  88. foreach ($sectionValue as $key => $value)
  89. {
  90. $s .= $key . ' = ';
  91. if (is_bool($value))
  92. {
  93. $s .= $value ? 'TRUE' : 'FALSE';
  94. }
  95. elseif (is_numeric($value))
  96. {
  97. $s .= $value;
  98. }
  99. elseif (is_string($value))
  100. {
  101. $s .= '"' . $value . '"';
  102. }
  103. else
  104. {
  105. die('Unsupported property type for '
  106. . "$sectionKey/$key: " . gettype($value)
  107. );
  108. }
  109. $s .= PHP_EOL;
  110. }
  111. }
  112. file_put_contents($name, $s);
  113. }
  114. }