1
0
Fork 0

Add option to set file size limit

pull/668/head
Jürgen Steitz 2018-08-29 23:02:55 +02:00
parent a5417cdf72
commit 24e3b77b58
3 changed files with 44 additions and 6 deletions

View File

@ -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());
} }

View File

@ -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
* *
@ -20,13 +27,29 @@ class Options
public function setFileTypes(array $fileTypes) public function setFileTypes(array $fileTypes)
{ {
$fileTypes = filter_var_array($fileTypes, FILTER_SANITIZE_STRING); $fileTypes = filter_var_array($fileTypes, FILTER_SANITIZE_STRING);
$fileTypes = filter_var($fileTypes, FILTER_CALLBACK, ['options' => [$this, 'filterFileTypes']]); $fileTypes = filter_var($fileTypes, FILTER_CALLBACK, ['options' => [$this, 'filterFileTypes']]);
$fileTypes = array_filter($fileTypes, 'strlen'); $fileTypes = array_filter($fileTypes, 'strlen');
$fileTypes = array_values($fileTypes); $fileTypes = array_values($fileTypes);
$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);
}
} }

View File

@ -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) {