     1: <?php
     2:
     3:     abstract class Syllable_Cache_FileAbstract implements Syllable_Cache_Interface {
     4:         private static $language    = null;
     5:         private static $path        = null;
     6:         private static $data        = null;
     7:
     8:         abstract protected function encode($array);
     9:         abstract protected function decode($array);
    10:         abstract protected function getFilename($language);
    11:
    12:         public function __construct($path) {
    13:             $this->setPath($path);
    14:         }
    15:
    16:         public function setPath($path) {
    17:             if ($path !== self::$path) {
    18:                 self::$path = $path;
    19:                 self::$data = null;
    20:             }
    21:         }
    22:
    23:         private function filename() {
    24:             return self::$path.'/'.$this->getFilename(self::$language);
    25:         }
    26:
    27:         public function open($language) {
    28:             $language = strtolower($language);
    29:
    30:             if (self::$language !== $language) {
    31:                 self::$language = $language;
    32:                 self::$data     = null;
    33:
    34:                 $file = $this->filename($language);
    35:                 if (is_file($file)) {
    36:                     self::$data = $this->decode(file_get_contents($file), true);
    37:                 }
    38:             }
    39:         }
    40:
    41:         public function close() {
    42:             $file = $this->filename();
    43:             file_put_contents($file, $this->encode(self::$data));
    44:             @chmod($file, 0777);
    45:         }
    46:
    47:         public function __set($key, $value) {
    48:             self::$data[$key] = $value;
    49:         }
    50:
    51:         public function __get($key) {
    52:             return self::$data[$key];
    53:         }
    54:
    55:         public function __isset($key) {
    56:             return isset(self::$data[$key]);
    57:         }
    58:
    59:         public function __unset($key) {
    60:             unset(self::$data[$key]);
    61:         }
    62:     }