Skip to content

Commit b2b4700

Browse files
committed
normalize cache path
1 parent b62d174 commit b2b4700

File tree

5 files changed

+47
-49
lines changed

5 files changed

+47
-49
lines changed

src/Services/SearchCode/PhpParserService.php

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Elegantly\Translator\Services\SearchCode;
44

5-
use Closure;
65
use Elegantly\Translator\Caches\SearchCodeCache;
76
use Exception;
87
use Illuminate\Contracts\Filesystem\Filesystem;
@@ -21,6 +20,8 @@
2120
use Symfony\Component\Finder\Finder;
2221
use Symfony\Component\Finder\SplFileInfo;
2322

23+
use function Orchestra\Testbench\package_path;
24+
2425
class PhpParserService implements SearchCodeServiceInterface
2526
{
2627
public ?SearchCodeCache $cache = null;
@@ -71,13 +72,8 @@ public function finder(): Finder
7172
->files();
7273
}
7374

74-
public static function filterTranslationsKeys(?string $key): bool
75+
public static function isTranslationKeyFromPackage(string $key): bool
7576
{
76-
77-
if (blank($key)) {
78-
return false;
79-
}
80-
8177
preg_match(
8278
'/^(?<package>[a-zA-Z0-9-_]+)::(?<key>.+)$/',
8379
$key,
@@ -87,6 +83,16 @@ public static function filterTranslationsKeys(?string $key): bool
8783
return empty($matches);
8884
}
8985

86+
public static function filterTranslationsKeys(?string $key): bool
87+
{
88+
89+
if (blank($key)) {
90+
return false;
91+
}
92+
93+
return static::isTranslationKeyFromPackage($key);
94+
}
95+
9096
public static function isFunCallTo(
9197
FuncCall $node,
9298
string $function,
@@ -173,17 +179,13 @@ public static function scanCode(string $code): array
173179
->toArray();
174180
}
175181

176-
/**
177-
* @param null|(Closure(string $file, string[] $translations):void) $progress
178-
*/
179-
public function translationsByFiles(
180-
?Closure $progress = null,
181-
): array {
182+
public function translationsByFiles(): array
183+
{
182184
return collect($this->finder())
183-
->map(function (SplFileInfo $file, string $key) use ($progress) {
185+
->mapWithKeys(function (SplFileInfo $file, string $path) {
184186

185187
$lastModified = $file->getMTime();
186-
$cachedResult = $this->cache?->get($key);
188+
$cachedResult = $this->cache?->get($path);
187189

188190
if (
189191
$lastModified && $cachedResult &&
@@ -204,27 +206,26 @@ public function translationsByFiles(
204206
previous: $th
205207
);
206208
}
207-
$this->cache?->put($key, $translations);
209+
$this->cache?->put($path, $translations);
208210
}
209211

210-
if ($progress) {
211-
$progress($file, $translations);
212-
}
212+
$relativePath = str($path)
213+
->after(package_path())
214+
->after(base_path())
215+
->value();
213216

214-
return $translations;
217+
return [
218+
$relativePath => $translations,
219+
];
215220
})
216221
->filter()
217222
->sortKeys(SORT_NATURAL)
218223
->toArray();
219224
}
220225

221-
/**
222-
* @param null|(Closure(string $file, string[] $translations):void) $progress
223-
*/
224-
public function filesByTranslations(
225-
?Closure $progress = null,
226-
): array {
227-
$translations = $this->translationsByFiles($progress);
226+
public function filesByTranslations(): array
227+
{
228+
$translations = $this->translationsByFiles();
228229

229230
$results = [];
230231

src/Services/SearchCode/SearchCodeServiceInterface.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Elegantly\Translator\Services\SearchCode;
44

5-
use Closure;
65
use Elegantly\Translator\Caches\SearchCodeCache;
76

87
interface SearchCodeServiceInterface
@@ -12,14 +11,12 @@ public static function make(): self;
1211
public function getCache(): ?SearchCodeCache;
1312

1413
/**
15-
* @param null|(Closure(string $file, string[] $translations):void) $progress
1614
* @return array<string, string[]>
1715
*/
18-
public function translationsByFiles(?Closure $progress = null): array;
16+
public function translationsByFiles(): array;
1917

2018
/**
21-
* @param null|(Closure(string $file, string[] $translations):void) $progress
2219
* @return array<string, array{ count: int, files: string[] }>
2320
*/
24-
public function filesByTranslations(?Closure $progress = null): array;
21+
public function filesByTranslations(): array;
2522
}

tests/Feature/JsonDriverTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,26 +35,26 @@
3535
'messages.dummy.class' => [
3636
'count' => 1,
3737
'files' => [
38-
0 => '/Users/quentingabriele/dev/laravel-translator/tests/src/app/DummyClass.php',
38+
0 => '/tests/src/app/DummyClass.php',
3939
],
4040
],
4141
'messages.dummy.component' => [
4242
'count' => 1,
4343
'files' => [
44-
0 => '/Users/quentingabriele/dev/laravel-translator/tests/src/resources/components/dummy-component.blade.php',
44+
0 => '/tests/src/resources/components/dummy-component.blade.php',
4545
],
4646
],
4747
'messages.dummy.nested' => [
4848
'count' => 1,
4949
'files' => [
50-
0 => '/Users/quentingabriele/dev/laravel-translator/tests/src/resources/views/dummy-view.blade.php',
50+
0 => '/tests/src/resources/views/dummy-view.blade.php',
5151
],
5252
],
5353
'messages.dummy.view' => [
5454
'count' => 3,
5555
'files' => [
56-
0 => '/Users/quentingabriele/dev/laravel-translator/tests/src/resources/components/dummy-component.blade.php',
57-
1 => '/Users/quentingabriele/dev/laravel-translator/tests/src/resources/views/dummy-view.blade.php',
56+
0 => '/tests/src/resources/components/dummy-component.blade.php',
57+
1 => '/tests/src/resources/views/dummy-view.blade.php',
5858
],
5959
],
6060
]);

tests/Feature/PhpDriverTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
'This one is used.' => [
4747
'count' => 1,
4848
'files' => [
49-
0 => '/Users/quentingabriele/dev/laravel-translator/tests/src/resources/views/dummy-view.blade.php',
49+
0 => '/tests/src/resources/views/dummy-view.blade.php',
5050
],
5151
],
5252
]);

tests/Unit/PhpParserServiceTest.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,14 @@
6464
);
6565

6666
expect($service->translationsByFiles())->toBe([
67-
"{$appPath}/DummyClass.php" => [
67+
'/tests/src/app/DummyClass.php' => [
6868
'messages.dummy.class',
6969
],
70-
"{$resourcesPath}/components/dummy-component.blade.php" => [
70+
'/tests/src/resources/components/dummy-component.blade.php' => [
7171
'messages.dummy.component',
7272
'messages.dummy.view',
7373
],
74-
"{$resourcesPath}/views/dummy-view.blade.php" => [
74+
'/tests/src/resources/views/dummy-view.blade.php' => [
7575
'This one is used.',
7676
'messages.dummy.nested',
7777
'messages.dummy.view',
@@ -97,32 +97,32 @@
9797
'This one is used.' => [
9898
'count' => 1,
9999
'files' => [
100-
"{$resourcesPath}/views/dummy-view.blade.php",
100+
'/tests/src/resources/views/dummy-view.blade.php',
101101
],
102102
],
103103
'messages.dummy.class' => [
104104
'count' => 1,
105105
'files' => [
106-
"{$appPath}/DummyClass.php",
106+
'/tests/src/app/DummyClass.php',
107107
],
108108
],
109109
'messages.dummy.component' => [
110110
'count' => 1,
111111
'files' => [
112-
"{$resourcesPath}/components/dummy-component.blade.php",
112+
'/tests/src/resources/components/dummy-component.blade.php',
113113
],
114114
],
115115
'messages.dummy.nested' => [
116116
'count' => 1,
117117
'files' => [
118-
"{$resourcesPath}/views/dummy-view.blade.php",
118+
'/tests/src/resources/views/dummy-view.blade.php',
119119
],
120120
],
121121
'messages.dummy.view' => [
122122
'count' => 3,
123123
'files' => [
124-
"{$resourcesPath}/components/dummy-component.blade.php",
125-
"{$resourcesPath}/views/dummy-view.blade.php",
124+
'/tests/src/resources/components/dummy-component.blade.php',
125+
'/tests/src/resources/views/dummy-view.blade.php',
126126
],
127127
],
128128

@@ -143,11 +143,11 @@
143143
cachePath: Storage::fake('cache')
144144
);
145145

146-
$service->cache->put("{$appPath}/DummyClass.php", [
146+
$service->cache->put('/tests/src/app/DummyClass.php', [
147147
'messages.dummy.class',
148148
]);
149149

150-
$result = $service->cache->get("{$appPath}/DummyClass.php");
150+
$result = $service->cache->get('/tests/src/app/DummyClass.php');
151151

152152
expect($result['created_at'])->toBeInt();
153153

0 commit comments

Comments
 (0)