/registry/native.php
PHP | 214 lines | 82 code | 58 blank | 74 comment | 13 complexity | faa10424720fc9cef1704d0494d3c29e MD5 | raw file
- <?php
- /**
- * SPF_Registry_Native
- * @package spf
- * @author Simon Downes <simon@simondownes.co.uk>
- * @copyright Copyright (c) 2007, Simon Downes
- * @license http://www.opensource.org/licenses/mit-license.php
- */
-
- /**
- * Native driver for the registry.
- * Stores data in SPF_APP_DIR/registry.conf in the format 'var = value'.
- * One line per item, leading and trailing whitespace is trimmed.
- * Lines beginning with # are ignored.
- *
- * @final
- * @package spf
- * @author Simon Downes <simon@simondownes.co.uk>
- * @copyright Copyright (c) 2007, Simon Downes
- * @license http://www.opensource.org/licenses/mit-license.php
- */
- final class SPF_Registry_Native extends SPF_Registry {
-
- // path and filename of the registry data file.
- private $data_file = '';
-
- // stores the initial contents of the data file.
- private $data_file_contents = array();
-
- // will be set to true if any of the items have their value modified.
- private $dirty = false;
-
- /**
- * Constructor.
- *
- * @return void
- */
- public function __construct() {
-
- parent::__construct();
-
- $this->data_file = SPF_APP_DIR. '/registry.conf';
-
- if( !file_exists($this->data_file) )
- throw new SPF_Fatal_Exception("Registry Initialisation Error: Data file '{$this->data_file}' does not exist!");
-
- if( !is_writable($this->data_file) )
- throw new SPF_Fatal_Exception("Registry Initialisation Error: Data file '{$this->data_file}' is not writable!");
-
- $data = file($this->data_file);
-
- foreach( $data as $line ) {
-
- $line = trim($line);
-
- if( !$line || substr($line, 0, 1) == '#' ) {
- $this->data_file_contents[] = $line;
- continue;
- }
-
- $pos = strpos($line, '=');
-
- if( $pos === false ) {
- $k = $line;
- $v = '';
- }
- else {
- $k = trim(substr($line, 0, $pos));
- $v = trim(substr($line, $pos + 1));
- }
-
- $this->items[$k] = $v;
-
- $this->data_file_contents[] = "$k = $v";
-
- } // each $data
-
- } // __construct
-
- /**
- * Destructor.
- *
- * @return void
- */
- public function __destruct() {
-
- // ensure any changes are written to the data file
- $this->write_config_file();
-
- parent::__destruct();
-
- } // __destruct
-
- /**
- * Determines if a the specified variable exists in the registry.
- *
- * @param string $var_name the name of the variable to look for.
- * @return boolean
- */
- public function exists( $var_name ) {
-
- return array_key_exists($var_name, $this->items);
-
- } // exists
-
- /**
- * Retrieves the value of a variable from the registry.
- *
- * @param string $var_name the name of the variable.
- * @param mixed $default the default value to return if the variable doesn't exist.
- * @return mixed the value from the registry or the specified default value if the variable doesn't exist.
- */
- public function get( $var_name, $default = '' ) {
-
- return ( $this->exists($var_name) ) ? $this->items[$var_name] : $default;
-
- } // get
-
- /**
- * Returns the entire contents of the registry in an array.
- *
- * @return array all the entries in the registry.
- */
- public function get_all() {
-
- return $this->items;
-
- } // get_all
-
- /**
- * Sets the value of variable in the registry.
- *
- * @param string $var_name the name of the variable.
- * @param mixed $val the value of the variable.
- * @return boolean true if the variable was stored successfully; false otherwise.
- */
- public function set( $var_name, $val ) {
-
- if( !$var_name )
- return false;
-
- $this->items[$var_name] = $val;
-
- $this->dirty = true;
-
- return true;
-
- } // set
-
- /**
- * Removes a variable from the registry.
- *
- * @param string $var_name the name of the variable.
- * @return boolean true if the variable was removed successfully; false otherwise.
- */
- public function delete( $var_name ) {
-
- unset($this->items[$var_name]);
-
- $this->dirty = true;
-
- return true;
-
- } // delete
-
- /**
- * Removes a variable from the registry.
- *
- * @param string $var_name the name of the variable.
- * @return boolean true if the variable was removed successfully; false otherwise.
- */
- public function write_config_file() {
-
- if( !$this->dirty )
- return true;
-
- $file = new SPF_File($this->data_file, SPF_File::MODE_WRITE);
-
- $file->truncate();
-
- // temp copy of data so we can unset items as they are written to the file
- $items = $this->items;
-
- foreach( $this->data_file_contents as $line ) {
-
- if( !$line || substr($line, 0, 1) == '#' ) {
- $file->write_line($line);
- continue;
- }
-
- // find the key for this item
- $pos = strpos($line, '=');
- $k = ($pos === false) ? $line : trim(substr($line, 0, $pos));
-
- // if key exists then output current value
- if( isset($items[$k]) ) {
- $file->write_line("$k = {$items[$k]}");
- unset($items[$k]);
- }
-
- }
-
- // items still remaining the the array are new items and should be appended to the end of the file
- foreach( $items as $k => $v ) {
- $file->write_line("$k = $v");
- }
-
- $this->dirty = false;
-
- }
-
- } // SPF_Registry_Native
-
- ?>