2017-07-29 21:05:06 +02:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Provider;
|
|
|
|
|
|
|
|
use Psalm\Storage\FileStorage;
|
|
|
|
|
|
|
|
class FileStorageProvider
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* A list of data useful to analyse files
|
2018-01-21 16:53:17 +01:00
|
|
|
* Storing this statically is much faster (at least in PHP 7.2.1)
|
2017-07-29 21:05:06 +02:00
|
|
|
*
|
|
|
|
* @var array<string, FileStorage>
|
|
|
|
*/
|
|
|
|
private static $storage = [];
|
|
|
|
|
2018-02-19 06:27:39 +01:00
|
|
|
/**
|
|
|
|
* @var FileStorageCacheProvider
|
|
|
|
*/
|
|
|
|
public $cache;
|
|
|
|
|
|
|
|
public function __construct(FileStorageCacheProvider $cache)
|
|
|
|
{
|
|
|
|
$this->cache = $cache;
|
|
|
|
}
|
|
|
|
|
2017-07-29 21:05:06 +02:00
|
|
|
/**
|
|
|
|
* @param string $file_path
|
|
|
|
*
|
|
|
|
* @return FileStorage
|
|
|
|
*/
|
|
|
|
public function get($file_path)
|
|
|
|
{
|
|
|
|
$file_path = strtolower($file_path);
|
|
|
|
|
|
|
|
if (!isset(self::$storage[$file_path])) {
|
|
|
|
throw new \InvalidArgumentException('Could not get file storage for ' . $file_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
return self::$storage[$file_path];
|
|
|
|
}
|
|
|
|
|
2018-02-19 06:27:39 +01:00
|
|
|
/**
|
|
|
|
* @param string $file_path
|
|
|
|
* @param string $file_contents
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function has($file_path, $file_contents)
|
|
|
|
{
|
|
|
|
$file_path = strtolower($file_path);
|
|
|
|
|
|
|
|
if (isset(self::$storage[$file_path])) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$cached_value = $this->cache->getLatestFromCache($file_path, $file_contents);
|
|
|
|
|
|
|
|
if (!$cached_value) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
self::$storage[$file_path] = $cached_value;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-07-29 21:05:06 +02:00
|
|
|
/**
|
|
|
|
* @return array<string, FileStorage>
|
|
|
|
*/
|
|
|
|
public function getAll()
|
|
|
|
{
|
|
|
|
return self::$storage;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $file_path
|
|
|
|
*
|
|
|
|
* @return FileStorage
|
|
|
|
*/
|
|
|
|
public function create($file_path)
|
|
|
|
{
|
2018-02-19 17:53:30 +01:00
|
|
|
$file_path_lc = strtolower($file_path);
|
2017-07-29 21:05:06 +02:00
|
|
|
|
2018-02-19 17:53:30 +01:00
|
|
|
self::$storage[$file_path_lc] = $storage = new FileStorage();
|
2017-07-29 21:05:06 +02:00
|
|
|
|
|
|
|
$storage->file_path = $file_path;
|
|
|
|
|
|
|
|
return $storage;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2018-01-21 18:44:46 +01:00
|
|
|
public static function deleteAll()
|
2017-07-29 21:05:06 +02:00
|
|
|
{
|
|
|
|
self::$storage = [];
|
|
|
|
}
|
|
|
|
}
|