From d4830e3610abc46ddf9e2c4bf2262b8d5382d3e8 Mon Sep 17 00:00:00 2001 From: Arzaroth Lekva Date: Sat, 11 Apr 2020 17:39:16 +0200 Subject: [PATCH 1/2] Turn off locations & configure relative locations in scanner --- src/Scanner/CodeScanner.php | 41 +++++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/src/Scanner/CodeScanner.php b/src/Scanner/CodeScanner.php index c84b0dbc..e8b41e29 100644 --- a/src/Scanner/CodeScanner.php +++ b/src/Scanner/CodeScanner.php @@ -14,6 +14,10 @@ abstract class CodeScanner extends Scanner { protected $ignoreInvalidFunctions = false; + protected $enableLocations = true; + + protected $relativeBasePath = null; + protected $commentsPrefixes = []; protected $functions = [ @@ -63,6 +67,20 @@ public function ignoreInvalidFunctions($ignore = true): self return $this; } + public function enableLocations($enabled = true): self + { + $this->enableLocations = $enabled; + + return $this; + } + + public function setRelativeBasePath($basePath = null): self + { + $this->relativeBasePath = $basePath !== null ? (realpath($basePath) ?: null) : null; + + return $this; + } + public function extractCommentsStartingWith(string ...$prefixes): self { $this->commentsPrefixes = $prefixes; @@ -82,6 +100,21 @@ public function scanString(string $string, string $filename): void abstract public function getFunctionsScanner(): FunctionsScannerInterface; + protected function getRelativePath(string $path): string { + $to = explode(DIRECTORY_SEPARATOR, $this->relativeBasePath); + $from = explode(DIRECTORY_SEPARATOR, realpath($path)); + + while ($to && $from && ($to[0] === $from[0])) { + array_shift($to); + array_shift($from); + } + for ($i = 0; $i < count($to); $i++) { + array_unshift($from, '..'); + } + + return implode(DIRECTORY_SEPARATOR, $from); + } + protected function handleFunction(ParsedFunction $function) { $name = $function->getName(); @@ -93,8 +126,12 @@ protected function handleFunction(ParsedFunction $function) $translation = call_user_func([$this, $handler], $function); - if ($translation) { - $translation->getReferences()->add($function->getFilename(), $function->getLine()); + if ($translation && $this->enableLocations) { + $path = $function->getFilename(); + if ($this->relativeBasePath !== null) { + $path = $this->getRelativePath($path); + } + $translation->getReferences()->add($path, $function->getLine()); } } From 33b5139cc499b470bb7965edebdd5a1f5b9826d0 Mon Sep 17 00:00:00 2001 From: Arzaroth Lekva Date: Thu, 23 Apr 2020 17:57:43 +0200 Subject: [PATCH 2/2] Formatting --- src/Scanner/CodeScanner.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Scanner/CodeScanner.php b/src/Scanner/CodeScanner.php index e8b41e29..c2f5d23b 100644 --- a/src/Scanner/CodeScanner.php +++ b/src/Scanner/CodeScanner.php @@ -100,7 +100,8 @@ public function scanString(string $string, string $filename): void abstract public function getFunctionsScanner(): FunctionsScannerInterface; - protected function getRelativePath(string $path): string { + protected function getRelativePath(string $path): string + { $to = explode(DIRECTORY_SEPARATOR, $this->relativeBasePath); $from = explode(DIRECTORY_SEPARATOR, realpath($path)); @@ -108,7 +109,7 @@ protected function getRelativePath(string $path): string { array_shift($to); array_shift($from); } - for ($i = 0; $i < count($to); $i++) { + for ($i = 0; $i < count($to); $i++) { array_unshift($from, '..'); }