HumanReadableCache.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace Luracast\Restler;
  3. /**
  4. * Default Cache that writes/reads human readable files for caching purpose
  5. *
  6. * @category Framework
  7. * @package Restler
  8. * @author R.Arul Kumaran <arul@luracast.com>
  9. * @copyright 2010 Luracast
  10. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  11. * @link http://luracast.com/products/restler/
  12. *
  13. */
  14. class HumanReadableCache implements iCache
  15. {
  16. /**
  17. * @var string path of the folder to hold cache files
  18. */
  19. public static $cacheDir;
  20. public function __construct()
  21. {
  22. if (is_null(self::$cacheDir)) {
  23. self::$cacheDir = Defaults::$cacheDirectory;
  24. }
  25. }
  26. /**
  27. * store data in the cache
  28. *
  29. * @param string $name
  30. * @param mixed $data
  31. *
  32. * @throws \Exception
  33. * @return boolean true if successful
  34. */
  35. public function set($name, $data)
  36. {
  37. if (is_array($data)) {
  38. $s = '$o = array();' . PHP_EOL . PHP_EOL;
  39. $s .= '// ** THIS IS AN AUTO GENERATED FILE.'
  40. . ' DO NOT EDIT MANUALLY ** ';
  41. foreach ($data as $key => $value) {
  42. $s .= PHP_EOL . PHP_EOL .
  43. "//==================== $key ===================="
  44. . PHP_EOL . PHP_EOL;
  45. if (is_array($value)) {
  46. $s .= '$o[\'' . $key . '\'] = array();';
  47. foreach ($value as $ke => $va) {
  48. $s .= PHP_EOL . PHP_EOL . "//==== $key $ke ===="
  49. . PHP_EOL . PHP_EOL;
  50. $s .= '$o[\'' . $key . '\'][\'' . $ke . '\'] = ' .
  51. str_replace(' ', ' ',
  52. var_export($va, true)) . ';';
  53. }
  54. } else {
  55. $s .= '$o[\'' . $key . '\'] = '
  56. . var_export($value, true) . ';';
  57. }
  58. }
  59. $s .= PHP_EOL . 'return $o;';
  60. } else {
  61. $s = 'return ' . var_export($data, true) . ';';
  62. }
  63. $file = $this->_file($name);
  64. $r = @file_put_contents($file, "<?php $s");
  65. @chmod($file, 0777);
  66. if ($r === false) {
  67. $this->throwException();
  68. }
  69. return $r;
  70. }
  71. /**
  72. * retrieve data from the cache
  73. *
  74. * @param string $name
  75. * @param bool $ignoreErrors
  76. *
  77. * @return mixed
  78. */
  79. public function get($name, $ignoreErrors = false)
  80. {
  81. $file = $this->_file($name);
  82. if (file_exists($file)) {
  83. return include($file);
  84. }
  85. }
  86. /**
  87. * delete data from the cache
  88. *
  89. * @param string $name
  90. * @param bool $ignoreErrors
  91. *
  92. * @return boolean true if successful
  93. */
  94. public function clear($name, $ignoreErrors = false)
  95. {
  96. return @unlink($this->_file($name));
  97. }
  98. /**
  99. * check if the given name is cached
  100. *
  101. * @param string $name
  102. *
  103. * @return boolean true if cached
  104. */
  105. public function isCached($name)
  106. {
  107. return file_exists($this->_file($name));
  108. }
  109. private function _file($name)
  110. {
  111. return self::$cacheDir . '/' . $name . '.php';
  112. }
  113. private function throwException()
  114. {
  115. throw new \Exception(
  116. 'The cache directory `'
  117. . self::$cacheDir . '` should exist with write permission.'
  118. );
  119. }
  120. }