runInternal($context); } catch (\Throwable $e) { // 본 보완 로직은 실패해도 업그레이드 본체를 중단하지 않는다. $context->logger->warning(sprintf( '[7.0.0-beta.6] BackfillNewFilesManifest 실패 (계속 진행): %s', $e->getMessage(), )); Log::warning('beta.6 BackfillNewFilesManifest 실패', [ 'error' => $e->getMessage(), 'trace' => $e->getTraceAsString(), ]); } } private function runInternal(UpgradeContext $context): void { $backupsDir = storage_path('app'.DIRECTORY_SEPARATOR.'core_backups'); if (! is_dir($backupsDir)) { $context->logger->info('[7.0.0-beta.6] core_backups 디렉토리 부재 — manifest 사후 작성 skip'); return; } $latestBackup = $this->findLatestBackupDir($backupsDir); if ($latestBackup === null) { $context->logger->info('[7.0.0-beta.6] core_backups 비어 있음 — manifest 사후 작성 skip'); return; } $manifestPath = $latestBackup.DIRECTORY_SEPARATOR.self::MANIFEST_FILENAME; if (file_exists($manifestPath)) { $context->logger->info(sprintf( '[7.0.0-beta.6] manifest 이미 존재 — noop (path=%s)', $manifestPath, )); return; } // 신 버전 디스크의 config 를 require — 부모 메모리의 stale config 와 무관한 SSoT $config = $this->loadCoreUpdateConfigFromDisk(); $targets = $config['targets']; $protectedPaths = $config['protected_paths']; $excludes = $config['excludes']; $activeRoot = base_path(); $newFiles = []; $newDirs = []; $protectedSet = $this->normalizeProtectedSet($protectedPaths); $excludeSet = array_values(array_filter(array_map('trim', $excludes))); foreach ($targets as $target) { $target = trim((string) $target); if ($target === '') { continue; } if ($this->isWithinProtectedPath($target, $protectedSet)) { continue; } $activeTargetPath = $activeRoot.DIRECTORY_SEPARATOR.$target; if (! file_exists($activeTargetPath)) { continue; } if (is_file($activeTargetPath) && ! is_link($activeTargetPath)) { $backupItem = $latestBackup.DIRECTORY_SEPARATOR.$target; if (! file_exists($backupItem)) { $newFiles[] = $this->normalizeRelative($target); } continue; } if (! is_dir($activeTargetPath)) { continue; } $iterator = new RecursiveIteratorIterator( new RecursiveDirectoryIterator($activeTargetPath, FilesystemIterator::SKIP_DOTS), RecursiveIteratorIterator::SELF_FIRST, ); foreach ($iterator as $item) { /** @var SplFileInfo $item */ $absolute = $item->getPathname(); $relative = $this->normalizeRelative( $target.'/'.ltrim(substr($absolute, strlen($activeTargetPath)), DIRECTORY_SEPARATOR.'/'), ); if ($this->matchesExcludes($relative, $excludeSet)) { continue; } if ($this->isWithinProtectedPath($relative, $protectedSet)) { continue; } $backupItemPath = $latestBackup.DIRECTORY_SEPARATOR.str_replace('/', DIRECTORY_SEPARATOR, $relative); if ($item->isDir() && ! $item->isLink()) { if (! is_dir($backupItemPath)) { $newDirs[] = $relative; } } elseif ($item->isFile()) { if (! file_exists($backupItemPath)) { $newFiles[] = $relative; } } } } sort($newFiles, SORT_STRING); sort($newDirs, SORT_STRING); $manifest = [ 'version' => self::MANIFEST_SCHEMA_VERSION, 'created_at' => date('c'), 'from_version' => $context->fromVersion, 'to_version' => $context->toVersion, 'new_files' => $newFiles, 'new_dirs' => $newDirs, ]; File::put( $manifestPath, json_encode($manifest, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE), ); $context->logger->info(sprintf( '[7.0.0-beta.6] manifest 사후 작성 완료 — backup=%s, new_files=%d, new_dirs=%d', $latestBackup, count($newFiles), count($newDirs), )); } /** * `storage/app/core_backups/` 의 가장 최근 백업 디렉토리를 식별. * * CoreBackupHelper::listBackups() 직접 호출 금지 — 로컬 scandir 로 동일 결과 산출. */ private function findLatestBackupDir(string $backupsDir): ?string { $entries = @scandir($backupsDir); if ($entries === false) { return null; } $candidates = []; foreach ($entries as $entry) { if ($entry === '.' || $entry === '..') { continue; } $full = $backupsDir.DIRECTORY_SEPARATOR.$entry; if (! is_dir($full)) { continue; } $candidates[$full] = (int) @filemtime($full); } if ($candidates === []) { return null; } arsort($candidates, SORT_NUMERIC); return (string) array_key_first($candidates); } /** * 신 버전 디스크의 `config/app.php` 에서 update 관련 설정 추출. * * 부모 메모리의 stale `config()` 캐시를 우회하기 위해 디스크의 config 를 직접 require. * env() 호출이 안전한 default 값을 반환하므로 신 버전 적용 후의 fresh 값을 얻을 수 있다. * * @return array{targets:array, protected_paths:array, excludes:array} */ private function loadCoreUpdateConfigFromDisk(): array { // 부모 메모리의 config 사용을 우회 — 디스크 SSoT 의 신 버전 config 로드 $appConfig = require base_path('config'.DIRECTORY_SEPARATOR.'app.php'); $update = $appConfig['update'] ?? []; return [ 'targets' => (array) ($update['targets'] ?? []), 'protected_paths' => (array) ($update['protected_paths'] ?? []), 'excludes' => (array) ($update['excludes'] ?? []), ]; } private function normalizeProtectedSet(array $paths): array { $out = []; foreach ($paths as $p) { $p = trim((string) $p); if ($p === '') { continue; } $out[] = $this->normalizeRelative($p); } return $out; } private function isWithinProtectedPath(string $relative, array $protectedSet): bool { $relative = $this->normalizeRelative($relative); foreach ($protectedSet as $p) { if ($p === '') { continue; } if ($relative === $p) { return true; } if (str_starts_with($relative, $p.'/')) { return true; } } return false; } private function matchesExcludes(string $relative, array $excludes): bool { $segments = explode('/', $relative); foreach ($excludes as $exclude) { if ($exclude === '') { continue; } if (str_contains($exclude, '/')) { if ($relative === $exclude || str_starts_with($relative, $exclude.'/')) { return true; } } else { if (in_array($exclude, $segments, true)) { return true; } } } return false; } private function normalizeRelative(string $path): string { $p = str_replace('\\', '/', $path); $p = ltrim($p, '/'); while (str_contains($p, '//')) { $p = str_replace('//', '/', $p); } return $p; } }