| 1234567891011121314151617181920212223242526272829303132333435363738 |
- <?php
- namespace App\Support;
- /**
- * 周报产物仅保留「带时间戳」文件名:teacher-weekly-stats-{Y-m-d}_{His}.pdf
- * 「打开最新」通过按修改时间选取目录内匹配该模式的文件。
- */
- final class TeacherWeeklyStatsReportPaths
- {
- private const PDF_PATTERN = '/^teacher-weekly-stats-\d{4}-\d{2}-\d{2}_\d{6}\.pdf$/';
- public static function reportsDirectory(): string
- {
- return storage_path('app/reports');
- }
- /** 最近一次生成的带时间戳 PDF(按 mtime),无则 null */
- public static function resolveLatestPdfPath(): ?string
- {
- $dir = self::reportsDirectory();
- if (! is_dir($dir)) {
- return null;
- }
- $files = glob($dir.'/teacher-weekly-stats-*.pdf') ?: [];
- $files = array_values(array_filter($files, static fn ($p) => preg_match(self::PDF_PATTERN, basename((string) $p))));
- if ($files === []) {
- return null;
- }
- usort($files, static fn ($a, $b) => filemtime((string) $b) <=> filemtime((string) $a));
- $path = $files[0];
- return is_string($path) && is_file($path) ? $path : null;
- }
- }
|