jig.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. /*
  3. Copyright (c) 2009-2014 F3::Factory/Bong Cosca, All rights reserved.
  4. This file is part of the Fat-Free Framework (http://fatfree.sf.net).
  5. THE SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF
  6. ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  7. IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
  8. PURPOSE.
  9. Please see the license.txt file for more information.
  10. */
  11. namespace DB;
  12. //! Flat-file DB wrapper
  13. class Jig {
  14. //@{ Storage formats
  15. const
  16. FORMAT_JSON=0,
  17. FORMAT_Serialized=1;
  18. //@}
  19. protected
  20. //! UUID
  21. $uuid,
  22. //! Storage location
  23. $dir,
  24. //! Current storage format
  25. $format,
  26. //! Jig log
  27. $log;
  28. /**
  29. * Read data from file
  30. * @return array
  31. * @param $file string
  32. **/
  33. function read($file) {
  34. $fw=\Base::instance();
  35. if (!is_file($dst=$this->dir.$file))
  36. return array();
  37. $raw=$fw->read($dst);
  38. switch ($this->format) {
  39. case self::FORMAT_JSON:
  40. $data=json_decode($raw,TRUE);
  41. break;
  42. case self::FORMAT_Serialized:
  43. $data=$fw->unserialize($raw);
  44. break;
  45. }
  46. return $data;
  47. }
  48. /**
  49. * Write data to file
  50. * @return int
  51. * @param $file string
  52. * @param $data array
  53. **/
  54. function write($file,array $data=NULL) {
  55. $fw=\Base::instance();
  56. switch ($this->format) {
  57. case self::FORMAT_JSON:
  58. $out=json_encode($data,@constant('JSON_PRETTY_PRINT'));
  59. break;
  60. case self::FORMAT_Serialized:
  61. $out=$fw->serialize($data);
  62. break;
  63. }
  64. return $fw->write($this->dir.$file,$out);
  65. }
  66. /**
  67. * Return directory
  68. * @return string
  69. **/
  70. function dir() {
  71. return $this->dir;
  72. }
  73. /**
  74. * Return UUID
  75. * @return string
  76. **/
  77. function uuid() {
  78. return $this->uuid;
  79. }
  80. /**
  81. * Return SQL profiler results
  82. * @return string
  83. **/
  84. function log() {
  85. return $this->log;
  86. }
  87. /**
  88. * Jot down log entry
  89. * @return NULL
  90. * @param $frame string
  91. **/
  92. function jot($frame) {
  93. if ($frame)
  94. $this->log.=date('r').' '.$frame.PHP_EOL;
  95. }
  96. /**
  97. * Clean storage
  98. * @return NULL
  99. **/
  100. function drop() {
  101. if ($glob=@glob($this->dir.'/*',GLOB_NOSORT))
  102. foreach ($glob as $file)
  103. @unlink($file);
  104. }
  105. /**
  106. * Instantiate class
  107. * @param $dir string
  108. * @param $format int
  109. **/
  110. function __construct($dir,$format=self::FORMAT_JSON) {
  111. if (!is_dir($dir))
  112. mkdir($dir,\Base::MODE,TRUE);
  113. $this->uuid=\Base::instance()->hash($this->dir=$dir);
  114. $this->format=$format;
  115. }
  116. }