PhpFileCache.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. public function __construct($directory, $extension = self::EXTENSION, $umask = 0002)
  33. {
  34. parent::__construct($directory, $extension, $umask);
  35. }
  36. /**
  37. * {@inheritdoc}
  38. */
  39. protected function doFetch($id)
  40. {
  41. $value = $this->includeFileForId($id);
  42. if (! $value) {
  43. return false;
  44. }
  45. if ($value['lifetime'] !== 0 && $value['lifetime'] < time()) {
  46. return false;
  47. }
  48. return $value['data'];
  49. }
  50. /**
  51. * {@inheritdoc}
  52. */
  53. protected function doContains($id)
  54. {
  55. $value = $this->includeFileForId($id);
  56. if (! $value) {
  57. return false;
  58. }
  59. return $value['lifetime'] === 0 || $value['lifetime'] > time();
  60. }
  61. /**
  62. * {@inheritdoc}
  63. */
  64. protected function doSave($id, $data, $lifeTime = 0)
  65. {
  66. if ($lifeTime > 0) {
  67. $lifeTime = time() + $lifeTime;
  68. }
  69. if (is_object($data) && ! method_exists($data, '__set_state')) {
  70. throw new \InvalidArgumentException(
  71. "Invalid argument given, PhpFileCache only allows objects that implement __set_state() " .
  72. "and fully support var_export(). You can use the FilesystemCache to save arbitrary object " .
  73. "graphs using serialize()/deserialize()."
  74. );
  75. }
  76. $filename = $this->getFilename($id);
  77. $value = array(
  78. 'lifetime' => $lifeTime,
  79. 'data' => $data
  80. );
  81. $value = var_export($value, true);
  82. $code = sprintf('<?php return %s;', $value);
  83. return $this->writeFile($filename, $code);
  84. }
  85. /**
  86. * @param string $id
  87. *
  88. * @return array|false
  89. */
  90. private function includeFileForId($id)
  91. {
  92. $fileName = $this->getFilename($id);
  93. // note: error suppression is still faster than `file_exists`, `is_file` and `is_readable`
  94. $value = @include $fileName;
  95. if (! isset($value['lifetime'])) {
  96. return false;
  97. }
  98. return $value;
  99. }
  100. }