@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
|
||||
final class SgfWorkflowExtension
|
||||
{
|
||||
public static function transform(string $php): string
|
||||
{
|
||||
$result = '';
|
||||
foreach (token_get_all($php, TOKEN_PARSE) as $token) {
|
||||
if (! is_array($token)) {
|
||||
$result .= $token;
|
||||
|
||||
continue;
|
||||
}
|
||||
[$type, $text] = $token;
|
||||
if (in_array($type, [T_NAME_QUALIFIED, T_NAME_FULLY_QUALIFIED], true)) {
|
||||
$text = preg_replace('/^(\\\\?)App\\\\/', '$1Tempo\\Purchasing\\Embedded\\', $text);
|
||||
}
|
||||
$result .= $text;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
public static function prepare(string $source, string $base, string $baseHash, string $output, array $updates): array
|
||||
{
|
||||
if (! preg_match('/^[a-f0-9]{64}$/D', $baseHash) || is_link($base) || is_link($output)
|
||||
|| ! hash_equals($baseHash, hash_file('sha256', $base.'/manifest.json'))) {
|
||||
throw new LogicException('Workflow predecessor mismatch.');
|
||||
}
|
||||
$manifest = json_decode(file_get_contents($base.'/manifest.json'), true, 64, JSON_THROW_ON_ERROR);
|
||||
$bodies = [];
|
||||
foreach ($manifest['files'] as $path => $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.');
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user