PhpArrayTrait.php 5.0 KB

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