Skip to content

Commit

Permalink
Refactor language key generation
Browse files Browse the repository at this point in the history
  • Loading branch information
dmason30 committed Oct 7, 2023
1 parent 48809e9 commit 34ec4ed
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 27 deletions.
2 changes: 1 addition & 1 deletion src/Contracts/Collections/ResultObjectCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

/**
* @method self __construct(ResultObject[] $items = null)
* @method self push(ResultObject[] ...$items)
* @method self push(ResultObject $item)
*/
interface ResultObjectCollection extends Arrayable, Enumerable
{
Expand Down
10 changes: 10 additions & 0 deletions src/Contracts/Factories/LanguageKeyFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Fidum\LaravelTranslationLinter\Contracts\Factories;

use Symfony\Component\Finder\SplFileInfo;

interface LanguageKeyFactory
{
public function getLanguageKey(SplFileInfo $file, string $locale, string $key): string;
}
2 changes: 1 addition & 1 deletion src/Contracts/Readers/LanguageFileReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@

interface LanguageFileReader
{
public function execute(SplFileInfo $file): array;
public function getTranslations(SplFileInfo $file): array;
}
27 changes: 27 additions & 0 deletions src/Factories/LanguageKeyFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Fidum\LaravelTranslationLinter\Factories;

use Fidum\LaravelTranslationLinter\Contracts\Factories\LanguageKeyFactory as LanguageKeyFactoryContract;
use Fidum\LaravelTranslationLinter\Managers\LanguageFileReaderManager;
use Symfony\Component\Finder\SplFileInfo;

class LanguageKeyFactory implements LanguageKeyFactoryContract
{
public function __construct(protected LanguageFileReaderManager $manager) {}

public function getLanguageKey(SplFileInfo $file, string $locale, string $key): string
{
$extension = $file->getExtension();

if ($this->manager->isEnabled($extension)) {
$reader = $this->manager->driver($extension);

if ($reader instanceof LanguageKeyFactoryContract) {
return $reader->getLanguageKey($file, $locale, $key);
}
}

return $key;
}
}
5 changes: 5 additions & 0 deletions src/LaravelTranslationLinterServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@
use Fidum\LaravelTranslationLinter\Contracts\Collections\ResultObjectCollection as ResultObjectCollectionContract;
use Fidum\LaravelTranslationLinter\Contracts\Collections\UnusedFieldCollection as UnusedFieldCollectionContract;
use Fidum\LaravelTranslationLinter\Contracts\Collections\UnusedFilterCollection as UnusedFilterCollectionContract;
use Fidum\LaravelTranslationLinter\Contracts\Factories\LanguageKeyFactory as LanguageKeyFactoryContract;
use Fidum\LaravelTranslationLinter\Contracts\Finders\ApplicationFileFinder as ApplicationFileFinderContract;
use Fidum\LaravelTranslationLinter\Contracts\Finders\LanguageFileFinder as LanguageFileFinderContract;
use Fidum\LaravelTranslationLinter\Contracts\Finders\LanguageNamespaceFinder as LanguageNamespaceFinderContract;
use Fidum\LaravelTranslationLinter\Contracts\Linters\UnusedTranslationLinter as UnusedTranslationLinterContract;
use Fidum\LaravelTranslationLinter\Contracts\Parsers\ApplicationFileParser as ApplicationFileParserContract;
use Fidum\LaravelTranslationLinter\Contracts\Readers\ApplicationFileReader as ApplicationFileReaderContract;
use Fidum\LaravelTranslationLinter\Contracts\Readers\LanguageFileReader as LanguageFileReaderContract;
use Fidum\LaravelTranslationLinter\Factories\LanguageKeyFactory;
use Fidum\LaravelTranslationLinter\Finders\ApplicationFileFinder;
use Fidum\LaravelTranslationLinter\Finders\LanguageFileFinder;
use Fidum\LaravelTranslationLinter\Finders\LanguageNamespaceFinder;
Expand Down Expand Up @@ -68,6 +70,8 @@ public function registeringPackage()
->needs('$driverConfig')
->giveConfig('translation-linter.lang.readers');

$this->app->bind(LanguageKeyFactoryContract::class, LanguageKeyFactory::class);

$this->app->bind(LanguageNamespaceFinderContract::class, LanguageNamespaceFinder::class);

$this->app->bind(ResultObjectCollectionContract::class, ResultObjectCollection::class);
Expand Down Expand Up @@ -96,6 +100,7 @@ public function provides()
LanguageFileFinderContract::class,
LanguageFileReaderContract::class,
LanguageFileReaderManager::class,
LanguageKeyFactoryContract::class,
LanguageNamespaceFinderContract::class,
ResultObjectCollectionContract::class,
UnusedFieldCollectionContract::class,
Expand Down
27 changes: 7 additions & 20 deletions src/Linters/UnusedTranslationLinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
namespace Fidum\LaravelTranslationLinter\Linters;

use Fidum\LaravelTranslationLinter\Contracts\Collections\ResultObjectCollection;
use Fidum\LaravelTranslationLinter\Contracts\Factories\LanguageKeyFactory;
use Fidum\LaravelTranslationLinter\Contracts\Finders\LanguageFileFinder;
use Fidum\LaravelTranslationLinter\Contracts\Finders\LanguageNamespaceFinder;
use Fidum\LaravelTranslationLinter\Contracts\Linters\UnusedTranslationLinter as UnusedTranslationLinterContract;
use Fidum\LaravelTranslationLinter\Contracts\Readers\ApplicationFileReader;
use Fidum\LaravelTranslationLinter\Contracts\Readers\LanguageFileReader;
use Fidum\LaravelTranslationLinter\Data\ResultObject;
use Fidum\LaravelTranslationLinter\Finders\LanguageNamespaceFinder;
use Fidum\LaravelTranslationLinter\Readers\ApplicationFileReader;
use Fidum\LaravelTranslationLinter\Readers\LanguageFileReader;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Illuminate\Support\Stringable;
Expand All @@ -20,6 +21,7 @@ public function __construct(
protected ApplicationFileReader $used,
protected LanguageFileFinder $files,
protected LanguageFileReader $translations,
protected LanguageKeyFactory $factory,
protected LanguageNamespaceFinder $namespaces,
protected ResultObjectCollection $results,
protected array $locales,
Expand All @@ -37,10 +39,10 @@ public function execute(): ResultObjectCollection

/** @var SplFileInfo $file */
foreach ($files as $file) {
$translations = $this->translations->execute($file);
$translations = $this->translations->getTranslations($file);

foreach ($translations as $field => $children) {
$group = $this->getLanguageKey($file, $locale, $field);
$group = $this->factory->getLanguageKey($file, $locale, $field);

foreach (Arr::dot(Arr::wrap($children)) as $key => $value) {
$groupedKey = Str::of($group)
Expand Down Expand Up @@ -70,19 +72,4 @@ public function execute(): ResultObjectCollection

return $this->results;
}

protected function getLanguageKey(SplFileInfo $file, string $language, string $key): string
{
if ($file->getExtension() === 'json') {
return $key;
}

return Str::of($file->getPath())
->finish(DIRECTORY_SEPARATOR)
->after(DIRECTORY_SEPARATOR.$language.DIRECTORY_SEPARATOR)
->append($file->getFilenameWithoutExtension())
->append('.')
->append($key)
->toString();
}
}
2 changes: 1 addition & 1 deletion src/Readers/JsonFileReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

class JsonFileReader implements LanguageFileReaderContract
{
public function execute(SplFileInfo $file): array
public function getTranslations(SplFileInfo $file): array
{
$translations = json_decode($file->getContents(), true);

Expand Down
4 changes: 2 additions & 2 deletions src/Readers/LanguageFileReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ class LanguageFileReader implements LanguageFileReaderContract
{
public function __construct(protected LanguageFileReaderManager $manager) {}

public function execute(SplFileInfo $file): array
public function getTranslations(SplFileInfo $file): array
{
$extension = $file->getExtension();
$translations = [];

if ($this->manager->isEnabled($extension)) {
$translations = $this->manager->driver($extension)->execute($file);
$translations = $this->manager->driver($extension)->getTranslations($file);

if (! $translations) {
throw new InvalidArgumentException("Unable to extract any data from {$file->getPathname()}!");
Expand Down
17 changes: 15 additions & 2 deletions src/Readers/PhpFileReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,26 @@

namespace Fidum\LaravelTranslationLinter\Readers;

use Fidum\LaravelTranslationLinter\Contracts\Factories\LanguageKeyFactory as LanguageKeyFactoryContract;
use Fidum\LaravelTranslationLinter\Contracts\Readers\LanguageFileReader as LanguageFileReaderContract;
use Illuminate\Support\Str;
use InvalidArgumentException;
use Symfony\Component\Finder\SplFileInfo;

class PhpFileReader implements LanguageFileReaderContract
class PhpFileReader implements LanguageFileReaderContract, LanguageKeyFactoryContract
{
public function execute(SplFileInfo $file): array
public function getLanguageKey(SplFileInfo $file, string $locale, string $key): string
{
return Str::of($file->getPath())
->finish(DIRECTORY_SEPARATOR)
->after(DIRECTORY_SEPARATOR.$locale.DIRECTORY_SEPARATOR)
->append($file->getFilenameWithoutExtension())
->append('.')
->append($key)
->toString();
}

public function getTranslations(SplFileInfo $file): array
{
$translations = include $file->getPathname();

Expand Down

0 comments on commit 34ec4ed

Please sign in to comment.