1
0
Fork 0

Moved utility functions to utils.php

pull/31/head
Stephan Unverwerth 2016-09-19 21:06:31 +02:00
parent 1865ce8538
commit 6b28cb6a93
3 changed files with 37 additions and 11 deletions

View File

@ -34,7 +34,8 @@
"autoload": {
"psr-4": {
"LanguageServer\\": "src/"
}
},
"files" : ["src/utils.php"]
},
"autoload-dev": {
"psr-4": {

View File

@ -129,13 +129,7 @@ class LanguageServer extends \AdvancedJsonRpc\Dispatcher
*/
private function indexProject(string $rootPath)
{
$dir = new \RecursiveDirectoryIterator($rootPath);
$ite = new \RecursiveIteratorIterator($dir);
$files = new \RegexIterator($ite, '/^.+\.php$/i', \RegexIterator::GET_MATCH);
$fileList = array();
foreach($files as $file) {
$fileList = array_merge($fileList, $file);
}
$fileList = findFilesRecursive($rootPath, '/^.+\.php$/i');
$numTotalFiles = count($fileList);
$startTime = microtime(true);
@ -143,8 +137,7 @@ class LanguageServer extends \AdvancedJsonRpc\Dispatcher
$processFile = function() use (&$fileList, &$processFile, $rootPath, $numTotalFiles, $startTime) {
if ($file = array_pop($fileList)) {
$uri = 'file://'.($file[0] == '/' || $file[0] == '\\' ? '' : '/').str_replace('\\', '/', $file);
$uri = pathToUri($file);
$fileNum = $numTotalFiles - count($fileList);
$shortName = substr($file, strlen($rootPath)+1);
$this->client->window->logMessage(3, "Parsing file $fileNum/$numTotalFiles: $shortName.");
@ -155,7 +148,8 @@ class LanguageServer extends \AdvancedJsonRpc\Dispatcher
}
else {
$duration = (int)(microtime(true) - $startTime);
$this->client->window->logMessage(3, "All PHP files parsed in $duration seconds.");
$mem = memory_get_usage(true);
$this->client->window->logMessage(3, "All PHP files parsed in $duration seconds. $mem bytes allocated.");
}
};

31
src/utils.php Normal file
View File

@ -0,0 +1,31 @@
<?php
namespace LanguageServer;
/**
* 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 = array();
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 {
return 'file://'.($filepath[0] == '/' || $filepath[0] == '\\' ? '' : '/').str_replace('\\', '/', $filepath);
}