2016-09-30 09:30:08 +00:00
|
|
|
<?php
|
2016-09-30 09:54:49 +00:00
|
|
|
declare(strict_types = 1);
|
2016-09-30 09:30:08 +00:00
|
|
|
|
|
|
|
namespace LanguageServer;
|
|
|
|
|
2016-10-10 13:06:02 +00:00
|
|
|
use InvalidArgumentException;
|
|
|
|
|
2016-09-30 09:30:08 +00:00
|
|
|
/**
|
|
|
|
* Recursively Searches files with matching filename, starting at $path.
|
|
|
|
*
|
|
|
|
* @param string $path
|
|
|
|
* @param string $pattern
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
function findFilesRecursive(string $path, string $pattern): array {
|
|
|
|
$dir = new \RecursiveDirectoryIterator($path);
|
|
|
|
$ite = new \RecursiveIteratorIterator($dir);
|
|
|
|
$files = new \RegexIterator($ite, $pattern, \RegexIterator::GET_MATCH);
|
|
|
|
$fileList = [];
|
|
|
|
foreach ($files as $file) {
|
|
|
|
$fileList = array_merge($fileList, $file);
|
|
|
|
}
|
|
|
|
return $fileList;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Transforms an absolute file path into a URI as used by the language server protocol.
|
|
|
|
*
|
|
|
|
* @param string $filepath
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function pathToUri(string $filepath): string {
|
|
|
|
$filepath = trim(str_replace('\\', '/', $filepath), '/');
|
2016-10-10 13:06:02 +00:00
|
|
|
$parts = explode('/', $filepath);
|
|
|
|
// Don't %-encode the colon after a Windows drive letter
|
|
|
|
$first = array_shift($parts);
|
|
|
|
if (substr($first, -1) !== ':') {
|
|
|
|
$first = urlencode($first);
|
|
|
|
}
|
|
|
|
$parts = array_map('urlencode', $parts);
|
|
|
|
array_unshift($parts, $first);
|
|
|
|
$filepath = implode('/', $parts);
|
2016-09-30 09:30:08 +00:00
|
|
|
return 'file:///' . $filepath;
|
|
|
|
}
|
2016-10-10 13:06:02 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Transforms URI into file path
|
|
|
|
*
|
|
|
|
* @param string $uri
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function uriToPath(string $uri)
|
|
|
|
{
|
|
|
|
$fragments = parse_url($uri);
|
|
|
|
if ($fragments === null || !isset($fragments['scheme']) || $fragments['scheme'] !== 'file') {
|
|
|
|
throw new InvalidArgumentException("Not a valid file URI: $uri");
|
|
|
|
}
|
|
|
|
$filepath = urldecode($fragments['path']);
|
|
|
|
return strpos($filepath, ':') === false ? $filepath : str_replace('/', '\\', $filepath);
|
|
|
|
}
|