/** * Create default encryption middleware. * * @param SyncSettingsContract $settings * @param Application $app * @param BackupCommand|null $command * * @throws PathNotFoundException */ protected function createEncryptionMiddleware(SyncSettingsContract $settings, Application &$app, BackupCommand $command = null) { if ($settings->encrypt() === false) { return; } $command = $this->getCommand($command); try { $encryptionMiddleware = new EncryptionMiddleware($settings->encrypt()); $app->middleware($encryptionMiddleware); } catch (PathNotFoundException $e) { // If we have access to the actual command object we can ask the user // if a new random encryption key should be generated and do this task // for him or her. if (!$command) { throw $e; } $helper = $command->getHelper('question'); $question = new ConfirmationQuestion("<comment>The encryption-key '" . $e->getPath() . "' doesn't exist. Create a new random one?</comment> <info>[y|n]</info>\n", false); if (!$helper->ask($input = $command->getInput(), $output = $command->getOutput(), $question)) { throw $e; } touch($e->getPath()); chmod($e->getPath(), 0600); file_put_contents($e->getPath(), (new Rand())->bytes(32)); $output->writeln(''); $this->createEncryptionMiddleware($settings, $app, $command); } }