PhpArrayTrait.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Cache\Traits;
  11. use Symfony\Component\Cache\CacheItem;
  12. use Symfony\Component\Cache\Exception\InvalidArgumentException;
  13. use Symfony\Component\VarExporter\VarExporter;
  14. /**
  15. * @author Titouan Galopin <galopintitouan@gmail.com>
  16. * @author Nicolas Grekas <p@tchwork.com>
  17. *
  18. * @internal
  19. */
  20. trait PhpArrayTrait
  21. {
  22. use ProxyTrait;
  23. private $file;
  24. private $keys;
  25. private $values;
  26. /**
  27. * Store an array of cached values.
  28. *
  29. * @param array $values The cached values
  30. */
  31. public function warmUp(array $values)
  32. {
  33. if (file_exists($this->file)) {
  34. if (!is_file($this->file)) {
  35. throw new InvalidArgumentException(sprintf('Cache path exists and is not a file: %s.', $this->file));
  36. }
  37. if (!is_writable($this->file)) {
  38. throw new InvalidArgumentException(sprintf('Cache file is not writable: %s.', $this->file));
  39. }
  40. } else {
  41. $directory = \dirname($this->file);
  42. if (!is_dir($directory) && !@mkdir($directory, 0777, true)) {
  43. throw new InvalidArgumentException(sprintf('Cache directory does not exist and cannot be created: %s.', $directory));
  44. }
  45. if (!is_writable($directory)) {
  46. throw new InvalidArgumentException(sprintf('Cache directory is not writable: %s.', $directory));
  47. }
  48. }
  49. $dumpedValues = '';
  50. $dumpedMap = [];
  51. $dump = <<<'EOF'
  52. <?php
  53. // This file has been auto-generated by the Symfony Cache Component.
  54. return [[
  55. EOF;
  56. foreach ($values as $key => $value) {
  57. CacheItem::validateKey(\is_int($key) ? (string) $key : $key);
  58. $isStaticValue = true;
  59. if (null === $value) {
  60. $value = "'N;'";
  61. } elseif (\is_object($value) || \is_array($value)) {
  62. try {
  63. $value = VarExporter::export($value, $isStaticValue);
  64. } catch (\Exception $e) {
  65. throw new InvalidArgumentException(sprintf('Cache key "%s" has non-serializable %s value.', $key, \is_object($value) ? \get_class($value) : 'array'), 0, $e);
  66. }
  67. } elseif (\is_string($value)) {
  68. // Wrap "N;" in a closure to not confuse it with an encoded `null`
  69. if ('N;' === $value) {
  70. $isStaticValue = false;
  71. }
  72. $value = var_export($value, true);
  73. } elseif (!is_scalar($value)) {
  74. throw new InvalidArgumentException(sprintf('Cache key "%s" has non-serializable %s value.', $key, \gettype($value)));
  75. } else {
  76. $value = var_export($value, true);
  77. }
  78. if (!$isStaticValue) {
  79. $value = str_replace("\n", "\n ", $value);
  80. $value = "static function () {\n return {$value};\n}";
  81. }
  82. $hash = hash('md5', $value);
  83. if (null === $id = $dumpedMap[$hash] ?? null) {
  84. $id = $dumpedMap[$hash] = \count($dumpedMap);
  85. $dumpedValues .= "{$id} => {$value},\n";
  86. }
  87. $dump .= var_export($key, true)." => {$id},\n";
  88. }
  89. $dump .= "\n], [\n\n{$dumpedValues}\n]];\n";
  90. $tmpFile = uniqid($this->file, true);
  91. file_put_contents($tmpFile, $dump);
  92. @chmod($tmpFile, 0666 & ~umask());
  93. unset($serialized, $value, $dump);
  94. @rename($tmpFile, $this->file);
  95. $this->initialize();
  96. }
  97. /**
  98. * {@inheritdoc}
  99. */
  100. public function clear()
  101. {
  102. $this->keys = $this->values = [];
  103. $cleared = @unlink($this->file) || !file_exists($this->file);
  104. return $this->pool->clear() && $cleared;
  105. }
  106. /**
  107. * Load the cache file.
  108. */
  109. private function initialize()
  110. {
  111. if (!file_exists($this->file)) {
  112. $this->keys = $this->values = [];
  113. return;
  114. }
  115. $values = (include $this->file) ?: [[], []];
  116. if (2 !== \count($values) || !isset($values[0], $values[1])) {
  117. $this->keys = $this->values = [];
  118. } else {
  119. list($this->keys, $this->values) = $values;
  120. }
  121. }
  122. }