Cache.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * This file is part of PHP CS Fixer.
  5. *
  6. * (c) Fabien Potencier <fabien@symfony.com>
  7. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  8. *
  9. * This source file is subject to the MIT license that is bundled
  10. * with this source code in the file LICENSE.
  11. */
  12. namespace PhpCsFixer\Cache;
  13. use PhpCsFixer\Utils;
  14. /**
  15. * @author Andreas Möller <am@localheinz.com>
  16. *
  17. * @internal
  18. */
  19. final class Cache implements CacheInterface
  20. {
  21. private SignatureInterface $signature;
  22. /**
  23. * @var array<string, string>
  24. */
  25. private array $hashes = [];
  26. public function __construct(SignatureInterface $signature)
  27. {
  28. $this->signature = $signature;
  29. }
  30. public function getSignature(): SignatureInterface
  31. {
  32. return $this->signature;
  33. }
  34. public function has(string $file): bool
  35. {
  36. return \array_key_exists($file, $this->hashes);
  37. }
  38. public function get(string $file): ?string
  39. {
  40. if (!$this->has($file)) {
  41. return null;
  42. }
  43. return $this->hashes[$file];
  44. }
  45. public function set(string $file, string $hash): void
  46. {
  47. $this->hashes[$file] = $hash;
  48. }
  49. public function clear(string $file): void
  50. {
  51. unset($this->hashes[$file]);
  52. }
  53. public function toJson(): string
  54. {
  55. $json = json_encode([
  56. 'php' => $this->getSignature()->getPhpVersion(),
  57. 'version' => $this->getSignature()->getFixerVersion(),
  58. 'indent' => $this->getSignature()->getIndent(),
  59. 'lineEnding' => $this->getSignature()->getLineEnding(),
  60. 'rules' => $this->getSignature()->getRules(),
  61. 'hashes' => $this->hashes,
  62. ]);
  63. if (JSON_ERROR_NONE !== json_last_error() || false === $json) {
  64. throw new \UnexpectedValueException(sprintf(
  65. 'Cannot encode cache signature to JSON, error: "%s". If you have non-UTF8 chars in your signature, like in license for `header_comment`, consider enabling `ext-mbstring` or install `symfony/polyfill-mbstring`.',
  66. json_last_error_msg()
  67. ));
  68. }
  69. return $json;
  70. }
  71. /**
  72. * @throws \InvalidArgumentException
  73. */
  74. public static function fromJson(string $json): self
  75. {
  76. $data = json_decode($json, true);
  77. if (null === $data && JSON_ERROR_NONE !== json_last_error()) {
  78. throw new \InvalidArgumentException(sprintf(
  79. 'Value needs to be a valid JSON string, got "%s", error: "%s".',
  80. $json,
  81. json_last_error_msg()
  82. ));
  83. }
  84. $requiredKeys = [
  85. 'php',
  86. 'version',
  87. 'indent',
  88. 'lineEnding',
  89. 'rules',
  90. 'hashes',
  91. ];
  92. $missingKeys = array_diff_key(array_flip($requiredKeys), $data);
  93. if (\count($missingKeys) > 0) {
  94. throw new \InvalidArgumentException(sprintf(
  95. 'JSON data is missing keys %s',
  96. Utils::naturalLanguageJoin(array_keys($missingKeys))
  97. ));
  98. }
  99. $signature = new Signature(
  100. $data['php'],
  101. $data['version'],
  102. $data['indent'],
  103. $data['lineEnding'],
  104. $data['rules']
  105. );
  106. $cache = new self($signature);
  107. // before v3.11.1 the hashes were crc32 encoded and saved as integers
  108. // @TODO: remove the to string cast/array_map in v4.0
  109. $cache->hashes = array_map(static fn ($v): string => \is_int($v) ? (string) $v : $v, $data['hashes']);
  110. return $cache;
  111. }
  112. /**
  113. * @internal
  114. */
  115. public function backfillHashes(self $oldCache): bool
  116. {
  117. if (!$this->getSignature()->equals($oldCache->getSignature())) {
  118. return false;
  119. }
  120. $this->hashes = array_merge($oldCache->hashes, $this->hashes);
  121. return true;
  122. }
  123. }