jig.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. /*
  3. Copyright (c) 2009-2017 F3::Factory/Bong Cosca, All rights reserved.
  4. This file is part of the Fat-Free Framework (http://fatfreeframework.com).
  5. This is free software: you can redistribute it and/or modify it under the
  6. terms of the GNU General Public License as published by the Free Software
  7. Foundation, either version 3 of the License, or later.
  8. Fat-Free Framework is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License along
  13. with Fat-Free Framework. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. namespace DB;
  16. //! In-memory/flat-file DB wrapper
  17. class Jig {
  18. //@{ Storage formats
  19. const
  20. FORMAT_JSON=0,
  21. FORMAT_Serialized=1;
  22. //@}
  23. protected
  24. //! UUID
  25. $uuid,
  26. //! Storage location
  27. $dir,
  28. //! Current storage format
  29. $format,
  30. //! Jig log
  31. $log,
  32. //! Memory-held data
  33. $data,
  34. //! lazy load/save files
  35. $lazy;
  36. /**
  37. * Read data from memory/file
  38. * @return array
  39. * @param $file string
  40. **/
  41. function &read($file) {
  42. if (!$this->dir || !is_file($dst=$this->dir.$file)) {
  43. if (!isset($this->data[$file]))
  44. $this->data[$file]=[];
  45. return $this->data[$file];
  46. }
  47. if ($this->lazy && isset($this->data[$file]))
  48. return $this->data[$file];
  49. $fw=\Base::instance();
  50. $raw=$fw->read($dst);
  51. switch ($this->format) {
  52. case self::FORMAT_JSON:
  53. $data=json_decode($raw,TRUE);
  54. break;
  55. case self::FORMAT_Serialized:
  56. $data=$fw->unserialize($raw);
  57. break;
  58. }
  59. $this->data[$file] = $data;
  60. return $this->data[$file];
  61. }
  62. /**
  63. * Write data to memory/file
  64. * @return int
  65. * @param $file string
  66. * @param $data array
  67. **/
  68. function write($file,array $data=NULL) {
  69. if (!$this->dir || $this->lazy)
  70. return count($this->data[$file]=$data);
  71. $fw=\Base::instance();
  72. switch ($this->format) {
  73. case self::FORMAT_JSON:
  74. $out=json_encode($data,JSON_PRETTY_PRINT);
  75. break;
  76. case self::FORMAT_Serialized:
  77. $out=$fw->serialize($data);
  78. break;
  79. }
  80. return $fw->write($this->dir.'/'.$file,$out);
  81. }
  82. /**
  83. * Return directory
  84. * @return string
  85. **/
  86. function dir() {
  87. return $this->dir;
  88. }
  89. /**
  90. * Return UUID
  91. * @return string
  92. **/
  93. function uuid() {
  94. return $this->uuid;
  95. }
  96. /**
  97. * Return profiler results (or disable logging)
  98. * @param $flag bool
  99. * @return string
  100. **/
  101. function log($flag=TRUE) {
  102. if ($flag)
  103. return $this->log;
  104. $this->log=FALSE;
  105. }
  106. /**
  107. * Jot down log entry
  108. * @return NULL
  109. * @param $frame string
  110. **/
  111. function jot($frame) {
  112. if ($frame)
  113. $this->log.=date('r').' '.$frame.PHP_EOL;
  114. }
  115. /**
  116. * Clean storage
  117. * @return NULL
  118. **/
  119. function drop() {
  120. if ($this->lazy) // intentional
  121. $this->data=[];
  122. if (!$this->dir)
  123. $this->data=[];
  124. elseif ($glob=@glob($this->dir.'/*',GLOB_NOSORT))
  125. foreach ($glob as $file)
  126. @unlink($file);
  127. }
  128. //! Prohibit cloning
  129. private function __clone() {
  130. }
  131. /**
  132. * Instantiate class
  133. * @param $dir string
  134. * @param $format int
  135. **/
  136. function __construct($dir=NULL,$format=self::FORMAT_JSON,$lazy=FALSE) {
  137. if ($dir && !is_dir($dir))
  138. mkdir($dir,\Base::MODE,TRUE);
  139. $this->uuid=\Base::instance()->hash($this->dir=$dir);
  140. $this->format=$format;
  141. $this->lazy=$lazy;
  142. }
  143. /**
  144. * save file on destruction
  145. **/
  146. function __destruct() {
  147. if ($this->lazy) {
  148. $this->lazy = FALSE;
  149. foreach ($this->data?:[] as $file => $data)
  150. $this->write($file,$data);
  151. }
  152. }
  153. }