Add option to set file size limit
parent
a5417cdf72
commit
24e3b77b58
|
@ -221,7 +221,7 @@ class Indexer
|
||||||
yield timeout();
|
yield timeout();
|
||||||
$this->client->window->logMessage(MessageType::LOG, "Parsing $uri");
|
$this->client->window->logMessage(MessageType::LOG, "Parsing $uri");
|
||||||
try {
|
try {
|
||||||
$document = yield $this->documentLoader->load($uri);
|
$document = yield $this->documentLoader->load($uri, $this->options->fileSizeLimit);
|
||||||
if (!isVendored($document, $this->composerJson)) {
|
if (!isVendored($document, $this->composerJson)) {
|
||||||
$this->client->textDocument->publishDiagnostics($uri, $document->getDiagnostics());
|
$this->client->textDocument->publishDiagnostics($uri, $document->getDiagnostics());
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,6 +12,13 @@ class Options
|
||||||
*/
|
*/
|
||||||
public $fileTypes = ['.php'];
|
public $fileTypes = ['.php'];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Maximum file size to index
|
||||||
|
*
|
||||||
|
* @var int
|
||||||
|
*/
|
||||||
|
public $fileSizeLimit = 150000;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Validate/Filter input and set options for file types
|
* Validate/Filter input and set options for file types
|
||||||
*
|
*
|
||||||
|
@ -27,6 +34,22 @@ class Options
|
||||||
$this->fileTypes = !empty($fileTypes) ? $fileTypes : $this->fileTypes;
|
$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
|
* Filter valid file type
|
||||||
*
|
*
|
||||||
|
@ -47,4 +70,20 @@ class Options
|
||||||
|
|
||||||
return $fileType;
|
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.
|
* The document is NOT added to the list of open documents, but definitions are registered.
|
||||||
*
|
*
|
||||||
* @param string $uri
|
* @param string $uri
|
||||||
|
* @param int $limit
|
||||||
* @return Promise <PhpDocument>
|
* @return Promise <PhpDocument>
|
||||||
*/
|
*/
|
||||||
public function load(string $uri): Promise
|
public function load(string $uri, int $limit): Promise
|
||||||
{
|
{
|
||||||
return coroutine(function () use ($uri) {
|
return coroutine(function () use ($uri, $limit) {
|
||||||
|
|
||||||
$limit = 150000;
|
|
||||||
$content = yield $this->contentRetriever->retrieve($uri);
|
$content = yield $this->contentRetriever->retrieve($uri);
|
||||||
$size = strlen($content);
|
$size = strlen($content);
|
||||||
if ($size > $limit) {
|
if ($size > $limit) {
|
||||||
|
|
Loading…
Reference in New Issue