From 89763896d3cbeaaf55a7fee94e787e563cb695eb Mon Sep 17 00:00:00 2001 From: Robert Lu Date: Tue, 21 May 2019 23:52:49 +0800 Subject: [PATCH] content-limit become configurable --- bin/php-language-server.php | 8 ++++++-- .../FileSystemContentRetriever.php | 6 ++---- src/ContentTooLargeException.php | 19 +++++++++---------- src/Indexer.php | 3 ++- 4 files changed, 19 insertions(+), 17 deletions(-) diff --git a/bin/php-language-server.php b/bin/php-language-server.php index 8e5d348..dd39df6 100644 --- a/bin/php-language-server.php +++ b/bin/php-language-server.php @@ -1,10 +1,10 @@ setLogger($logger); $xdebugHandler->check(); unset($xdebugHandler); +if (is_integer($options['content-limit'] ?? null)) { + ContentTooLargeException::$limit = (int)$options['content-limit']; +} + if (!empty($options['tcp'])) { // Connect to a TCP server $address = $options['tcp']; diff --git a/src/ContentRetriever/FileSystemContentRetriever.php b/src/ContentRetriever/FileSystemContentRetriever.php index 0ba21c2..59e2d3e 100644 --- a/src/ContentRetriever/FileSystemContentRetriever.php +++ b/src/ContentRetriever/FileSystemContentRetriever.php @@ -20,12 +20,10 @@ class FileSystemContentRetriever implements ContentRetriever */ public function retrieve(string $uri): Promise { - $limit = 150000; - $path = uriToPath($uri); $size = filesize($path); - if ($size > $limit) { - return Promise\reject(new ContentTooLargeException($uri, $size, $limit)); + if ($size > ContentTooLargeException::$limit) { + return Promise\reject(new ContentTooLargeException($uri, $size)); } return Promise\resolve(file_get_contents($path)); } diff --git a/src/ContentTooLargeException.php b/src/ContentTooLargeException.php index 8e615ab..8a8d483 100644 --- a/src/ContentTooLargeException.php +++ b/src/ContentTooLargeException.php @@ -8,6 +8,13 @@ namespace LanguageServer; */ class ContentTooLargeException extends \Exception { + /** + * The limit of content + * + * @var int + */ + public static $limit = 1000000; + /** * The URI of the file that exceeded the limit * @@ -22,24 +29,16 @@ class ContentTooLargeException extends \Exception */ public $size; - /** - * The limit that was exceeded in bytes - * - * @var int - */ - public $limit; - /** * @param string $uri The URI of the file that exceeded the limit * @param int $size The size of the file in bytes - * @param int $limit The limit that was exceeded in bytes * @param \Throwable $previous The previous exception used for the exception chaining. */ - public function __construct(string $uri, int $size, int $limit, \Throwable $previous = null) + public function __construct(string $uri, int $size, \Throwable $previous = null) { $this->uri = $uri; $this->size = $size; - $this->limit = $limit; + $limit = self::$limit; parent::__construct("$uri exceeds size limit of $limit bytes ($size)", 0, $previous); } } diff --git a/src/Indexer.php b/src/Indexer.php index 7ebff3f..42a5dde 100644 --- a/src/Indexer.php +++ b/src/Indexer.php @@ -218,9 +218,10 @@ class Indexer $this->client->textDocument->publishDiagnostics($uri, $document->getDiagnostics()); } } catch (ContentTooLargeException $e) { + $limit = ContentTooLargeException::$limit; $this->client->window->logMessage( MessageType::INFO, - "Ignoring file {$uri} because it exceeds size limit of {$e->limit} bytes ({$e->size})" + "Ignoring file {$uri} because it exceeds size limit of {$limit} bytes ({$e->size})" ); } catch (\Exception $e) { $this->client->window->logMessage(MessageType::ERROR, "Error parsing $uri: " . (string)$e);