diff --git a/src/Indexer.php b/src/Indexer.php index e411054..a9e3335 100644 --- a/src/Indexer.php +++ b/src/Indexer.php @@ -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()); } diff --git a/src/Options.php b/src/Options.php index 48ba540..81a0fa5 100644 --- a/src/Options.php +++ b/src/Options.php @@ -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 * @@ -20,13 +27,29 @@ class Options public function setFileTypes(array $fileTypes) { $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_values($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 * @@ -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); + } } diff --git a/src/PhpDocumentLoader.php b/src/PhpDocumentLoader.php index 57a7e9c..7bcb067 100644 --- a/src/PhpDocumentLoader.php +++ b/src/PhpDocumentLoader.php @@ -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 */ - 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) {