Skip to content

Commit

Permalink
feat: Added configurable functions to scan
Browse files Browse the repository at this point in the history
  • Loading branch information
maicol07 authored and thiagocordeiro committed Jan 31, 2023
1 parent f348ac9 commit a21f796
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 18 deletions.
8 changes: 8 additions & 0 deletions src/Framework/LaravelConfigLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ public function extensions(): array
return $this->loadConfigInArray('extensions');
}

/**
* @inheritDoc
*/
public function functions(): array
{
return $this->loadConfigInArray('functions');
}

/**
* @return array<string>
*/
Expand Down
1 change: 1 addition & 0 deletions src/Framework/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
],
'output' => resource_path('lang'),
'extensions' => ['php'],
'functions' => ['lang', '__'],
'container' => [
'config_loader' => LaravelConfigLoader::class,
'translation_repository' => LaravelJsonTranslationRepository::class,
Expand Down
7 changes: 7 additions & 0 deletions src/Translator/ConfigLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,11 @@ public function output(): string;
* @return array<string>
*/
public function extensions(): array;

/**
* Load the list of functions to be scanned
*
* @return array<string>
*/
public function functions(): array;
}
13 changes: 13 additions & 0 deletions src/Translator/Exception/InvalidFunctionsConfiguration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php declare(strict_types=1);

namespace Translator\Translator\Exception;

use Exception;

class InvalidFunctionsConfiguration extends Exception
{
public function __construct()
{
parent::__construct('Invalid functions configuration');
}
}
41 changes: 30 additions & 11 deletions src/Translator/TranslationScanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,20 @@

use Translator\Translator\Exception\InvalidDirectoriesConfiguration;
use Translator\Translator\Exception\InvalidExtensionsConfiguration;
use Translator\Translator\Exception\InvalidFunctionsConfiguration;

class TranslationScanner
{
/**
* @param string[] $extensions
* @param string[] $directories
* @param string[] $functions
* @return Translation[]
* @throws InvalidDirectoriesConfiguration
* @throws InvalidExtensionsConfiguration
* @throws InvalidFunctionsConfiguration
*/
public function scan(array $extensions, array $directories): array
public function scan(array $extensions, array $directories, array $functions): array
{
if (empty($extensions)) {
throw new InvalidExtensionsConfiguration();
Expand All @@ -24,30 +27,46 @@ public function scan(array $extensions, array $directories): array
throw new InvalidDirectoriesConfiguration();
}

if (empty($functions)) {
throw new InvalidFunctionsConfiguration();
}

$ext = implode(',', $extensions);

return array_reduce($directories, function (array $collection, string $directory) use ($ext): array {
return array_merge(
$collection,
$this->scanDirectory($directory, $ext)
);
}, []);
return array_reduce(
$directories,
function (array $collection, string $directory) use ($ext, $functions): array {
return array_merge(
$collection,
$this->scanDirectory($directory, $ext, $functions)
);
},
[]
);
}

/**
* @param string[] $functions
* @return Translation[]
*/
private function scanDirectory(string $path, string $extensions): array
private function scanDirectory(string $path, string $extensions, array $functions): array
{
$files = glob_recursive("{$path}/*.{{$extensions}}", GLOB_BRACE);

return array_reduce($files, function (array $keys, $file): array {
return array_reduce($files, function (array $keys, $file) use ($functions): array {
$content = $this->getFileContent($file);

$keysFromFunctions = array_reduce(
$functions,
function (array $keys, string $function) use ($content): array {
return array_merge($keys, $this->getKeysFromFunction($function, $content));
},
[]
);

return array_merge(
$keys,
$this->getKeysFromFunction('lang', $content),
$this->getKeysFromFunction('__', $content)
$keysFromFunctions
);
}, []);
}
Expand Down
3 changes: 2 additions & 1 deletion src/Translator/TranslationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ public function scanAndSaveNewKeys(): void
{
$directories = $this->config->directories();
$extensions = $this->config->extensions();
$functions = $this->config->functions();

$translations = $this->scanner->scan($extensions, $directories);
$translations = $this->scanner->scan($extensions, $directories, $functions);

$this->storeTranslations($translations);
}
Expand Down
22 changes: 16 additions & 6 deletions tests/Unit/Translator/TranslationScannerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PHPUnit\Framework\TestCase;
use Translator\Translator\Exception\InvalidDirectoriesConfiguration;
use Translator\Translator\Exception\InvalidFunctionsConfiguration;
use Translator\Translator\Translation;
use Translator\Translator\TranslationScanner;

Expand All @@ -24,14 +25,23 @@ public function testWhenDirectoriesToScanAreNotSetThenThrowException(): void

$this->expectException(InvalidDirectoriesConfiguration::class);

$this->scanner->scan(['php'], $directories);
$this->scanner->scan(['php'], $directories, ['lang', '__']);
}

public function testWhenFunctionsToScanAreNotSetThenThrowException(): void
{
$functions = [];

$this->expectException(InvalidFunctionsConfiguration::class);

$this->scanner->scan(['php'], ['App'], $functions);
}

public function testShouldFindTranslationsForUnderscoreFunctions(): void
{
$__dir = $this->fixturesDir . '/App/Functions/UnderscoreUnderscore';

$translations = $this->scanner->scan(['php'], [$__dir]);
$translations = $this->scanner->scan(['php'], [$__dir], ['lang', '__']);

$this->assertEquals(
[
Expand All @@ -45,7 +55,7 @@ public function testShouldFindTranslationsForLangFunctions(): void
{
$langDir = $this->fixturesDir . '/App/Functions/Lang';

$translations = $this->scanner->scan(['php'], [$langDir]);
$translations = $this->scanner->scan(['php'], [$langDir], ['lang', '__']);

$this->assertEquals(
[
Expand All @@ -59,7 +69,7 @@ public function testShouldFindTranslationsForDifferentFileExtensions(): void
{
$langDir = $this->fixturesDir . '/App/View';

$translations = $this->scanner->scan(['vue'], [$langDir]);
$translations = $this->scanner->scan(['vue'], [$langDir], ['lang', '__']);

$this->assertEquals(
[
Expand All @@ -74,7 +84,7 @@ public function testShouldFindTranslationsForBladeTemplates(): void
{
$viewDir = $this->fixturesDir . '/App/View';

$translations = $this->scanner->scan(['php'], [$viewDir]);
$translations = $this->scanner->scan(['php'], [$viewDir], ['lang', '__']);

$this->assertEquals(
[
Expand Down Expand Up @@ -104,7 +114,7 @@ public function testShouldFindMultipleTranslationForDifferentFunctionsAndFiles()
{
$appDir = $this->fixturesDir . '/App';

$translations = $this->scanner->scan(['php'], [$appDir]);
$translations = $this->scanner->scan(['php'], [$appDir], ['lang', '__']);

$this->assertEquals(
[
Expand Down
6 changes: 6 additions & 0 deletions tests/Unit/Translator/TranslationServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ public function testShouldScanAndSaveKeys(): void
$this->configLoader
->method('directories')
->willReturn([$this->fixturesDir . '/App/View']);
$this->configLoader
->method('functions')
->willReturn(['lang', '__']);

$translations = [
[new Translation('Welcome, :name', '')],
Expand All @@ -70,6 +73,9 @@ public function testWhenGivenTranslationAlreadyExistsThenDoNotOverride(): void
$this->configLoader
->method('directories')
->willReturn([$this->fixturesDir . '/App/Functions/Lang']);
$this->configLoader
->method('functions')
->willReturn(['lang', '__']);
$this->configLoader
->method('extensions')
->willReturn(['php']);
Expand Down

0 comments on commit a21f796

Please sign in to comment.