Location: PHPKode > projects > ATUIN > atuin/app/models/dbapi/DBTree.php
<?php

class DBTree {          


   private $root;
   private $file;
   private $serialized;
   private $elementList;
   
   function __construct( $file) {
      $this->root = array( );
      $this->file = $file;
      $this->loadFromFile( );
   }
   
   /* =====================================
      public functions
    ===================================== */

   public function __set( $name, $value) {
      switch( $name) {
         case 'root':
            $this->root = $value;
            break;
         case 'file':
            $this->file = $value;
            break;
         case 'serialized':
            $this->serialized = $value;
            break;
         case 'elementList':
            $this->elementList = $value;
            break;
      }
   }
   
   public function __get( $name) {
      switch( $name) {
         case 'root':
            return $this->root;
         case 'file':
            return $this->file;
         case 'serialized':
            return $this->serialized;
         case 'elementList':
            return $this->elementList;
      }
   }
   
   public function addKey( $key, $value) {
      $this->add_rek( $this->root, $key, $value);
      $this->saveToFile( );
   }

   private function add_rek( &$node, $key, &$value) {
      if( strlen( $key)>0) {
         $ch = substr( $key, 0, 1);
         if( !isset( $node[$ch])) {
            $node[$ch] = array( $ch => array( ));
         }
         $restkey = substr( $key, 1, strlen( $key)-1);
         if( strlen( $restkey)>0) {
            $this->add_rek( $node[$ch], $restkey, $value);
         }
         else {
            if( isset( $node[$ch]['value'])) {
               $node[$ch]['value'] = $value;
            }
            else {
               $node[$ch] = array_merge( $node[$ch], array( 'value' => $value));
            }
         }
      }
   }
   
   public function getValue( $key) {
      $arr = preg_split( '//', $key);
      $run = &$this->root;
      foreach( $arr as $ch) {
         if( strlen( $ch)>0) {
            if( !isset( $run[$ch])) {
               return false;
            }
            $run = &$run[$ch];
         }
      }
      if( isset( $run['value'])) {
         return $run['value'];
      }
      return false;
   }
      
   public function removeKey( $key) {
      $arr = preg_split( '//', $key);
      $run = &$this->root;
      foreach( $arr as $ch) {
         if( strlen( $ch)>0) {
            if( !isset( $run[$ch]))
               return;
            $run = &$run[$ch];
         }
      }
      if( isset( $run['value'])) {
         unset( $run['value']);
         $this->saveToFile( );
      }
   }

   public function getAllKeys( ) {
      $this->elementList = array( );
      $this->generateElementList( $this->root, "");
      return $this->elementList;
   }

   /* =====================================
      private functions
    ===================================== */
   
   private function loadFromFile( ) {
      if( file_exists( $this->file)===false)
         return;      
      $str = file_get_contents( $this->file);
      $arr = array( );
      for( $i = 0; $i<strlen( $str); $i++) {
         array_push( $arr, substr( $str, $i, 1));
      }

      $value = '';
      $flag = 0;
      $d = 0;
      $stack = array( $d => &$this->root);
      
      foreach( $arr as $ch) {
         if( $flag==1) {
            if( strcmp( $ch, "\n")==0) {
               $stack[$d]['value'] = $value;
               $value = '';
               $flag = 0;
            }
            else {
               $value .= $ch;
            }
         }
         else if( strcmp( $ch, "\n")==0) {
            unset( $stack[$d]);
            $d--;
         }
         else if( strcmp( $ch, ":")==0) {
            $flag = 1;
         }
         else {
            $stack[$d] = array_merge( (array)$stack[$d], array( $ch => array( )));
            $stack[$d+1] = &$stack[$d][$ch];
            $d++;
         }
      }
   }
   
   private function saveToFile( ) {
      $this->serialized = '';
      $buf = '';
      $this->serialize( $this->root, $buf);
      $this->serialized .= "\n";
      file_put_contents( $this->file, $this->serialized);
   }

   private function serialize( &$node, &$buf) {
      if( strlen( $node['value'])>0) {
         $this->serialized .= $buf . ':' . $node['value'] . "\n";
         $buf = '';
      }
      $newbuf = '';
      foreach( $node as $ch => $arr) {
         if( strlen( $ch)>0) {
            if( strcmp( $ch, 'value')!=0) {
               $newbuf = $buf . $ch;
               $this->serialize( $arr, $newbuf);
               if( $newbuf=='')
                  $buf = ''; 
            }
         }
      }
      if( $buf=='')
         $this->serialized .= "\n";
   }

   private function generateElementList( &$node, $str) {
      if( isset( $node['value'])) {
         array_push( $this->elementList, $str);
      }
      foreach( $node as $ch => $arr) {
         if( strlen( $ch)>0) {
            if( strcmp( $ch, 'value')!=0) {
               $this->generateElementList( $arr, $str . $ch);
            }
         }
      }
   }   
};

?>
Return current item: ATUIN