2017-07-29 21:05:06 +02:00
|
|
|
<?php
|
2018-11-06 03:57:36 +01:00
|
|
|
namespace Psalm\Internal\Provider;
|
2017-07-29 21:05:06 +02:00
|
|
|
|
2019-07-05 22:24:00 +02:00
|
|
|
use function array_merge;
|
2017-07-29 21:05:06 +02:00
|
|
|
use Psalm\Storage\FileStorage;
|
2019-06-26 22:52:29 +02:00
|
|
|
use function strtolower;
|
2017-07-29 21:05:06 +02:00
|
|
|
|
2018-12-02 00:37:49 +01:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2017-07-29 21:05:06 +02:00
|
|
|
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-10-18 16:42:37 +02:00
|
|
|
/**
|
|
|
|
* A list of data useful to analyse new files
|
|
|
|
* Storing this statically is much faster (at least in PHP 7.2.1)
|
|
|
|
*
|
|
|
|
* @var array<string, FileStorage>
|
|
|
|
*/
|
|
|
|
private static $new_storage = [];
|
|
|
|
|
2018-02-19 06:27:39 +01:00
|
|
|
/**
|
2018-09-28 22:18:45 +02:00
|
|
|
* @var ?FileStorageCacheProvider
|
2018-02-19 06:27:39 +01:00
|
|
|
*/
|
|
|
|
public $cache;
|
|
|
|
|
2020-09-07 01:36:47 +02:00
|
|
|
public function __construct(?FileStorageCacheProvider $cache = null)
|
2018-02-19 06:27:39 +01:00
|
|
|
{
|
|
|
|
$this->cache = $cache;
|
|
|
|
}
|
|
|
|
|
2020-09-07 01:36:47 +02:00
|
|
|
public function get(string $file_path): FileStorage
|
2017-07-29 21:05:06 +02:00
|
|
|
{
|
|
|
|
$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];
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function remove(string $file_path): void
|
2018-09-28 22:18:45 +02:00
|
|
|
{
|
|
|
|
unset(self::$storage[strtolower($file_path)]);
|
|
|
|
}
|
|
|
|
|
2020-09-07 01:36:47 +02:00
|
|
|
public function has(string $file_path, ?string $file_contents = null): bool
|
2018-02-19 06:27:39 +01:00
|
|
|
{
|
|
|
|
$file_path = strtolower($file_path);
|
|
|
|
|
|
|
|
if (isset(self::$storage[$file_path])) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-04-03 23:41:33 +02:00
|
|
|
if ($file_contents === null) {
|
2018-09-28 22:18:45 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-04-03 23:41:33 +02:00
|
|
|
if (!$this->cache) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-02-19 06:27:39 +01:00
|
|
|
|
2019-04-03 23:41:33 +02:00
|
|
|
$cached_value = $this->cache->getLatestFromCache($file_path, $file_contents);
|
2018-02-19 06:27:39 +01:00
|
|
|
|
2019-04-03 23:41:33 +02:00
|
|
|
if (!$cached_value) {
|
|
|
|
return false;
|
2019-04-03 16:43:41 +02:00
|
|
|
}
|
2018-02-19 06:27:39 +01:00
|
|
|
|
2019-04-03 23:41:33 +02:00
|
|
|
self::$storage[$file_path] = $cached_value;
|
|
|
|
self::$new_storage[$file_path] = $cached_value;
|
|
|
|
|
2018-02-19 06:27:39 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-07-29 21:05:06 +02:00
|
|
|
/**
|
|
|
|
* @return array<string, FileStorage>
|
|
|
|
*/
|
2020-09-04 22:26:33 +02:00
|
|
|
public function getAll(): array
|
2017-07-29 21:05:06 +02:00
|
|
|
{
|
|
|
|
return self::$storage;
|
|
|
|
}
|
|
|
|
|
2018-10-18 16:42:37 +02:00
|
|
|
/**
|
|
|
|
* @return array<string, FileStorage>
|
|
|
|
*/
|
2020-09-04 22:26:33 +02:00
|
|
|
public function getNew(): array
|
2018-10-18 16:42:37 +02:00
|
|
|
{
|
|
|
|
return self::$new_storage;
|
|
|
|
}
|
|
|
|
|
2018-10-11 19:58:39 +02:00
|
|
|
/**
|
|
|
|
* @param array<string, FileStorage> $more
|
2019-07-05 22:24:00 +02:00
|
|
|
*
|
2018-10-11 19:58:39 +02:00
|
|
|
*/
|
2020-09-12 17:24:05 +02:00
|
|
|
public function addMore(array $more): void
|
2018-10-11 19:58:39 +02:00
|
|
|
{
|
2019-01-16 05:24:29 +01:00
|
|
|
self::$new_storage = array_merge(self::$new_storage, $more);
|
|
|
|
self::$storage = array_merge(self::$storage, $more);
|
2018-10-11 19:58:39 +02:00
|
|
|
}
|
|
|
|
|
2020-09-07 01:36:47 +02:00
|
|
|
public function create(string $file_path): FileStorage
|
2017-07-29 21:05:06 +02:00
|
|
|
{
|
2018-02-19 17:53:30 +01:00
|
|
|
$file_path_lc = strtolower($file_path);
|
2017-07-29 21:05:06 +02:00
|
|
|
|
2018-10-18 16:42:37 +02:00
|
|
|
$storage = new FileStorage($file_path);
|
|
|
|
self::$storage[$file_path_lc] = $storage;
|
|
|
|
self::$new_storage[$file_path_lc] = $storage;
|
2017-07-29 21:05:06 +02:00
|
|
|
|
|
|
|
return $storage;
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public static function deleteAll(): void
|
2017-07-29 21:05:06 +02:00
|
|
|
{
|
|
|
|
self::$storage = [];
|
|
|
|
}
|
2018-10-18 16:42:37 +02:00
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public static function populated(): void
|
2018-10-18 16:42:37 +02:00
|
|
|
{
|
|
|
|
self::$new_storage = [];
|
|
|
|
}
|
2017-07-29 21:05:06 +02:00
|
|
|
}
|