| 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 |
$pattern = $realDir . '/*'; |
| 33 |
$this->log("Scanning " . $realDir . " for sub directories..."); |
| 34 |
|
| 35 |
foreach (glob($pattern, GLOB_ONLYDIR) as $dir) { |
| 36 |
|
| 37 |
// persist dir info |
| 38 |
$fileEntry = new FileEntry(); |
| 39 |
$fileEntry->parent_id = $parentFileEntry->id; |
| 40 |
$fileEntry->name = basename($dir); |
| 41 |
$fileEntry->type = FileEntry::TYPE_DIR; |
| 42 |
$fileEntry->size = 0; |
| 43 |
$this->fileEntryRepository->createOrUpdate($fileEntry); |
| 44 |
|
| 45 |
$this->queue->push((new ScanDirForSubDirsJob($fileEntry->id, $dir))->toArray()); |
| 46 |
} |
| 47 |
$this->queue->push((new PhaseCoordinatorJob())->toArray()); |
| 48 |
} |
| 49 |
|
| 50 |
public function toArray() { |
| 51 |
return ['type' => self::class, 'args' => [$this->parentId, $this->parentName]]; |
| 52 |
} |
| 53 |
|
| 54 |
public function toDescription(): string { |
| 55 |
return sprintf('Scanning dir: %s', $this->parentName); |
| 56 |
} |
| 57 |
|
| 58 |
} |
| 59 |
|