Add option to set file size limit
parent
a5417cdf72
commit
24e3b77b58
|
@ -221,7 +221,7 @@ class Indexer
|
|||
yield timeout();
|
||||
$this->client->window->logMessage(MessageType::LOG, "Parsing $uri");
|
||||
try {
|
||||
$document = yield $this->documentLoader->load($uri);
|
||||
$document = yield $this->documentLoader->load($uri, $this->options->fileSizeLimit);
|
||||
if (!isVendored($document, $this->composerJson)) {
|
||||
$this->client->textDocument->publishDiagnostics($uri, $document->getDiagnostics());
|
||||
}
|
||||
|
|
|
@ -12,6 +12,13 @@ class Options
|
|||
*/
|
||||
public $fileTypes = ['.php'];
|
||||
|
||||
/**
|
||||
* Maximum file size to index
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
public $fileSizeLimit = 150000;
|
||||
|
||||
/**
|
||||
* Validate/Filter input and set options for file types
|
||||
*
|
||||
|
@ -27,6 +34,22 @@ class Options
|
|||
$this->fileTypes = !empty($fileTypes) ? $fileTypes : $this->fileTypes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate/Filter input and set option for file size limit
|
||||
*
|
||||
* @param string $fileSizeLimit Size in human readable format or -1 for unlimited
|
||||
*/
|
||||
public function setFileSizeLimit(string $fileSizeLimit)
|
||||
{
|
||||
$fileSizeLimit = filter_var($fileSizeLimit, FILTER_SANITIZE_STRING);
|
||||
|
||||
if ($fileSizeLimit === '-1') {
|
||||
$this->fileSizeLimit = PHP_INT_MAX;
|
||||
} else {
|
||||
$this->fileSizeLimit = $this->convertFileSize($fileSizeLimit);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter valid file type
|
||||
*
|
||||
|
@ -47,4 +70,20 @@ class Options
|
|||
|
||||
return $fileType;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert human readable file size to byte
|
||||
*
|
||||
* @param string $fileSize
|
||||
* @return int
|
||||
*/
|
||||
private function convertFileSize(string $fileSize)
|
||||
{
|
||||
preg_match('/(\d+)(\w)/', $fileSize, $match);
|
||||
$sizes = 'KMG';
|
||||
$size = (int) $match[1];
|
||||
$factor = strpos($sizes, strtoupper($match[2])) + 1;
|
||||
|
||||
return $size * pow(1000, $factor);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -100,13 +100,12 @@ class PhpDocumentLoader
|
|||
* The document is NOT added to the list of open documents, but definitions are registered.
|
||||
*
|
||||
* @param string $uri
|
||||
* @param int $limit
|
||||
* @return Promise <PhpDocument>
|
||||
*/
|
||||
public function load(string $uri): Promise
|
||||
public function load(string $uri, int $limit): Promise
|
||||
{
|
||||
return coroutine(function () use ($uri) {
|
||||
|
||||
$limit = 150000;
|
||||
return coroutine(function () use ($uri, $limit) {
|
||||
$content = yield $this->contentRetriever->retrieve($uri);
|
||||
$size = strlen($content);
|
||||
if ($size > $limit) {
|
||||
|
|
Loading…
Reference in New Issue