Files
compras/integrations/sgf/operational/BootOverlay.php
T
nala 47649bd46c
tests / ci (push) Canceled after 0s
commit compras1 3011
2026-11-30 09:33:36 +02:00

62 lines
3.4 KiB
PHP

<?php
namespace Tempo\Purchasing\Delivery;
final class BootOverlay
{
public static function prepare(string $host): array
{
$result = [];
foreach (['app/Providers/AppServiceProvider.php', 'app/Shared/Context/ModuleNavigation.php'] as $path) {
$source = file_get_contents($host.'/'.$path);
$hash = hash('sha256', $source);
$source = str_replace("\r\n", "\n", $source);
if (str_ends_with($path, 'AppServiceProvider.php')) {
$old = '$this->app->scoped(CompanyContext::class';
$new = "if (is_file(base_path('packages/internal-requisitions/bootstrap.php'))) { require_once base_path('packages/internal-requisitions/bootstrap.php'); }\n ".$old;
if (! str_contains($source, "base_path('packages/internal-requisitions/bootstrap.php')")) {
$source = self::replace($source, $old, $new);
} elseif (substr_count($source, $new) !== 1) {
throw new \RuntimeException('Arranque de Compras divergente; reconciliar antes de preparar.');
}
$boot = "public function boot(): void\n {";
$mounted = $boot."\n if (is_file(base_path('packages/internal-requisitions/bootstrap.php'))) { \$this->loadRoutesFrom(base_path('packages/internal-requisitions/integrations/sgf/operational/routes.php')); }";
if (! str_contains($source, 'packages/internal-requisitions/integrations/sgf/operational/routes.php')) {
$source = self::replace($source, $boot, $mounted);
} elseif (substr_count($source, $mounted) !== 1) {
throw new \RuntimeException('Rotas de Compras divergentes; reconciliar antes de preparar.');
}
} else {
$count = 0;
$source = preg_replace_callback("~('purchasing'\\s*=>\\s*\\[)([^\\[\\]]*)(\\])~", static function (array $match): string {
// Extend only the declared prefix list; preserve every later host capability.
if (! preg_match("~^\\s*'[a-z_]+\\.'(?:\\s*,\\s*'[a-z_]+\\.')*\\s*,?\\s*$~", $match[2])
|| ! str_contains($match[2], "'purchasing_budget.'") || ! str_contains($match[2], "'supplier_payables.'")) {
throw new \RuntimeException('Navegação de Compras divergente; lista explícita requerida.');
}
if (str_contains($match[2], "'purchasing_requisitions.'")) {
return $match[0];
}
return $match[1]."'purchasing_requisitions.', ".$match[2].$match[3];
}, $source, -1, $count);
if ($source === null || $count !== 1) {
throw new \RuntimeException('Navegação de Compras ausente ou ambígua; reconciliar antes de preparar.');
}
}
$result[$path] = ['before_sha256' => $hash, 'after_sha256' => hash('sha256', $source), 'contents' => $source];
}
return $result;
}
private static function replace(string $source, string $old, string $new): string
{
if (substr_count($source, $old) !== 1) {
throw new \RuntimeException('Arranque/navegação do host divergiu; reconciliar antes de preparar.');
}
return str_replace($old, $new, $source);
}
}