1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-15 19:07:00 +01:00
psalm/src/Psalm/Internal/Provider/ClassLikeStorageCacheProvider.php

166 lines
5.0 KiB
PHP
Raw Normal View History

2018-02-19 06:27:39 +01:00
<?php
2018-11-06 03:57:36 +01:00
namespace Psalm\Internal\Provider;
2018-02-19 06:27:39 +01:00
2019-07-19 05:08:54 +02:00
use Psalm\Config;
use Psalm\Storage\ClassLikeStorage;
2019-07-05 22:24:00 +02:00
use function dirname;
2019-07-19 05:08:54 +02:00
use const DIRECTORY_SEPARATOR;
use function array_merge;
use function file_exists;
2019-07-05 22:24:00 +02:00
use function filemtime;
2019-07-19 05:08:54 +02:00
use function strtolower;
use function file_put_contents;
2019-07-05 22:24:00 +02:00
use function igbinary_serialize;
use function serialize;
2019-07-19 05:08:54 +02:00
use function get_class;
2019-07-05 22:24:00 +02:00
use function unlink;
2019-07-19 05:08:54 +02:00
use function sha1;
use function igbinary_unserialize;
use function file_get_contents;
2019-07-05 22:24:00 +02:00
use function unserialize;
2019-07-19 05:08:54 +02:00
use function is_dir;
use function mkdir;
2018-02-19 06:27:39 +01:00
/**
* @internal
*/
2018-02-19 06:27:39 +01:00
class ClassLikeStorageCacheProvider
{
/**
* @var Config
*/
private $config;
/**
* @var string
*/
private $modified_timestamps = '';
2020-09-20 18:54:46 +02:00
private const CLASS_CACHE_DIRECTORY = 'class_cache';
2018-02-19 06:27:39 +01:00
public function __construct(Config $config)
{
$this->config = $config;
2018-11-06 03:57:36 +01:00
$storage_dir = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'Storage' . DIRECTORY_SEPARATOR;
2018-02-19 06:27:39 +01:00
$dependent_files = [
$storage_dir . 'FileStorage.php',
2018-02-19 06:27:39 +01:00
$storage_dir . 'FunctionLikeStorage.php',
$storage_dir . 'ClassLikeStorage.php',
2018-02-19 06:27:39 +01:00
$storage_dir . 'MethodStorage.php',
];
if ($config->after_visit_classlikes) {
$dependent_files = array_merge($dependent_files, $config->plugin_paths);
}
2018-02-19 06:27:39 +01:00
foreach ($dependent_files as $dependent_file_path) {
if (!file_exists($dependent_file_path)) {
throw new \UnexpectedValueException($dependent_file_path . ' must exist');
}
$this->modified_timestamps .= ' ' . filemtime($dependent_file_path);
}
$this->modified_timestamps .= $this->config->hash;
2018-02-19 06:27:39 +01:00
}
public function writeToCache(ClassLikeStorage $storage, ?string $file_path, ?string $file_contents): void
2018-02-19 06:27:39 +01:00
{
$fq_classlike_name_lc = strtolower($storage->name);
2019-07-19 05:08:54 +02:00
$cache_location = $this->getCacheLocationForClass($fq_classlike_name_lc, $file_path, true);
2018-02-19 06:27:39 +01:00
$storage->hash = $this->getCacheHash($file_path, $file_contents);
if ($this->config->use_igbinary) {
file_put_contents($cache_location, igbinary_serialize($storage));
} else {
file_put_contents($cache_location, serialize($storage));
}
2018-02-19 06:27:39 +01:00
}
public function getLatestFromCache(
string $fq_classlike_name_lc,
?string $file_path,
?string $file_contents
): ClassLikeStorage {
$cached_value = $this->loadFromCache($fq_classlike_name_lc, $file_path);
2018-02-19 06:27:39 +01:00
if (!$cached_value) {
throw new \UnexpectedValueException($fq_classlike_name_lc . ' should be in cache');
2018-02-19 06:27:39 +01:00
}
$cache_hash = $this->getCacheHash($file_path, $file_contents);
2018-09-04 20:37:33 +02:00
/** @psalm-suppress TypeDoesNotContainType */
if (@get_class($cached_value) === '__PHP_Incomplete_Class'
|| $cache_hash !== $cached_value->hash
) {
unlink($this->getCacheLocationForClass($fq_classlike_name_lc, $file_path));
2018-02-19 06:27:39 +01:00
throw new \UnexpectedValueException($fq_classlike_name_lc . ' should not be outdated');
2018-02-19 06:27:39 +01:00
}
return $cached_value;
}
private function getCacheHash(?string $file_path, ?string $file_contents): string
2018-02-19 06:27:39 +01:00
{
return sha1(($file_path ? $file_contents : '') . $this->modified_timestamps);
2018-02-19 06:27:39 +01:00
}
/**
* @psalm-suppress MixedAssignment
2018-02-19 06:27:39 +01:00
*/
private function loadFromCache(string $fq_classlike_name_lc, ?string $file_path): ?ClassLikeStorage
2018-02-19 06:27:39 +01:00
{
$cache_location = $this->getCacheLocationForClass($fq_classlike_name_lc, $file_path);
2018-02-19 06:27:39 +01:00
if (file_exists($cache_location)) {
if ($this->config->use_igbinary) {
$storage = igbinary_unserialize((string)file_get_contents($cache_location));
if ($storage instanceof ClassLikeStorage) {
return $storage;
}
return null;
}
$storage = unserialize((string)file_get_contents($cache_location));
if ($storage instanceof ClassLikeStorage) {
return $storage;
}
return null;
2018-02-19 06:27:39 +01:00
}
return null;
}
private function getCacheLocationForClass(
string $fq_classlike_name_lc,
?string $file_path,
bool $create_directory = false
): string {
2018-02-19 06:27:39 +01:00
$root_cache_directory = $this->config->getCacheDirectory();
if (!$root_cache_directory) {
throw new \UnexpectedValueException('No cache directory defined');
}
$parser_cache_directory = $root_cache_directory . DIRECTORY_SEPARATOR . self::CLASS_CACHE_DIRECTORY;
if ($create_directory && !is_dir($parser_cache_directory)) {
mkdir($parser_cache_directory, 0777, true);
}
return $parser_cache_directory
. DIRECTORY_SEPARATOR
. sha1(($file_path ? strtolower($file_path) . ' ' : '') . $fq_classlike_name_lc)
. ($this->config->use_igbinary ? '-igbinary' : '');
2018-02-19 06:27:39 +01:00
}
}