Skip to content

Commit

Permalink
include to eval
Browse files Browse the repository at this point in the history
  • Loading branch information
QuentinGab committed Aug 16, 2024
1 parent 66be241 commit 42b0603
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/Facades/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
* @method static array getDeadTranslations(string $locale, string $namespace, ?SearchCodeServiceInterface $service = null, null|(Closure(string $file, string[] $translations):void) $progress = null, ?array $ignore = null )
* @method static void sortAllTranslations()
* @method static \Elegantly\Translator\Translator clearCache()
* @method static Translations setTranslations(string $locale, string $namespace, array $values)
* @method static Translations deleteTranslations(string $locale, string $namespace, array $keys)
*
* @see \Elegantly\Translator\Translator
*/
Expand Down
22 changes: 18 additions & 4 deletions src/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,33 @@ public function getNamespaces(string $locale): array
->toArray();
}

/**
* This function uses eval and not include
* Because using 'include' would cache/compile the code in opcache
* Therefore it would not reflect the changes after the file is edited
*/
public function getTranslations(string $locale, string $namespace): Translations
{
$path = "{$locale}/{$namespace}.php";

if ($this->storage->exists($path)) {
if ($content = $this->getTranslationsFileContent($locale, $namespace)) {
return new Translations(
items: include $this->storage->path($path),
items: eval('?>'.$content),
);
}

return new Translations;
}

public function getTranslationsFileContent(string $locale, string $namespace): ?string
{
$path = "{$locale}/{$namespace}.php";

if ($this->storage->exists($path)) {
return $this->storage->get($path);
}

return null;
}

/**
* Return all the translations keys present in the reference locale but not in the other ones
*
Expand Down

0 comments on commit 42b0603

Please sign in to comment.