make(Kernel::class)->bootstrap(); $database = $argv[1] ?? ''; $root = realpath($argv[3] ?? ''); if (! app()->environment('testing') || DB::getDriverName() !== 'pgsql' || DB::connection()->getDatabaseName() !== $database || DB::connection()->getConfig('host') !== '127.0.0.1' || ! preg_match('/^compras_receipts_test_[a-f0-9]{12}$/D', $database) || $root === false || ! is_dir($root)) { throw new LogicException('Real-source preparation requires a supplied corpus and disposable local PostgreSQL QA.'); } require_once __DIR__.'/../Support/ReceiptFixture.php'; require_once __DIR__.'/../Support/SupplyFixture.php'; config(['import-legal.qa_enabled' => true, 'import-tariffs.qa_enabled' => true]); $directory = storage_path('framework/testing/legal-corpus-'.$database); Storage::set('local', Storage::build(['driver' => 'local', 'root' => $directory, 'throw' => true])); $inventory = []; foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($root, FilesystemIterator::SKIP_DOTS)) as $entry) { if ($entry->isFile()) { $inventory[] = ['path' => str_replace('\\', '/', substr($entry->getPathname(), strlen($root) + 1)), 'bytes' => $entry->getSize()]; } } usort($inventory, fn ($a, $b) => strcmp($a['path'], $b['path'])); $patterns = [ 'Legislacao/2022/12 Dezembro/series-i/br-series-i-29-12-2022-*.pdf' => 'BR I Série 251, 29/12/2022 — Leis 17/2022 e 19/2022, exemplar integral', 'novas/BR_248_I_SERIE_SUPLEMENTO_2025.pdf' => 'BR I Série 248, suplemento de 29/12/2025 — Leis 7/2025 a 12/2025', 'lei-nr-28-2022-de-29-de-dezembro-lei-cambial.pdf' => 'BR I Série 251, suplemento de 29/12/2022 — Lei 28/2022, Lei Cambial', 'Legislacao/2024/03 */series-i/br-series-i-20-03-2024-*.pdf' => 'BR I Série 57, 20/03/2024 — Aviso 3/GBM/2024, normas e procedimentos cambiais', ]; $f = receiptFixture(); supplyGrant($f['issuer'], ['procurement', 'import_customs']); auth()->guard()->setUser($f['issuer']); $fingerprint = fn () => [DB::table('budget_entries')->get()->toJson(), DB::table('invoices')->get()->toJson(), DB::table('stock_movements')->get()->toJson()]; $before = $fingerprint(); $sources = []; try { foreach ($patterns as $pattern => $reference) { $matches = glob(str_replace('\\', '/', $root).'/'.$pattern); if ($matches === false || count($matches) !== 1) { throw new RuntimeException('The selected corpus reference is missing or ambiguous: '.$reference); } $path = $matches[0]; $checksum = hash_file('sha256', $path); $input = ['operation_key' => (string) Str::uuid(), 'action' => 'source', 'version' => 0, 'official_reference' => $reference, 'reason' => 'Fonte oficial real partilhada pelo proprietário; preparação exclusivamente em empresa e base QA sintéticas, sem publicar regras']; $file = new UploadedFile($path, basename($path), 'application/pdf', UPLOAD_ERR_OK, true); $record = app(ImportLegalRules::class)->save($f['issuer'], $input, $file); $resolved = app(ImportLegalRules::class)->source($f['issuer'], $f['r'], $record->id); if (hash('sha256', app(ImportEvidence::class)->bytes($resolved)) !== $checksum || hash_file('sha256', $path) !== $checksum || app(ImportLegalRules::class)->save($f['issuer'], $input, $file)->id !== $record->id) { throw new RuntimeException('The original, private source or idempotent replay differs.'); } $sources[] = ['reference' => $reference, 'relative_path' => str_replace('\\', '/', substr($path, strlen($root) + 1)), 'bytes' => filesize($path), 'sha256' => $checksum, 'native_authorized_download_verified' => true]; } if ($fingerprint() !== $before || SupplyRecord::query()->where('kind', 'import_legal_rule')->exists()) { throw new RuntimeException('Registering sources must not publish rules or create financial/stock effects.'); } echo json_encode(['shared_corpus_files_inventoried' => count($inventory), 'inventory_metadata_sha256' => hash('sha256', json_encode($inventory, JSON_THROW_ON_ERROR)), 'real_official_sources_prepared' => $sources, 'rules_published' => 0, 'originals_unchanged' => true, 'financial_and_stock_unchanged' => true, 'synthetic_qa_organization_only' => true], JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR).PHP_EOL; } finally { $resolved = realpath($directory); $boundary = realpath(storage_path('framework/testing')); if ($resolved !== false && $boundary !== false && str_starts_with(str_replace('\\', '/', $resolved), str_replace('\\', '/', $boundary).'/legal-corpus-compras_receipts_test_')) { File::deleteDirectory($resolved); } }