app->singleton('db', fn ($app): DatabaseManager => new class($app, $app['db.factory'], $file) extends DatabaseManager { public function __construct($app, $factory, private string $file) { parent::__construct($app, $factory); } public function connection($name = null) { $name ??= $this->getDefaultConnection(); [$database] = $this->parseConnectionName($name); if ($database !== 'sandbox') { throw SandboxViolation::record('database', "Verbindung [{$name}] ist im Prozess-Tester nicht erlaubt."); } return parent::connection($name); } /** Every (re)connected sandbox connection gets the guard: each statement must go to the run's own file. */ protected function configure(Connection $connection, $type) { $connection = parent::configure($connection, $type); $expected = realpath($this->file) ?: $this->file; $connection->beforeExecuting(function (string $query, array $bindings, Connection $connection) use ($expected): void { $actual = (string) $connection->getConfig('database'); if ((realpath($actual) ?: $actual) !== $expected) { throw SandboxViolation::record('database', 'Abfrage an eine andere Datenbankdatei: '.basename($actual)); } }); return $connection; } }); $this->app->singleton('filesystem', fn ($app): FilesystemManager => new class($app) extends FilesystemManager { public function build($config) { $root = is_array($config) ? (string) ($config['root'] ?? '') : ''; if (! str_starts_with(str_replace('\\', '/', $root), str_replace('\\', '/', (string) config('sandbox.directory')))) { throw SandboxViolation::record('file', 'Dateiablage außerhalb des Laufverzeichnisses ist im Prozess-Tester nicht erlaubt.'); } return parent::build($config); } }); // Mail and cache come from deferred providers, which would register their managers again on first use: they are // loaded and replaced just before the application boots, when the list of deferred services is complete. $this->app->booting(function (): void { foreach (['mail.manager', 'cache'] as $service) { $this->app->loadDeferredProvider($service); } $this->app->singleton('mail.manager', fn ($app): MailManager => new class($app) extends MailManager { public function mailer($name = null) { if (($name ?? $this->getDefaultDriver()) !== 'array') { throw SandboxViolation::record('mail', "Mailer [{$name}] ist im Prozess-Tester nicht erlaubt."); } return parent::mailer($name); } }); $this->app->singleton('cache', fn ($app): CacheManager => new class($app) extends CacheManager { public function store($name = null) { if (($name ?? $this->getDefaultDriver()) !== 'array') { throw SandboxViolation::record('cache', "Cache [{$name}] ist im Prozess-Tester nicht erlaubt."); } return parent::store($name); } }); }); $this->app->singleton(MailboxReader::class, fn (): MailboxReader => new SandboxMailboxReader); $this->app->singleton(ChannelRegistry::class, fn (): ChannelRegistry => new SandboxChannelRegistry); } public function boot(): void { // No network: only the scenario's scripted answers (ProcessSandboxDriver::script) are served. Http::preventStrayRequests(); Http::fake(function (Request $request) { $answer = SandboxNetwork::answer($request); if ($answer === null) { throw SandboxViolation::record('network', 'Netzzugriff ohne Szenario-Antwort: '.$request->method().' '.parse_url($request->url(), PHP_URL_HOST)); } EffectLedger::record('network', ['method' => $request->method(), 'host' => parse_url($request->url(), PHP_URL_HOST), 'scripted' => true]); return $answer; }); } }