Skip to content

Commit

Permalink
Merge pull request #28 from thiagocordeiro/thiago/#26-scan-different-…
Browse files Browse the repository at this point in the history
…file-extension

#26 Added multiple extension scan capabilities
  • Loading branch information
thiagocordeiro authored Apr 25, 2020
2 parents f3d1f2a + 4fc7061 commit 288807b
Show file tree
Hide file tree
Showing 11 changed files with 545 additions and 428 deletions.
875 changes: 459 additions & 416 deletions composer.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
<exclude name="SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingTraversableReturnTypeHintSpecification"/>
<exclude name="SlevomatCodingStandard.TypeHints.DisallowArrayTypeHintSyntax.DisallowedArrayTypeHintSyntax"/>
<exclude name="SlevomatCodingStandard.TypeHints.TypeHintDeclaration.UselessDocComment"/>
<exclude name="Squiz.Strings.ConcatenationSpacing.PaddingFound"/>
</rule>

<rule ref="SlevomatCodingStandard.TypeHints.DeclareStrictTypes">
Expand Down
5 changes: 5 additions & 0 deletions src/Framework/LaravelConfigLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ public function output(): string
return $this->loadConfigInString('output');
}

public function extensions(): array
{
return $this->loadConfigInArray('extensions');
}

private function loadConfigInArray(string $key): array
{
$values = $this->load($key);
Expand Down
1 change: 1 addition & 0 deletions src/Framework/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
resource_path('views'),
],
'output' => resource_path('lang'),
'extensions' => ['php'],
'container' => [
'config_loader' => LaravelConfigLoader::class,
'translation_repository' => LaravelJsonTranslationRepository::class,
Expand Down
2 changes: 2 additions & 0 deletions src/Translator/ConfigLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ public function languages(): array;
public function directories(): array;

public function output(): string;

public function extensions(): array;
}
13 changes: 13 additions & 0 deletions src/Translator/Exception/InvalidExtensionsConfiguration.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 InvalidExtensionsConfiguration extends Exception
{
public function __construct()
{
parent::__construct('Invalid extensions configuration');
}
}
20 changes: 15 additions & 5 deletions src/Translator/TranslationScanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,43 @@
namespace Translator\Translator;

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

class TranslationScanner
{
/**
* @param string[] $extensions
* @param string[] $directories
* @return Translation[]
* @throws InvalidDirectoriesConfiguration
* @throws InvalidExtensionsConfiguration
*/
public function scan(string ...$directories): array
public function scan(array $extensions, array $directories): array
{
if (empty($extensions)) {
throw new InvalidExtensionsConfiguration();
}

if (empty($directories)) {
throw new InvalidDirectoriesConfiguration();
}

return array_reduce($directories, function (array $collection, string $directory): array {
$ext = implode(',', $extensions);

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

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

return array_reduce($files, function (array $keys, $file): array {
$content = $this->getFileContent($file);
Expand Down
5 changes: 4 additions & 1 deletion src/Translator/TranslationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Translator\Translator;

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

class TranslationService
{
Expand All @@ -24,12 +25,14 @@ public function __construct(ConfigLoader $config, TranslationScanner $scanner, T

/**
* @throws InvalidDirectoriesConfiguration
* @throws InvalidExtensionsConfiguration
*/
public function scanAndSaveNewKeys(): void
{
$directories = $this->config->directories();
$extensions = $this->config->extensions();

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

$this->storeTranslations($translations);
}
Expand Down
18 changes: 18 additions & 0 deletions tests/Fixtures/App/View/Component.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<template>
<div>
{{ __('This is a vue component') }}
</div>
</template>

<script>
export default {
data() {
return {
labels: {
title: __('Vue Component Title'),
},
};
},
};
</script>

27 changes: 21 additions & 6 deletions tests/Unit/Translator/TranslationScannerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ public function testWhenDirectoriesToScanAreNotSetThenThrowException(): void

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

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

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

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

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

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

$this->assertEquals(
[
Expand All @@ -58,11 +58,26 @@ public function testShouldFindTranslationsForLangFunctions(): void
);
}

public function testShouldFindTranslationsForDifferentFileExtensions(): void
{
$langDir = $this->fixturesDir . '/App/View';

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

$this->assertEquals(
[
'This is a vue component' => new Translation('This is a vue component', ''),
'Vue Component Title' => new Translation('Vue Component Title', ''),
],
$translations
);
}

public function testShouldFindTranslationsForBladeTemplates(): void
{
$viewDir = $this->fixturesDir.'/App/View';
$viewDir = $this->fixturesDir . '/App/View';

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

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

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

$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 @@ -44,6 +44,9 @@ protected function setUp(): void

public function testShouldScanAndSaveKeys(): void
{
$this->configLoader
->method('extensions')
->willReturn(['php']);
$this->configLoader
->method('directories')
->willReturn([$this->fixturesDir.'/App/View']);
Expand Down Expand Up @@ -72,6 +75,9 @@ public function testWhenGivenTranslationAlreadyExistsThenDoNotOverride(): void
$this->configLoader
->method('directories')
->willReturn([$this->fixturesDir.'/App/Functions/Lang']);
$this->configLoader
->method('extensions')
->willReturn(['php']);

$this->repository
->method('exists')
Expand Down

0 comments on commit 288807b

Please sign in to comment.