| 1 |
<?php |
| 2 |
|
| 3 |
namespace Mgleis\DiskUsageInsights\Domain\Collect; |
| 4 |
|
| 5 |
use Mgleis\DiskUsageInsights\Domain\FileEntry; |
| 6 |
use Mgleis\DiskUsageInsights\Domain\Jobs\BaseJob; |
| 7 |
use Mgleis\DiskUsageInsights\Domain\Jobs\PhaseCoordinatorJob; |
| 8 |
|
| 9 |
class ScanDirForSubDirsJob extends BaseJob { |
| 10 |
|
| 11 |
private int $parentId; |
| 12 |
private string $parentName = ''; |
| 13 |
|
| 14 |
public function __construct(int $parentId, string $parentName = '') { |
| 15 |
$this->parentId = $parentId; |
| 16 |
$this->parentName = $parentName; |
| 17 |
} |
| 18 |
|
| 19 |
public function work() { |
| 20 |
|
| 21 |
if ($this->parentId != 0) { |
| 22 |
$parentFileEntry = $this->fileEntryRepository->findById($this->parentId); |
| 23 |
} else { |
| 24 |
// create dummy entry |
| 25 |
$parentFileEntry = new FileEntry(); |
| 26 |
$parentFileEntry->parent_id = 0; |
| 27 |
} |
| 28 |
$root = $this->snapshotRepository->load()->root; |
| 29 |
|
| 30 |
$realDir = realpath($this->fileEntryRepository->calcFullPath($parentFileEntry, $root)); |
| 31 |
|
| 32 |
//$this->log("Scanning " . $realDir . " for sub directories..."); |
| 33 |
|
| 34 |
$files = scandir($realDir) ?? []; |
| 35 |
foreach ($files as $file) { |
| 36 |
|
| 37 |
if ($file == '.') { |
| 38 |
continue;; |
| 39 |
} |
| 40 |
if ($file == '..') { |
| 41 |
continue; |
| 42 |
} |
| 43 |
if (!is_dir($realDir . '/' . $file)) { |
| 44 |
continue; |
| 45 |
} |
| 46 |
if (is_link($realDir . '/' . $file)) { |
| 47 |
continue; |
| 48 |
} |
| 49 |
|
| 50 |
$dir = $file; |
| 51 |
|
| 52 |
// persist dir info |
| 53 |
$fileEntry = new FileEntry(); |
| 54 |
$fileEntry->parent_id = $parentFileEntry->id; |
| 55 |
$fileEntry->name = $dir; |
| 56 |
$fileEntry->type = FileEntry::TYPE_DIR; |
| 57 |
$fileEntry->size = 0; |
| 58 |
$this->fileEntryRepository->createOrUpdate($fileEntry); |
| 59 |
|
| 60 |
$this->queue->push((new ScanDirForSubDirsJob($fileEntry->id, $dir))->toArray()); |
| 61 |
} |
| 62 |
if ($this->queue->size() == 0) { |
| 63 |
$this->queue->push((new PhaseCoordinatorJob())->toArray()); |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
public function toArray() { |
| 68 |
return ['type' => self::class, 'args' => [$this->parentId, $this->parentName]]; |
| 69 |
} |
| 70 |
|
| 71 |
public function toDescription(): string { |
| 72 |
return sprintf('Scanning dir: %s', $this->parentName); |
| 73 |
} |
| 74 |
|
| 75 |
} |
| 76 |
|