2017-02-18 19:41:27 +01:00
|
|
|
<?php
|
|
|
|
namespace Psalm\Provider;
|
|
|
|
|
2018-10-17 21:52:26 +02:00
|
|
|
use PhpParser;
|
|
|
|
use Psalm\Checker\ProjectChecker;
|
|
|
|
|
2017-02-18 19:41:27 +01:00
|
|
|
class FileProvider
|
|
|
|
{
|
2018-10-26 22:17:15 +02:00
|
|
|
/**
|
|
|
|
* @var array<string, string>
|
|
|
|
*/
|
|
|
|
protected $temp_files = [];
|
|
|
|
|
2017-02-18 19:41:27 +01:00
|
|
|
/**
|
|
|
|
* @param string $file_path
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2017-07-25 22:11:02 +02:00
|
|
|
* @return string
|
2017-02-18 19:41:27 +01:00
|
|
|
*/
|
2018-10-26 22:17:15 +02:00
|
|
|
public function getContents($file_path, bool $go_to_source = false)
|
2017-02-18 19:41:27 +01:00
|
|
|
{
|
2018-10-26 22:17:15 +02:00
|
|
|
if (!$go_to_source && isset($this->temp_files[strtolower($file_path)])) {
|
|
|
|
return $this->temp_files[strtolower($file_path)];
|
|
|
|
}
|
|
|
|
|
2017-07-25 22:11:02 +02:00
|
|
|
return (string)file_get_contents($file_path);
|
2017-02-18 19:41:27 +01:00
|
|
|
}
|
|
|
|
|
2017-09-16 18:45:11 +02:00
|
|
|
/**
|
|
|
|
* @param string $file_path
|
|
|
|
* @param string $file_contents
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function setContents($file_path, $file_contents)
|
|
|
|
{
|
|
|
|
file_put_contents($file_path, $file_contents);
|
|
|
|
}
|
|
|
|
|
2017-02-18 19:41:27 +01:00
|
|
|
/**
|
2017-07-25 22:11:02 +02:00
|
|
|
* @param string $file_path
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2017-07-25 22:11:02 +02:00
|
|
|
* @return int
|
2017-02-18 19:41:27 +01:00
|
|
|
*/
|
2017-07-25 22:11:02 +02:00
|
|
|
public function getModifiedTime($file_path)
|
2017-02-18 19:41:27 +01:00
|
|
|
{
|
2017-07-25 22:11:02 +02:00
|
|
|
return (int)filemtime($file_path);
|
2017-02-18 19:41:27 +01:00
|
|
|
}
|
|
|
|
|
2018-10-26 22:17:15 +02:00
|
|
|
/**
|
|
|
|
* @param \LanguageServerProtocol\TextDocumentContentChangeEvent[] $changes
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function addTemporaryFileChanges(string $file_path, array $changes)
|
|
|
|
{
|
|
|
|
if (count($changes) === 1 && $changes[0]->range === null) {
|
|
|
|
$new_content = $changes[0]->text;
|
|
|
|
|
|
|
|
$this->temp_files[strtolower($file_path)] = $new_content;
|
|
|
|
} else {
|
|
|
|
throw new \UnexpectedValueException('Incremental changes not supported');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function openFile(string $file_path)
|
|
|
|
{
|
|
|
|
$this->temp_files[strtolower($file_path)] = $this->getContents($file_path, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function closeFile(string $file_path)
|
|
|
|
{
|
|
|
|
unset($this->temp_files[strtolower($file_path)]);
|
|
|
|
}
|
|
|
|
|
2017-02-18 19:41:27 +01:00
|
|
|
/**
|
2017-07-25 22:11:02 +02:00
|
|
|
* @param string $file_path
|
2017-05-27 02:16:18 +02:00
|
|
|
*
|
2017-07-25 22:11:02 +02:00
|
|
|
* @return bool
|
2017-02-18 19:41:27 +01:00
|
|
|
*/
|
2017-07-25 22:11:02 +02:00
|
|
|
public function fileExists($file_path)
|
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 string $dir_path
|
|
|
|
* @param array<string> $file_extensions
|
|
|
|
*
|
|
|
|
* @return array<int, string>
|
|
|
|
*/
|
|
|
|
public function getFilesInDir($dir_path, array $file_extensions)
|
|
|
|
{
|
|
|
|
$file_paths = [];
|
|
|
|
|
|
|
|
/** @var \RecursiveDirectoryIterator */
|
|
|
|
$iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($dir_path));
|
|
|
|
$iterator->rewind();
|
|
|
|
|
|
|
|
while ($iterator->valid()) {
|
|
|
|
if (!$iterator->isDot()) {
|
|
|
|
$extension = $iterator->getExtension();
|
|
|
|
if (in_array($extension, $file_extensions, true)) {
|
|
|
|
$file_paths[] = (string)$iterator->getRealPath();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$iterator->next();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $file_paths;
|
|
|
|
}
|
2017-02-18 19:41:27 +01:00
|
|
|
}
|