From 762820d85298f8fb73cd00734108df5098fc4205 Mon Sep 17 00:00:00 2001 From: Arzaroth Lekva Date: Fri, 24 Apr 2020 15:40:34 +0200 Subject: [PATCH] Relative strings location path --- src/Scanner/CodeScanner.php | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/src/Scanner/CodeScanner.php b/src/Scanner/CodeScanner.php index c84b0dbc..60e22277 100644 --- a/src/Scanner/CodeScanner.php +++ b/src/Scanner/CodeScanner.php @@ -14,6 +14,8 @@ abstract class CodeScanner extends Scanner { protected $ignoreInvalidFunctions = false; + protected $relativeBasePath = null; + protected $commentsPrefixes = []; protected $functions = [ @@ -63,6 +65,13 @@ public function ignoreInvalidFunctions($ignore = true): self 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 +91,22 @@ 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(); @@ -94,7 +119,11 @@ protected function handleFunction(ParsedFunction $function) $translation = call_user_func([$this, $handler], $function); if ($translation) { - $translation->getReferences()->add($function->getFilename(), $function->getLine()); + $path = $function->getFilename(); + if ($this->relativeBasePath !== null) { + $path = $this->getRelativePath($path); + } + $translation->getReferences()->add($path, $function->getLine()); } }