2018-02-19 06:27:39 +01:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Provider;
|
|
|
|
|
|
|
|
use Psalm\Config;
|
|
|
|
use Psalm\Storage\ClassLikeStorage;
|
|
|
|
|
|
|
|
class ClassLikeStorageCacheProvider
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var Config
|
|
|
|
*/
|
|
|
|
private $config;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $modified_timestamps = '';
|
|
|
|
|
|
|
|
const CLASS_CACHE_DIRECTORY = 'class_cache';
|
|
|
|
|
|
|
|
public function __construct(Config $config)
|
|
|
|
{
|
|
|
|
$this->config = $config;
|
|
|
|
|
|
|
|
$storage_dir = dirname(__DIR__) . DIRECTORY_SEPARATOR . 'Storage' . DIRECTORY_SEPARATOR;
|
|
|
|
|
|
|
|
$dependent_files = [
|
2018-03-27 04:13:10 +02:00
|
|
|
$storage_dir . 'FileStorage.php',
|
2018-02-19 06:27:39 +01:00
|
|
|
$storage_dir . 'FunctionLikeStorage.php',
|
2018-03-27 04:13:10 +02:00
|
|
|
$storage_dir . 'ClassLikeStorage.php',
|
2018-02-19 06:27:39 +01:00
|
|
|
$storage_dir . 'MethodStorage.php',
|
|
|
|
];
|
|
|
|
|
2018-09-04 21:28:03 +02:00
|
|
|
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);
|
|
|
|
}
|
2018-02-25 17:35:43 +01:00
|
|
|
|
2018-11-01 21:02:44 +01:00
|
|
|
$this->modified_timestamps .= PSALM_VERSION . $this->config->modified_time;
|
2018-02-19 06:27:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string|null $file_path
|
|
|
|
* @param string|null $file_contents
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function writeToCache(ClassLikeStorage $storage, $file_path, $file_contents)
|
|
|
|
{
|
|
|
|
$fq_classlike_name_lc = strtolower($storage->name);
|
2018-02-20 16:56:18 +01: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);
|
|
|
|
|
2018-02-19 17:14:07 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $fq_classlike_name_lc
|
|
|
|
* @param string|null $file_path
|
|
|
|
* @param string|null $file_contents
|
|
|
|
*
|
|
|
|
* @return ClassLikeStorage
|
|
|
|
*/
|
|
|
|
public function getLatestFromCache($fq_classlike_name_lc, $file_path, $file_contents)
|
|
|
|
{
|
2018-02-20 16:56:18 +01:00
|
|
|
$cached_value = $this->loadFromCache($fq_classlike_name_lc, $file_path);
|
2018-02-19 06:27:39 +01:00
|
|
|
|
|
|
|
if (!$cached_value) {
|
2018-10-17 21:52:26 +02:00
|
|
|
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 */
|
2018-03-12 05:01:21 +01:00
|
|
|
if (@get_class($cached_value) === '__PHP_Incomplete_Class'
|
|
|
|
|| $cache_hash !== $cached_value->hash
|
|
|
|
) {
|
2018-02-20 16:56:18 +01:00
|
|
|
unlink($this->getCacheLocationForClass($fq_classlike_name_lc, $file_path));
|
2018-02-19 06:27:39 +01:00
|
|
|
|
2018-10-17 21:52:26 +02:00
|
|
|
throw new \UnexpectedValueException($fq_classlike_name_lc . ' should not be outdated');
|
2018-02-19 06:27:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $cached_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string|null $file_path
|
|
|
|
* @param string|null $file_contents
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function getCacheHash($file_path, $file_contents)
|
|
|
|
{
|
2018-02-20 16:56:18 +01:00
|
|
|
return sha1(($file_path ? $file_contents : '') . $this->modified_timestamps);
|
2018-02-19 06:27:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $fq_classlike_name_lc
|
2018-02-20 16:56:18 +01:00
|
|
|
* @param string|null $file_path
|
2018-02-19 06:27:39 +01:00
|
|
|
*
|
|
|
|
* @return ClassLikeStorage|null
|
|
|
|
*/
|
2018-02-20 16:56:18 +01:00
|
|
|
private function loadFromCache($fq_classlike_name_lc, $file_path)
|
2018-02-19 06:27:39 +01:00
|
|
|
{
|
2018-02-20 16:56:18 +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)) {
|
2018-02-19 17:14:07 +01:00
|
|
|
if ($this->config->use_igbinary) {
|
|
|
|
/** @var ClassLikeStorage */
|
|
|
|
return igbinary_unserialize((string)file_get_contents($cache_location)) ?: null;
|
|
|
|
}
|
2018-02-19 17:53:30 +01:00
|
|
|
/** @var ClassLikeStorage */
|
|
|
|
return unserialize((string)file_get_contents($cache_location)) ?: null;
|
2018-02-19 06:27:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $fq_classlike_name_lc
|
2018-02-20 16:56:18 +01:00
|
|
|
* @param string|null $file_path
|
2018-02-19 06:27:39 +01:00
|
|
|
* @param bool $create_directory
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2018-02-20 16:56:18 +01:00
|
|
|
private function getCacheLocationForClass($fq_classlike_name_lc, $file_path, $create_directory = false)
|
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);
|
|
|
|
}
|
|
|
|
|
2018-02-19 17:14:07 +01:00
|
|
|
return $parser_cache_directory
|
|
|
|
. DIRECTORY_SEPARATOR
|
2018-02-20 16:56:18 +01:00
|
|
|
. sha1(($file_path ? strtolower($file_path) . ' ' : '') . $fq_classlike_name_lc)
|
2018-02-19 17:14:07 +01:00
|
|
|
. ($this->config->use_igbinary ? '-igbinary' : '');
|
2018-02-19 06:27:39 +01:00
|
|
|
}
|
|
|
|
}
|