Files
compras/tests/Support/sgf-composition-clone.php
T
2026-11-02 08:08:05 +02:00

51 lines
2.3 KiB
PHP

<?php
/** Code/dependencies only: no .env, cache, accounts, documents or runtime data copied. */
function compositionClone(string $host, string $bundle, string $suffix): string
{
$clone = sys_get_temp_dir().'/sgf-composition-'.$suffix;
if (file_exists($clone)) {
throw new RuntimeException('Clone must be new.');
}
mkdir($clone, 0700);
copy($host.'/composer.json', $clone.'/composer.json');
$copy = function (string $source, string $target) use (&$copy): void {
if (! is_dir($target)) {
mkdir($target, 0700, true);
}
foreach (new DirectoryIterator($source) as $file) {
if ($file->isDot() || $file->isLink() || str_starts_with($file->getFilename(), '.')) {
continue;
}
$to = $target.'/'.$file->getFilename();
if ($file->isDir()) {
$copy($file->getPathname(), $to);
} else {
copy($file->getPathname(), $to);
}
}
};
foreach (['app', 'config', 'routes', 'database/migrations', 'packages', 'vendor'] as $directory) {
$copy($host.'/'.$directory, $clone.'/'.$directory);
}
mkdir($clone.'/bootstrap/cache', 0700, true);
foreach (['app.php', 'providers.php'] as $file) {
copy($host.'/bootstrap/'.$file, $clone.'/bootstrap/'.$file);
}
foreach (['public', 'storage/logs', 'storage/framework/sessions', 'storage/framework/views', 'storage/framework/cache'] as $directory) {
mkdir($clone.'/'.$directory, 0700, true);
}
$copy($bundle, $clone.'/packages/internal-requisitions');
$manifest = json_decode(file_get_contents($bundle.'/manifest.json'), true, 32, JSON_THROW_ON_ERROR);
foreach ($manifest['host_overlays'] as $file => $hashes) {
if (hash_file('sha256', $clone.'/'.$file) !== $hashes['before_sha256']) {
throw new RuntimeException('Clone baseline mismatch.');
}
copy($bundle.'/'.$file, $clone.'/'.$file);
}
file_put_contents($clone.'/config/purchasing-operational.php', '<?php return ["enabled"=>getenv("IR_HTTP_ENABLED")==="1","target"=>getenv("IR_HTTP_TARGET"),"scopes"=>[getenv("IR_HTTP_SCOPE")]];');
file_put_contents($clone.'/config/purchasing-access.php', '<?php return ["initialisation_scopes"=>[getenv("IR_HTTP_SCOPE")]];');
return $clone;
}