2017-02-18 19:41:27 +01:00
|
|
|
<?php
|
2021-12-15 04:58:32 +01:00
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
namespace Psalm\Internal\Provider;
|
2017-02-18 19:41:27 +01:00
|
|
|
|
2021-12-14 17:51:31 +01:00
|
|
|
use FilesystemIterator;
|
2021-12-14 18:22:47 +01:00
|
|
|
use RecursiveCallbackFilterIterator;
|
2021-12-03 21:40:18 +01:00
|
|
|
use RecursiveDirectoryIterator;
|
2021-12-14 18:22:47 +01:00
|
|
|
use RecursiveIterator;
|
2021-12-03 21:40:18 +01:00
|
|
|
use RecursiveIteratorIterator;
|
|
|
|
use UnexpectedValueException;
|
|
|
|
|
2019-07-05 22:24:00 +02:00
|
|
|
use function file_exists;
|
2019-06-26 22:52:29 +02:00
|
|
|
use function file_get_contents;
|
|
|
|
use function file_put_contents;
|
|
|
|
use function filemtime;
|
|
|
|
use function in_array;
|
2020-01-12 16:53:12 +01:00
|
|
|
use function is_dir;
|
2019-06-26 22:52:29 +02:00
|
|
|
|
2021-12-14 18:22:47 +01:00
|
|
|
use const DIRECTORY_SEPARATOR;
|
|
|
|
|
2022-01-03 07:55:32 +01:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
2017-02-18 19:41:27 +01:00
|
|
|
class FileProvider
|
|
|
|
{
|
2018-10-26 22:17:15 +02:00
|
|
|
/**
|
2022-05-25 13:54:36 +02:00
|
|
|
* @var array<string, string>
|
2018-10-26 22:17:15 +02:00
|
|
|
*/
|
|
|
|
protected $temp_files = [];
|
|
|
|
|
2018-11-20 21:51:47 +01:00
|
|
|
/**
|
2022-05-25 13:54:36 +02:00
|
|
|
* @var array<string, string>
|
2018-11-20 21:51:47 +01:00
|
|
|
*/
|
2022-05-25 12:07:10 +02:00
|
|
|
protected static $open_files = [];
|
2018-11-20 21:51:47 +01:00
|
|
|
|
2022-11-04 19:04:23 +01:00
|
|
|
/** @psalm-mutation-free */
|
2020-09-07 01:36:47 +02:00
|
|
|
public function getContents(string $file_path, bool $go_to_source = false): string
|
2017-02-18 19:41:27 +01:00
|
|
|
{
|
2022-05-25 13:54:36 +02:00
|
|
|
if (!$go_to_source && isset($this->temp_files[$file_path])) {
|
|
|
|
return $this->temp_files[$file_path];
|
2018-10-26 22:17:15 +02:00
|
|
|
}
|
|
|
|
|
2022-11-04 19:04:23 +01:00
|
|
|
/** @psalm-suppress ImpureStaticProperty Used only for caching */
|
2022-05-25 13:54:36 +02:00
|
|
|
if (isset(self::$open_files[$file_path])) {
|
|
|
|
return self::$open_files[$file_path];
|
2018-11-20 21:51:47 +01:00
|
|
|
}
|
|
|
|
|
2022-11-04 19:04:23 +01:00
|
|
|
/** @psalm-suppress ImpureFunctionCall For our purposes, this should not mutate external state */
|
2019-07-11 17:07:39 +02:00
|
|
|
if (!file_exists($file_path)) {
|
2021-12-03 21:40:18 +01:00
|
|
|
throw new UnexpectedValueException('File ' . $file_path . ' should exist to get contents');
|
2019-07-11 17:07:39 +02:00
|
|
|
}
|
|
|
|
|
2022-11-04 19:04:23 +01:00
|
|
|
/** @psalm-suppress ImpureFunctionCall For our purposes, this should not mutate external state */
|
2020-01-12 16:53:12 +01:00
|
|
|
if (is_dir($file_path)) {
|
2021-12-03 21:40:18 +01:00
|
|
|
throw new UnexpectedValueException('File ' . $file_path . ' is a directory');
|
2020-01-12 16:53:12 +01:00
|
|
|
}
|
|
|
|
|
2022-11-04 19:04:23 +01:00
|
|
|
/** @psalm-suppress ImpureFunctionCall For our purposes, this should not mutate external state */
|
2022-05-25 12:07:10 +02:00
|
|
|
$file_contents = (string) file_get_contents($file_path);
|
|
|
|
|
2022-11-04 19:04:23 +01:00
|
|
|
/** @psalm-suppress ImpureStaticProperty Used only for caching */
|
2022-05-25 13:54:36 +02:00
|
|
|
self::$open_files[$file_path] = $file_contents;
|
2022-05-25 12:07:10 +02:00
|
|
|
|
|
|
|
return $file_contents;
|
2017-02-18 19:41:27 +01:00
|
|
|
}
|
|
|
|
|
2020-10-12 21:02:52 +02:00
|
|
|
public function setContents(string $file_path, string $file_contents): void
|
2017-09-16 18:45:11 +02:00
|
|
|
{
|
2022-05-25 13:54:36 +02:00
|
|
|
if (isset(self::$open_files[$file_path])) {
|
|
|
|
self::$open_files[$file_path] = $file_contents;
|
2018-11-20 21:51:47 +01:00
|
|
|
}
|
|
|
|
|
2022-05-25 13:54:36 +02:00
|
|
|
if (isset($this->temp_files[$file_path])) {
|
|
|
|
$this->temp_files[$file_path] = $file_contents;
|
2018-11-20 21:51:47 +01:00
|
|
|
}
|
|
|
|
|
2017-09-16 18:45:11 +02:00
|
|
|
file_put_contents($file_path, $file_contents);
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function setOpenContents(string $file_path, string $file_contents): void
|
2019-06-13 21:25:55 +02:00
|
|
|
{
|
2022-05-25 13:54:36 +02:00
|
|
|
if (isset(self::$open_files[$file_path])) {
|
|
|
|
self::$open_files[$file_path] = $file_contents;
|
2019-06-13 21:25:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-07 01:36:47 +02:00
|
|
|
public function getModifiedTime(string $file_path): int
|
2017-02-18 19:41:27 +01:00
|
|
|
{
|
2019-07-11 17:07:39 +02:00
|
|
|
if (!file_exists($file_path)) {
|
2021-12-03 21:40:18 +01:00
|
|
|
throw new UnexpectedValueException('File should exist to get modified time');
|
2019-07-11 17:07:39 +02:00
|
|
|
}
|
|
|
|
|
2022-05-25 12:07:10 +02:00
|
|
|
return (int) filemtime($file_path);
|
2017-02-18 19:41:27 +01:00
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function addTemporaryFileChanges(string $file_path, string $new_content): void
|
2018-10-26 22:17:15 +02:00
|
|
|
{
|
2022-05-25 13:54:36 +02:00
|
|
|
$this->temp_files[$file_path] = $new_content;
|
2018-10-26 22:17:15 +02:00
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function removeTemporaryFileChanges(string $file_path): void
|
2018-11-20 21:51:47 +01:00
|
|
|
{
|
2022-05-25 13:54:36 +02:00
|
|
|
unset($this->temp_files[$file_path]);
|
2018-11-20 21:51:47 +01:00
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function openFile(string $file_path): void
|
2018-10-26 22:17:15 +02:00
|
|
|
{
|
2022-05-25 13:54:36 +02:00
|
|
|
self::$open_files[$file_path] = $this->getContents($file_path, true);
|
2018-10-26 22:17:15 +02:00
|
|
|
}
|
|
|
|
|
2020-09-04 22:26:33 +02:00
|
|
|
public function isOpen(string $file_path): bool
|
2018-11-20 22:32:40 +01:00
|
|
|
{
|
2022-05-25 13:54:36 +02:00
|
|
|
return isset($this->temp_files[$file_path]) || isset(self::$open_files[$file_path]);
|
2018-11-20 22:32:40 +01:00
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function closeFile(string $file_path): void
|
2018-10-26 22:17:15 +02:00
|
|
|
{
|
2022-05-25 13:54:36 +02:00
|
|
|
unset($this->temp_files[$file_path], self::$open_files[$file_path]);
|
2018-10-26 22:17:15 +02:00
|
|
|
}
|
|
|
|
|
2020-09-07 01:36:47 +02:00
|
|
|
public function fileExists(string $file_path): bool
|
2017-02-18 19:41:27 +01:00
|
|
|
{
|
2017-07-25 22:11:02 +02:00
|
|
|
return file_exists($file_path);
|
2017-02-18 19:41:27 +01:00
|
|
|
}
|
2018-10-17 17:03:32 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array<string> $file_extensions
|
2021-12-22 19:47:57 +01:00
|
|
|
* @param null|callable(string):bool $filter
|
2018-10-17 17:03:32 +02:00
|
|
|
*
|
2020-10-17 18:36:44 +02:00
|
|
|
* @return list<string>
|
2018-10-17 17:03:32 +02:00
|
|
|
*/
|
2021-12-22 19:47:57 +01:00
|
|
|
public function getFilesInDir(string $dir_path, array $file_extensions, callable $filter = null): array
|
2018-10-17 17:03:32 +02:00
|
|
|
{
|
|
|
|
$file_paths = [];
|
|
|
|
|
2021-12-14 17:51:31 +01:00
|
|
|
$iterator = new RecursiveDirectoryIterator(
|
|
|
|
$dir_path,
|
|
|
|
FilesystemIterator::CURRENT_AS_PATHNAME | FilesystemIterator::SKIP_DOTS
|
|
|
|
);
|
|
|
|
|
2021-12-22 19:47:57 +01:00
|
|
|
if ($filter !== null) {
|
2021-12-14 18:22:47 +01:00
|
|
|
$iterator = new RecursiveCallbackFilterIterator(
|
|
|
|
$iterator,
|
|
|
|
/** @param mixed $_ */
|
2022-04-09 11:58:26 +02:00
|
|
|
static function (string $current, $_, RecursiveIterator $iterator) use ($filter): bool {
|
2021-12-22 19:47:57 +01:00
|
|
|
if ($iterator->hasChildren()) {
|
|
|
|
$path = $current . DIRECTORY_SEPARATOR;
|
|
|
|
} else {
|
|
|
|
$path = $current;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $filter($path);
|
2021-12-14 18:22:47 +01:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-12-04 03:37:19 +01:00
|
|
|
/** @var RecursiveDirectoryIterator */
|
2021-12-14 17:51:31 +01:00
|
|
|
$iterator = new RecursiveIteratorIterator($iterator);
|
2018-10-17 17:03:32 +02:00
|
|
|
$iterator->rewind();
|
|
|
|
|
|
|
|
while ($iterator->valid()) {
|
2021-12-14 17:51:31 +01:00
|
|
|
$extension = $iterator->getExtension();
|
|
|
|
if (in_array($extension, $file_extensions, true)) {
|
|
|
|
$file_paths[] = (string)$iterator->getRealPath();
|
2018-10-17 17:03:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$iterator->next();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $file_paths;
|
|
|
|
}
|
2017-02-18 19:41:27 +01:00
|
|
|
}
|