PhpFileCache.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. /*
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. *
  15. * This software consists of voluntary contributions made by many individuals
  16. * and is licensed under the MIT license. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\Common\Cache;
  20. /**
  21. * Php file cache driver.
  22. *
  23. * @since 2.3
  24. * @author Fabio B. Silva <fabio.bat.silva@gmail.com>
  25. */
  26. class PhpFileCache extends FileCache
  27. {
  28. const EXTENSION = '.doctrinecache.php';
  29. /**
  30. * {@inheritdoc}
  31. */
  32. protected $extension = self::EXTENSION;
  33. /**
  34. * {@inheritdoc}
  35. */
  36. protected function doFetch($id)
  37. {
  38. $filename = $this->getFilename($id);
  39. if ( ! is_file($filename)) {
  40. return false;
  41. }
  42. $value = include $filename;
  43. if ($value['lifetime'] !== 0 && $value['lifetime'] < time()) {
  44. return false;
  45. }
  46. return $value['data'];
  47. }
  48. /**
  49. * {@inheritdoc}
  50. */
  51. protected function doContains($id)
  52. {
  53. $filename = $this->getFilename($id);
  54. if ( ! is_file($filename)) {
  55. return false;
  56. }
  57. $value = include $filename;
  58. return $value['lifetime'] === 0 || $value['lifetime'] > time();
  59. }
  60. /**
  61. * {@inheritdoc}
  62. */
  63. protected function doSave($id, $data, $lifeTime = 0)
  64. {
  65. if ($lifeTime > 0) {
  66. $lifeTime = time() + $lifeTime;
  67. }
  68. if (is_object($data) && ! method_exists($data, '__set_state')) {
  69. throw new \InvalidArgumentException(
  70. "Invalid argument given, PhpFileCache only allows objects that implement __set_state() " .
  71. "and fully support var_export(). You can use the FilesystemCache to save arbitrary object " .
  72. "graphs using serialize()/deserialize()."
  73. );
  74. }
  75. $filename = $this->getFilename($id);
  76. $filepath = pathinfo($filename, PATHINFO_DIRNAME);
  77. if ( ! is_dir($filepath)) {
  78. mkdir($filepath, 0777, true);
  79. }
  80. $value = array(
  81. 'lifetime' => $lifeTime,
  82. 'data' => $data
  83. );
  84. $value = var_export($value, true);
  85. $code = sprintf('<?php return %s;', $value);
  86. return file_put_contents($filename, $code);
  87. }
  88. }