<?php
    abstract class Storage
    {
        protected $_file;
        protected $_data = array();

        public function __construct($file)
        {
            $this->_file = $file;
            if (file_exists($this->_file)) {
                $content = file_get_contents($this->_file);
                $this->_data = json_decode($content, true);
            }
        }

        public function toJson()
        {
            return json_encode($this->_data);
        }

        public function toArray()
        {
            return $this->_data;
        }

        public function get()
        {
            return $this;
        }

        public function save()
        {
            $content = json_encode($this->_data);
            file_put_contents($this->_file, $content);
        }

        abstract protected function refresh($data = array());
    }
    
    class Cache extends Storage
    {
        public function __construct($file)
        {
            parent::__construct($file);
        }

        public function refresh($data = array(), $new = false)
        {
            if ($new) $this->_data = array();

            foreach ($data as $controlName => $storage) {
                if (!is_array($storage))
                    continue;
                foreach ($storage as $file => $content) {
                    if ('[DELETED]' === $content) {
                        if (isset($this->_data[$controlName]) && isset($this->_data[$controlName][$file])){
                            unset($this->_data[$controlName][$file]);
                            if (count($this->_data[$controlName]) < 1)
                                unset($this->_data[$controlName]);
                            $t = 'test';
                        }
                    } else {
                        $this->_data[$controlName][$file] = $content;
                    }
                }
            }
        }
    }
    
    class Hash extends Storage
    {
        public function __construct($file)
        {
            parent::__construct($file);
        }

        public function refresh($data = array(), $new = false)
        {
            if ($new) $this->_data = array();

            foreach ($data as $file => $hash) {
                if ('[DELETED]' === $hash) {
                    if (isset($this->_data[$file]))
                        unset($this->_data[$file]);
                } else {
                    $this->_data[$file] = $hash;
                }
            }
        }
    }
    
    class PreviewDiffs extends Storage
    {
        public function __construct($file)
        {
            parent::__construct($file);
        }

        public function refresh($data = array())
        {
            if (!is_array($data)) return;

            $this->_data = $data;
        }
    }
?>