PluginProbe
Disk Usage Insights / 1.3
Disk Usage Insights v1.3
trunk 1.0 1.10 1.11 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9
disk-usage-insights / src / Domain / Collect / ScanDirForFilesJob.php

ScanDirForFilesJob.php in Disk Usage Insights 1.3, at src/Domain/Collect/ScanDirForFilesJob.php

57 lines 1.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Mgleis\DiskUsageInsights\Domain\Collect;
4
5 use Mgleis\DiskUsageInsights\Domain\FileEntry;
6 use Mgleis\DiskUsageInsights\Domain\Jobs\BaseJob;
7
8 class ScanDirForFilesJob extends BaseJob {
9
10 private int $skip;
11 private int $count;
12 private int $totalCount;
13
14 public function __construct(int $skip, int $count, int $totalCount) {
15 $this->skip = $skip;
16 $this->count = $count;
17 $this->totalCount = $totalCount;
18 }
19
20 public function work() {
21
22 $dirEntries = $this->fileEntryRepository->get($this->skip, $this->count, FileEntry::TYPE_DIR);
23 $root = $this->snapshotRepository->load()->root;
24 foreach ($dirEntries as $dirEntry) {
25
26 $realDir = $this->fileEntryRepository->calcFullPath($dirEntry, $root);
27 $pattern = $realDir . '/*';
28 $this->log("Scanning " . $realDir . " for files...");
29
30 $entries = glob($pattern);
31 $files = array_values(array_filter($entries, 'is_file'));
32
33 foreach ($files as $file) {
34 // persist file info
35 $fileEntry = new FileEntry();
36 $fileEntry->parent_id = $dirEntry->id;
37 $fileEntry->name = basename($file);
38 $fileEntry->type = FileEntry::TYPE_FILE;
39 $fileEntry->size = 0;
40 $this->fileEntryRepository->createOrUpdate($fileEntry);
41 }
42 }
43 }
44
45 public function toArray() {
46 return ['type' => self::class, 'args' => [$this->skip, $this->count, $this->totalCount]];
47 }
48
49 public function toDescription(): string {
50 $percent = $this->totalCount > 0
51 ? round(100 * $this->skip / $this->totalCount)
52 : '0';
53 return sprintf('Scanning dirs for files... %s%%', $percent);
54 }
55
56 }
57