2026-04-17 16:30:52 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace Kiri\Di;
|
|
|
|
|
|
|
|
|
|
class ScanManifest
|
|
|
|
|
{
|
|
|
|
|
private array $entries = [];
|
|
|
|
|
|
2026-06-12 23:57:19 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $path
|
|
|
|
|
* @param int $mtime
|
|
|
|
|
* @param array $classes
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2026-04-17 16:30:52 +08:00
|
|
|
public function set(string $path, int $mtime, array $classes): void
|
|
|
|
|
{
|
|
|
|
|
$this->entries[$path] = [
|
|
|
|
|
'mtime' => $mtime,
|
|
|
|
|
'classes' => array_values(array_unique($classes)),
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-12 23:57:19 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $path
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2026-04-17 16:30:52 +08:00
|
|
|
public function has(string $path): bool
|
|
|
|
|
{
|
|
|
|
|
return isset($this->entries[$path]);
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-12 23:57:19 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $path
|
|
|
|
|
* @return int|null
|
|
|
|
|
*/
|
2026-04-17 16:30:52 +08:00
|
|
|
public function getMtime(string $path): ?int
|
|
|
|
|
{
|
|
|
|
|
return $this->entries[$path]['mtime'] ?? null;
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-12 23:57:19 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $path
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
2026-04-17 16:30:52 +08:00
|
|
|
public function getClasses(string $path): array
|
|
|
|
|
{
|
|
|
|
|
return $this->entries[$path]['classes'] ?? [];
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-12 23:57:19 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $path
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
2026-04-17 16:30:52 +08:00
|
|
|
public function remove(string $path): array
|
|
|
|
|
{
|
|
|
|
|
$classes = $this->getClasses($path);
|
|
|
|
|
unset($this->entries[$path]);
|
|
|
|
|
return $classes;
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-12 23:57:19 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
2026-04-17 16:30:52 +08:00
|
|
|
public function all(): array
|
|
|
|
|
{
|
|
|
|
|
return $this->entries;
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-12 23:57:19 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string|null $prefix
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
2026-04-17 16:30:52 +08:00
|
|
|
public function paths(?string $prefix = null): array
|
|
|
|
|
{
|
|
|
|
|
if ($prefix === null) {
|
|
|
|
|
return array_keys($this->entries);
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-24 20:21:29 +08:00
|
|
|
$keys = array_keys($this->entries);
|
|
|
|
|
$filtered = array_filter($keys, fn(string $path) => str_starts_with($path, $prefix));
|
|
|
|
|
return array_values($filtered);
|
2026-04-17 16:30:52 +08:00
|
|
|
}
|
|
|
|
|
|
2026-06-12 23:57:19 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param array $entries
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2026-04-17 16:30:52 +08:00
|
|
|
public function fromArray(array $entries): void
|
|
|
|
|
{
|
|
|
|
|
$this->entries = [];
|
|
|
|
|
foreach ($entries as $path => $entry) {
|
|
|
|
|
$mtime = (int)($entry['mtime'] ?? 0);
|
|
|
|
|
$classes = is_array($entry['classes'] ?? null) ? $entry['classes'] : [];
|
|
|
|
|
$this->set($path, $mtime, $classes);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|