Pgma_Model.php 2.4 KB

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