2017-02-18 19:41:27 +01:00
|
|
|
<?php
|
2018-11-06 03:57:36 +01:00
|
|
|
namespace Psalm\Internal\Provider;
|
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 = [];
|
|
|
|
|
2018-11-20 21:51:47 +01:00
|
|
|
/**
|
|
|
|
* @var array<string, string>
|
|
|
|
*/
|
|
|
|
protected $open_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)];
|
|
|
|
}
|
|
|
|
|
2019-05-08 14:40:26 +02:00
|
|
|
if (isset($this->open_files[strtolower($file_path)])) {
|
2018-11-20 21:51:47 +01:00
|
|
|
return $this->open_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)
|
|
|
|
{
|
2018-11-20 21:51:47 +01:00
|
|
|
if (isset($this->open_files[strtolower($file_path)])) {
|
|
|
|
$this->open_files[strtolower($file_path)] = $file_contents;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($this->temp_files[strtolower($file_path)])) {
|
|
|
|
$this->temp_files[strtolower($file_path)] = $file_contents;
|
|
|
|
}
|
|
|
|
|
2017-09-16 18:45:11 +02:00
|
|
|
file_put_contents($file_path, $file_contents);
|
|
|
|
}
|
|
|
|
|
2019-06-13 21:25:55 +02:00
|
|
|
/**
|
|
|
|
* @param string $file_path
|
|
|
|
* @param string $file_contents
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function setOpenContents($file_path, $file_contents)
|
|
|
|
{
|
|
|
|
if (isset($this->open_files[strtolower($file_path)])) {
|
|
|
|
$this->open_files[strtolower($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
|
|
|
/**
|
2018-11-09 16:41:51 +01:00
|
|
|
* @return void
|
2018-10-26 22:17:15 +02:00
|
|
|
*/
|
2018-11-09 16:41:51 +01:00
|
|
|
public function addTemporaryFileChanges(string $file_path, string $new_content)
|
2018-10-26 22:17:15 +02:00
|
|
|
{
|
2018-11-09 16:41:51 +01:00
|
|
|
$this->temp_files[strtolower($file_path)] = $new_content;
|
2018-10-26 22:17:15 +02:00
|
|
|
}
|
|
|
|
|
2018-11-20 21:51:47 +01:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function removeTemporaryFileChanges(string $file_path)
|
|
|
|
{
|
|
|
|
unset($this->temp_files[strtolower($file_path)]);
|
|
|
|
}
|
|
|
|
|
2018-10-26 22:17:15 +02:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function openFile(string $file_path)
|
|
|
|
{
|
2018-11-20 21:51:47 +01:00
|
|
|
$this->open_files[strtolower($file_path)] = $this->getContents($file_path, true);
|
2018-10-26 22:17:15 +02:00
|
|
|
}
|
|
|
|
|
2018-11-20 22:32:40 +01:00
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isOpen(string $file_path)
|
|
|
|
{
|
2019-05-17 12:48:05 +02:00
|
|
|
return isset($this->temp_files[strtolower($file_path)]) || isset($this->open_files[strtolower($file_path)]);
|
2018-11-20 22:32:40 +01:00
|
|
|
}
|
|
|
|
|
2018-10-26 22:17:15 +02:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function closeFile(string $file_path)
|
|
|
|
{
|
|
|
|
unset($this->temp_files[strtolower($file_path)]);
|
2018-11-20 21:51:47 +01:00
|
|
|
unset($this->open_files[strtolower($file_path)]);
|
2018-10-26 22:17:15 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|