$hash) { self::path($path); if (is_link($base.'/'.$path) || ! hash_equals($hash, hash_file('sha256', $base.'/'.$path))) { throw new LogicException('Workflow predecessor file mismatch.'); } $bodies[$path] = file_get_contents($base.'/'.$path); } foreach ($updates as $path) { self::path($path); if (! str_starts_with($path, 'app/') || is_link($source.'/'.$path) || ! is_file($source.'/'.$path)) { throw new LogicException('Canonical workflow source missing.'); } $bytes = file_get_contents($source.'/'.$path); $manifest['originals'][$path] = hash('sha256', $bytes); $bodies[$path] = self::transform($bytes); } ksort($bodies); $manifest['files'] = array_map(fn ($bytes) => hash('sha256', $bytes), $bodies); $manifest['predecessor_sha256'] = $baseHash; $manifest['extension_contract'] = 'COMPRAS-SGF-EXECUTION-v1'; $bytes = json_encode($manifest, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR)."\n"; $hash = hash('sha256', $bytes); $directory = $output.'/'.substr($hash, 0, 16).'/bundle'; foreach ($bodies + ['manifest.json' => $bytes] as $path => $body) { $file = $directory.'/'.$path; if (is_link($file) || (is_file($file) && ! hash_equals(hash('sha256', $body), hash_file('sha256', $file)))) { throw new LogicException('Immutable workflow candidate differs.'); } if (! is_dir(dirname($file))) { mkdir(dirname($file), 0700, true); } if (! is_file($file)) { file_put_contents($file, $body); } if (! hash_equals(hash('sha256', $body), hash_file('sha256', $file))) { throw new LogicException('Incomplete workflow candidate.'); } } return ['directory' => $directory, 'sha256' => $hash, 'predecessor_sha256' => $baseHash, 'updated_sources' => array_intersect_key($manifest['originals'], array_flip($updates))]; } private static function path(string $path): void { if (! preg_match('~^(app|fortify)/[A-Za-z0-9_/]+\.php$~D', $path)) { throw new LogicException('Unsafe workflow path.'); } } }