Skip to content

Commit 8172d10

Browse files
authored
Use database to build asset folder list (#311)
* Use database to build asset folder list * Fix bug with adding folders * Remove debugging * Test coverage * 🍺
1 parent 1cd16ac commit 8172d10

File tree

3 files changed

+97
-19
lines changed

3 files changed

+97
-19
lines changed

src/Assets/AssetContainerContents.php

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -40,28 +40,15 @@ public function directories()
4040
}
4141

4242
$this->folders = Cache::remember($this->key(), $this->ttl(), function () {
43-
return collect($this->directoryRecurse(''))
44-
->map(fn ($dir) => ['path' => $dir, 'type' => 'dir']);
43+
return $this->query()->select(['folder'])
44+
->distinct()
45+
->get()
46+
->map(fn ($model) => ['path' => $model->folder, 'type' => 'dir']);
4547
});
4648

4749
return $this->folders;
4850
}
4951

50-
private function directoryRecurse($directory)
51-
{
52-
$rootFolders = $this->container->disk()->getFolders($directory, false);
53-
54-
$folders = [];
55-
foreach ($rootFolders as $folder) {
56-
$folders[] = $folder;
57-
if ($subfolders = $this->directoryRecurse($folder)) {
58-
$folders = array_merge($folders, $subfolders);
59-
}
60-
}
61-
62-
return $folders;
63-
}
64-
6552
public function metaFilesIn($folder, $recursive)
6653
{
6754
return $this->query()

src/Commands/SyncAssets.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ private function processFolder(AssetContainer $container, $folder = '')
5151
$this->line("Processing folder: {$folder}");
5252

5353
// get raw listing of this folder, avoiding any of statamic's asset container caching
54-
$files = collect($container->disk()->filesystem()->listContents($folder))
54+
$contents = collect($container->disk()->filesystem()->listContents($folder));
55+
56+
$files = $contents
5557
->reject(fn ($item) => $item['type'] != 'file')
5658
->pluck('path');
5759

@@ -86,7 +88,9 @@ private function processFolder(AssetContainer $container, $folder = '')
8688
});
8789

8890
// process any sub-folders of this folder
89-
$container->folders($folder)
91+
$contents
92+
->reject(fn ($item) => $item['type'] != 'dir')
93+
->pluck('path')
9094
->each(function ($subfolder) use ($container, $folder) {
9195
if (str_contains($subfolder.'/', '.meta/')) {
9296
return;
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
3+
namespace Tests\Assets;
4+
5+
use Illuminate\Http\UploadedFile;
6+
use PHPUnit\Framework\Attributes\Test;
7+
use Statamic\Facades\AssetContainer;
8+
use Statamic\Testing\Concerns\PreventsSavingStacheItemsToDisk;
9+
use Tests\TestCase;
10+
11+
class AssetContainerContentsTest extends TestCase
12+
{
13+
use PreventsSavingStacheItemsToDisk;
14+
15+
public function setUp(): void
16+
{
17+
parent::setUp();
18+
19+
config(['filesystems.disks.test' => [
20+
'driver' => 'local',
21+
'root' => __DIR__.'/tmp',
22+
]]);
23+
}
24+
25+
public function tearDown(): void
26+
{
27+
app('files')->deleteDirectory(__DIR__.'/tmp');
28+
29+
parent::tearDown();
30+
}
31+
32+
#[Test]
33+
public function it_gets_a_folder_listing()
34+
{
35+
$container = tap(AssetContainer::make('test')->disk('test'))->save();
36+
$container->makeAsset('one/one.txt')->upload(UploadedFile::fake()->create('one.txt'));
37+
$container->makeAsset('two/two.txt')->upload(UploadedFile::fake()->create('two.txt'));
38+
39+
$this->assertSame([
40+
[
41+
'path' => 'one',
42+
'type' => 'dir',
43+
],
44+
[
45+
'path' => 'two',
46+
'type' => 'dir',
47+
],
48+
], $container->contents()->directories()->all());
49+
}
50+
51+
#[Test]
52+
public function it_adds_to_a_folder_listing()
53+
{
54+
$container = tap(AssetContainer::make('test')->disk('test'))->save();
55+
$container->makeAsset('one/one.txt')->upload(UploadedFile::fake()->create('one.txt'));
56+
$container->makeAsset('two/two.txt')->upload(UploadedFile::fake()->create('two.txt'));
57+
58+
$this->assertCount(2, $container->contents()->directories()->all());
59+
60+
$container->contents()->add('three');
61+
62+
$this->assertCount(3, $container->contents()->directories()->all());
63+
}
64+
65+
#[Test]
66+
public function it_forgets_a_folder_listing()
67+
{
68+
$container = tap(AssetContainer::make('test')->disk('test'))->save();
69+
$container->makeAsset('one/one.txt')->upload(UploadedFile::fake()->create('one.txt'));
70+
$container->makeAsset('two/two.txt')->upload(UploadedFile::fake()->create('two.txt'));
71+
72+
$this->assertCount(2, $container->contents()->directories()->all());
73+
74+
$container->contents()->forget('one');
75+
76+
$this->assertCount(1, $container->contents()->directories()->all());
77+
}
78+
79+
#[Test]
80+
public function it_creates_parent_folders_where_they_dont_exist()
81+
{
82+
$container = tap(AssetContainer::make('test')->disk('test'))->save();
83+
$container->makeAsset('one/two/three/file.txt')->upload(UploadedFile::fake()->create('one.txt'));
84+
85+
$this->assertCount(3, $container->contents()->filteredDirectoriesIn('', true));
86+
}
87+
}

0 commit comments

Comments
 (0)