Skip to content

Commit c26460f

Browse files
committed
support subdrivers
1 parent 1ebaa41 commit c26460f

File tree

13 files changed

+134
-5
lines changed

13 files changed

+134
-5
lines changed

src/Drivers/Driver.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,20 @@ abstract class Driver
1010
{
1111
/**
1212
* Make an instance based on configs
13+
*
14+
* @param array<array-key, mixed> $config
1315
*/
14-
abstract public static function make(): static;
16+
abstract public static function make(array $config = []): static;
17+
18+
/**
19+
* A unique identifier for the driver
20+
*/
21+
abstract public function getKey(): string;
22+
23+
/**
24+
* @return static[]
25+
*/
26+
abstract public function getSubDrivers(): array;
1527

1628
/**
1729
* @return array<int, string>

src/Drivers/JsonDriver.php

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,46 @@ final public function __construct(
1818
//
1919
}
2020

21-
public static function make(): static
21+
public static function make(array $config = []): static
2222
{
2323
return new static(
2424
storage: Storage::build([
2525
'driver' => 'local',
2626
'root' => config()->string('translator.lang_path'),
27+
...$config,
2728
])
2829
);
2930
}
3031

32+
public function getKey(): string
33+
{
34+
return $this->storage->path('');
35+
}
36+
37+
/**
38+
* @return static[]
39+
*/
40+
public function getSubDrivers(): array
41+
{
42+
return collect($this->storage->directories())
43+
->flatMap(function (string $directory) {
44+
$subdriver = static::make([
45+
'root' => $this->storage->path($directory),
46+
]);
47+
48+
return [
49+
$subdriver,
50+
...$subdriver->getSubDrivers(),
51+
];
52+
})
53+
->filter(function ($driver) {
54+
return ! empty($driver->getLocales());
55+
})
56+
->sortBy(fn ($driver) => $driver->getKey())
57+
->values()
58+
->all();
59+
}
60+
3161
public function getFilePath(string $locale): string
3262
{
3363
return "{$locale}.json";

src/Drivers/PhpDriver.php

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,49 @@ final public function __construct(
1818
//
1919
}
2020

21-
public static function make(): static
21+
public static function make(array $config = []): static
2222
{
2323
return new static(
2424
storage: Storage::build([
2525
'driver' => 'local',
2626
'root' => config()->string('translator.lang_path'),
27+
...$config,
2728
])
2829
);
2930
}
3031

32+
public function getKey(): string
33+
{
34+
return $this->storage->path('');
35+
}
36+
37+
/**
38+
* @return static[]
39+
*/
40+
public function getSubDrivers(): array
41+
{
42+
return collect($this->storage->directories())
43+
->flatMap(function (string $directory) {
44+
$subdriver = static::make([
45+
'root' => $this->storage->path($directory),
46+
]);
47+
48+
return [
49+
$subdriver,
50+
...$subdriver->getSubDrivers(),
51+
];
52+
})
53+
->filter(function ($driver) {
54+
return collect($driver->getLocales())
55+
->contains(function ($locale) use ($driver) {
56+
return ! empty($driver->getNamespaces($locale));
57+
});
58+
})
59+
->sortBy(fn ($driver) => $driver->getKey())
60+
->values()
61+
->all();
62+
}
63+
3164
public function getFilePath(string $locale, string $namespace): string
3265
{
3366
return "{$locale}/{$namespace}.php";
@@ -49,7 +82,7 @@ public function getLocales(): array
4982
*/
5083
public function getNamespaces(string $locale): array
5184
{
52-
return collect($this->storage->allFiles($locale))
85+
return collect($this->storage->files($locale))
5386
->filter(fn (string $file) => File::extension($file) === 'php')
5487
->map(fn (string $file) => File::name($file))
5588
->sort(SORT_NATURAL)

tests/Feature/PhpDriverTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
driver: $this->getPhpDriver(),
3030
);
3131

32-
expect($translator->getLocales())->toBe(['dummy', 'en', 'fr', 'fr_CA', 'pt_BR']);
32+
expect($translator->getLocales())->toBe(['en', 'fr', 'fr_CA', 'not_a_locale', 'pt_BR', 'sublang', 'vendorlang']);
3333
});
3434

3535
it('gets translations', function () {

tests/Unit/JsonDriverTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
it('gets nested folder as subdrivers', function () {
6+
$driver = $this->getJsonDriver();
7+
8+
$subDrivers = $driver->getSubDrivers();
9+
10+
$subDriversKeys = collect($subDrivers)->map(fn ($driver) => $driver->getKey())->all();
11+
12+
expect($subDriversKeys)->tobe([
13+
$driver->storage->path('sublang/'),
14+
$driver->storage->path('sublang/subsublang/'),
15+
$driver->storage->path('vendorlang/package/'),
16+
]);
17+
});

tests/Unit/PhpDriverTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,17 @@
2121
"<?php\n\ndeclare(strict_types=1);\n\nreturn [\n 'Login' => 'Login',\n 'Don\'t have an account?' => 'Don\'t have an account?',\n 'nested' => [\n 'Don\'t have an account?' => 'Don\'t have an account?',\n ],\n];\n"
2222
);
2323
});
24+
25+
it('gets nested folder as subdrivers', function () {
26+
$driver = $this->getPhpDriver();
27+
28+
$subDrivers = $driver->getSubDrivers();
29+
30+
$subDriversKeys = collect($subDrivers)->map(fn ($driver) => $driver->getKey())->all();
31+
32+
expect($subDriversKeys)->tobe([
33+
$driver->storage->path('sublang/'),
34+
$driver->storage->path('sublang/subsublang/'),
35+
$driver->storage->path('vendorlang/package/'),
36+
]);
37+
});
File renamed without changes.

tests/src/lang/sublang/fr.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"Sublang translations": "Sublang translations"
3+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
return [
6+
//
7+
];

0 commit comments

Comments
 (0)