TeacherWeeklyStatsReportPaths.php 1.1 KB

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