PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/registry/native.php

http://spf-php.googlecode.com/
PHP | 214 lines | 82 code | 58 blank | 74 comment | 13 complexity | faa10424720fc9cef1704d0494d3c29e MD5 | raw file
  1. <?php
  2. /**
  3. * SPF_Registry_Native
  4. * @package spf
  5. * @author Simon Downes <simon@simondownes.co.uk>
  6. * @copyright Copyright (c) 2007, Simon Downes
  7. * @license http://www.opensource.org/licenses/mit-license.php
  8. */
  9. /**
  10. * Native driver for the registry.
  11. * Stores data in SPF_APP_DIR/registry.conf in the format 'var = value'.
  12. * One line per item, leading and trailing whitespace is trimmed.
  13. * Lines beginning with # are ignored.
  14. *
  15. * @final
  16. * @package spf
  17. * @author Simon Downes <simon@simondownes.co.uk>
  18. * @copyright Copyright (c) 2007, Simon Downes
  19. * @license http://www.opensource.org/licenses/mit-license.php
  20. */
  21. final class SPF_Registry_Native extends SPF_Registry {
  22. // path and filename of the registry data file.
  23. private $data_file = '';
  24. // stores the initial contents of the data file.
  25. private $data_file_contents = array();
  26. // will be set to true if any of the items have their value modified.
  27. private $dirty = false;
  28. /**
  29. * Constructor.
  30. *
  31. * @return void
  32. */
  33. public function __construct() {
  34. parent::__construct();
  35. $this->data_file = SPF_APP_DIR. '/registry.conf';
  36. if( !file_exists($this->data_file) )
  37. throw new SPF_Fatal_Exception("Registry Initialisation Error: Data file '{$this->data_file}' does not exist!");
  38. if( !is_writable($this->data_file) )
  39. throw new SPF_Fatal_Exception("Registry Initialisation Error: Data file '{$this->data_file}' is not writable!");
  40. $data = file($this->data_file);
  41. foreach( $data as $line ) {
  42. $line = trim($line);
  43. if( !$line || substr($line, 0, 1) == '#' ) {
  44. $this->data_file_contents[] = $line;
  45. continue;
  46. }
  47. $pos = strpos($line, '=');
  48. if( $pos === false ) {
  49. $k = $line;
  50. $v = '';
  51. }
  52. else {
  53. $k = trim(substr($line, 0, $pos));
  54. $v = trim(substr($line, $pos + 1));
  55. }
  56. $this->items[$k] = $v;
  57. $this->data_file_contents[] = "$k = $v";
  58. } // each $data
  59. } // __construct
  60. /**
  61. * Destructor.
  62. *
  63. * @return void
  64. */
  65. public function __destruct() {
  66. // ensure any changes are written to the data file
  67. $this->write_config_file();
  68. parent::__destruct();
  69. } // __destruct
  70. /**
  71. * Determines if a the specified variable exists in the registry.
  72. *
  73. * @param string $var_name the name of the variable to look for.
  74. * @return boolean
  75. */
  76. public function exists( $var_name ) {
  77. return array_key_exists($var_name, $this->items);
  78. } // exists
  79. /**
  80. * Retrieves the value of a variable from the registry.
  81. *
  82. * @param string $var_name the name of the variable.
  83. * @param mixed $default the default value to return if the variable doesn't exist.
  84. * @return mixed the value from the registry or the specified default value if the variable doesn't exist.
  85. */
  86. public function get( $var_name, $default = '' ) {
  87. return ( $this->exists($var_name) ) ? $this->items[$var_name] : $default;
  88. } // get
  89. /**
  90. * Returns the entire contents of the registry in an array.
  91. *
  92. * @return array all the entries in the registry.
  93. */
  94. public function get_all() {
  95. return $this->items;
  96. } // get_all
  97. /**
  98. * Sets the value of variable in the registry.
  99. *
  100. * @param string $var_name the name of the variable.
  101. * @param mixed $val the value of the variable.
  102. * @return boolean true if the variable was stored successfully; false otherwise.
  103. */
  104. public function set( $var_name, $val ) {
  105. if( !$var_name )
  106. return false;
  107. $this->items[$var_name] = $val;
  108. $this->dirty = true;
  109. return true;
  110. } // set
  111. /**
  112. * Removes a variable from the registry.
  113. *
  114. * @param string $var_name the name of the variable.
  115. * @return boolean true if the variable was removed successfully; false otherwise.
  116. */
  117. public function delete( $var_name ) {
  118. unset($this->items[$var_name]);
  119. $this->dirty = true;
  120. return true;
  121. } // delete
  122. /**
  123. * Removes a variable from the registry.
  124. *
  125. * @param string $var_name the name of the variable.
  126. * @return boolean true if the variable was removed successfully; false otherwise.
  127. */
  128. public function write_config_file() {
  129. if( !$this->dirty )
  130. return true;
  131. $file = new SPF_File($this->data_file, SPF_File::MODE_WRITE);
  132. $file->truncate();
  133. // temp copy of data so we can unset items as they are written to the file
  134. $items = $this->items;
  135. foreach( $this->data_file_contents as $line ) {
  136. if( !$line || substr($line, 0, 1) == '#' ) {
  137. $file->write_line($line);
  138. continue;
  139. }
  140. // find the key for this item
  141. $pos = strpos($line, '=');
  142. $k = ($pos === false) ? $line : trim(substr($line, 0, $pos));
  143. // if key exists then output current value
  144. if( isset($items[$k]) ) {
  145. $file->write_line("$k = {$items[$k]}");
  146. unset($items[$k]);
  147. }
  148. }
  149. // items still remaining the the array are new items and should be appended to the end of the file
  150. foreach( $items as $k => $v ) {
  151. $file->write_line("$k = $v");
  152. }
  153. $this->dirty = false;
  154. }
  155. } // SPF_Registry_Native
  156. ?>