Debian Codegraph @

util/docscripts/lib/generator/hash/Freezer.php

raw symbols SCIP
1 <?php
2
3 final class Freezer
4 {
5 protected $key_delimeter = '%%%';
6
7 private $length = 99999;
8 private $nodes_location = '';
9 private $nodes = array();
10
11 public static function clean($directory, $suffix) {
12 $loc = $directory . '/' . $suffix;
13 file_exists($loc) && unlink($loc);
14 }
15
16 public function __construct($directory, $suffix) {
17 $this->nodes_location = $directory . '/' . $suffix;
18 touch($this->nodes_location);
19 $nodes_file = fopen($this->nodes_location, 'r');
20 $this->_readFromFile($nodes_file);
21 fclose($nodes_file);
22 }
23
24 public function __destruct() {
25 $this->flush();
26 }
27
28 private function _readFromFile($nodes_file) {
29 while (!feof($nodes_file)) {
30 $line = stream_get_line($nodes_file, $this->length, "\n");
31 list($key, $value) = explode($this->key_delimeter, $line);
32
33 if (trim($key)) {
34 $this->nodes[$key] = $value;
35 }
36 }
37 }
38
39 public function ids($flush = TRUE) {
40 return array_keys($this->nodes);
41 }
42
43 public function open($key, $default) {
44 if(array_key_exists($key, $this->nodes)){
45 return unserialize(str_replace("\\n", "\n", $this->nodes[$key]));
46 }
47 return $default;
48 }
49
50 public function save($key, $content) {
51 $this->nodes[$key] = str_replace("\n", "\\n", serialize($content));
52 }
53
54 private function flush() {
55 $tmp = fopen($this->nodes_location . '_tmp', 'w');
56 foreach ($this->nodes as $key => $value) {
57 fwrite($tmp, $key . $this->key_delimeter . $value . "\n");
58 }
59 fclose($tmp);
60 unlink($this->nodes_location);
61 rename($this->nodes_location . '_tmp', $this->nodes_location);
62 }
63 }