eventName = $this->getName(); $this ->setName('build:settings') ->setDescription('Step 4: build the *.settings.local.php files.') ->setDefinition( new InputDefinition([ new InputArgument(static::ARG_FILE, InputArgument::OPTIONAL), ]) ) ->setHelp( <<validateTemplateName($input, $output); if ($err) { return 1; }; $settingsPath = $this->getSettingsPath(); $templatePath = "${settingsPath}/${templateName}"; $realTemplatePath = realpath($templatePath); if (empty($realTemplatePath)) { $output->writeln(sprintf("Could not load template %s: no such file", $templateName)); return 2; } $paramsPath = "${settingsPath}/merged.params.local.yml"; $realParamsPath = realpath($paramsPath); if (empty($realParamsPath)) { $output->writeln(sprintf("Could not load parameters %s: no such file", $paramsPath)); return 3; } $yaml = new Yaml(); try { $params = $yaml->parseFile($realParamsPath); } catch (ParseException $e) { $output->writeln(sprintf("Could not parse %s: %s", $realParamsPath, $e->getMessage())); return 4; } $loader = new FilesystemLoader($settingsPath, $settingsPath); $twig = new Environment($loader, [ 'auto_reload' => true, 'cache' => false, 'debug' => true, 'strict_variables' => true, ]); $twig->addExtension(new DebugExtension()); $wrapper = $twig->load($templateName); foreach ($params['sites'] as $name => $siteParams) { foreach (['build', 'run'] as $stage) { $stageSettings = NestedArray::mergeDeepArray([ $siteParams['settings']['both'] ?? [], $siteParams['settings'][$stage] ?? [] ], true); $stageParams = $siteParams; $stageParams['settings'] = $stageSettings; $context = [ 'instance' => $params['instance'], 'name' => $name, 'stage' => $stage, 'site' => $stageParams, ]; $error = $this->render($wrapper, $context, $output); if ($error) { $output->writeln(sprintf("Failed rendering %s settings for site %s", $stage, $name)); return 5; } } } } protected function render(TemplateWrapper $wrapper, array $context, OutputInterface $output): int { $settingsPath = "web/sites/${context['name']}/${context['stage']}.settings.local.php"; if (file_exists($settingsPath)) { $ok = unlink($settingsPath); if (!$ok) { $output->writeln(sprintf( "Could not remove old %s file", $settingsPath )); return 1; } } $rendered = $wrapper->render($context); $ok = file_put_contents($settingsPath, $rendered, LOCK_EX); if (!$ok) { $output->writeln(sprintf('Could not write new %s file', $settingsPath)); return 2; } return 0; } }