2017-07-29 21:05:06 +02:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Provider;
|
|
|
|
|
|
|
|
use Psalm\Storage\ClassLikeStorage;
|
|
|
|
|
|
|
|
class ClassLikeStorageProvider
|
|
|
|
{
|
2018-01-21 16:53:17 +01:00
|
|
|
/**
|
|
|
|
* Storing this statically is much faster (at least in PHP 7.2.1)
|
|
|
|
*
|
|
|
|
* @var array<string, ClassLikeStorage>
|
|
|
|
*/
|
2017-07-29 21:05:06 +02:00
|
|
|
private static $storage = [];
|
|
|
|
|
2018-02-19 06:27:39 +01:00
|
|
|
/**
|
|
|
|
* @var ClassLikeStorageCacheProvider
|
|
|
|
*/
|
|
|
|
public $cache;
|
|
|
|
|
|
|
|
public function __construct(ClassLikeStorageCacheProvider $cache)
|
|
|
|
{
|
|
|
|
$this->cache = $cache;
|
|
|
|
}
|
|
|
|
|
2017-07-29 21:05:06 +02:00
|
|
|
/**
|
|
|
|
* @param string $fq_classlike_name
|
|
|
|
*
|
|
|
|
* @return ClassLikeStorage
|
|
|
|
*/
|
|
|
|
public function get($fq_classlike_name)
|
|
|
|
{
|
|
|
|
$fq_classlike_name_lc = strtolower($fq_classlike_name);
|
|
|
|
|
|
|
|
if (!isset(self::$storage[$fq_classlike_name_lc])) {
|
|
|
|
throw new \InvalidArgumentException('Could not get class storage for ' . $fq_classlike_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
return self::$storage[$fq_classlike_name_lc];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-02-19 06:27:39 +01:00
|
|
|
* @param string $fq_classlike_name
|
2017-07-29 21:05:06 +02:00
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function has($fq_classlike_name)
|
|
|
|
{
|
2018-02-19 06:27:39 +01:00
|
|
|
$fq_classlike_name_lc = strtolower($fq_classlike_name);
|
|
|
|
|
|
|
|
return isset(self::$storage[$fq_classlike_name_lc]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $fq_classlike_name
|
|
|
|
* @param string|null $file_path
|
|
|
|
* @param string|null $file_contents
|
|
|
|
*
|
|
|
|
* @return ClassLikeStorage
|
|
|
|
*/
|
|
|
|
public function exhume($fq_classlike_name, $file_path, $file_contents)
|
|
|
|
{
|
|
|
|
$fq_classlike_name_lc = strtolower($fq_classlike_name);
|
|
|
|
|
|
|
|
if (isset(self::$storage[$fq_classlike_name_lc])) {
|
2018-02-19 17:53:30 +01:00
|
|
|
return self::$storage[$fq_classlike_name_lc];
|
2018-02-19 06:27:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
self::$storage[$fq_classlike_name_lc]
|
|
|
|
= $cached_value
|
|
|
|
= $this->cache->getLatestFromCache($fq_classlike_name_lc, $file_path, $file_contents);
|
|
|
|
|
|
|
|
return $cached_value;
|
2017-07-29 21:05:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array<string, ClassLikeStorage>
|
|
|
|
*/
|
|
|
|
public function getAll()
|
|
|
|
{
|
|
|
|
return self::$storage;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $fq_classlike_name
|
|
|
|
*
|
|
|
|
* @return ClassLikeStorage
|
|
|
|
*/
|
|
|
|
public function create($fq_classlike_name)
|
|
|
|
{
|
|
|
|
$fq_classlike_name_lc = strtolower($fq_classlike_name);
|
|
|
|
|
2018-05-03 17:38:27 +02:00
|
|
|
self::$storage[$fq_classlike_name_lc] = $storage = new ClassLikeStorage($fq_classlike_name);
|
2017-07-29 21:05:06 +02:00
|
|
|
|
|
|
|
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 = [];
|
|
|
|
}
|
|
|
|
}
|