getComposer()->getPackage()->getExtra()[Builder::NAME] ?? []; $templateName = $conf['templates'][$buildName] ?? ''; if (empty($templateName)) { $output->writeln(sprintf( 'Could not build file %s: no such template in composer.json extra section', $buildName )); return ["", 1]; } return [$templateName, 0]; } /** * Perform template rendering. * * @param \Twig\TemplateWrapper $wrapper * A Twig user-space template wrapper. * @param array $context * The data context with which to perform the rendering. * @param string $destination * The path where to write the rendering result. * * @return array * - string message * - int status: 0 on success, other values on errors. */ protected function render( TemplateWrapper $wrapper, array $context, string $destination ): array { if (file_exists($destination)) { $ok = unlink($destination); if (!$ok) { return [sprintf("Could not remove old %s file", $destination), 1]; } } $rendered = $wrapper->render($context); $ok = file_put_contents($destination, $rendered, LOCK_EX); if (!$ok) { return [sprintf('Could not write new %s file', $destination), 2]; } return ["", 0]; } /** * Prepare the template and its parameters as obtained from configuration. * * @param \Symfony\Component\Console\Input\InputInterface $input * Command input. * @param \Symfony\Component\Console\Output\OutputInterface $output * Command output. * @param string $buildName * Machine name of build process. * * @return array * - TemplateWrapper|NULL: Twig template wrapper * - array: template parameters * - string: error message * - int: error, 0 if OK. If non-zero, only the error message is reliable. * * @throws \Twig\Error\LoaderError * @throws \Twig\Error\RuntimeError * @throws \Twig\Error\SyntaxError */ protected function prepare( InputInterface $input, OutputInterface $output, string $buildName ): array { [ $templateName, $err, ] = $this->validateTemplateName($input, $output, $buildName); if ($err) { return [NULL, [], "Could not validate template name", 1]; }; $settingsPath = $this->getSettingsPath(); $templatePath = "${settingsPath}/${templateName}"; $realTemplatePath = realpath($templatePath); if (empty($realTemplatePath)) { return [ NULL, [], sprintf("Could not load template %s: no such file", $templateName), 2, ]; } [$params, $msg, $err] = $this->getParams(); if ($err) { return [NULL, [], $msg, $err]; }; $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); return [$wrapper, $params, "", 0]; } /** * Get parameters as obtained from configuration. * * @return array * - array: parameters * - string: error message * - int: error, 0 if OK. If non-zero, only the error message is reliable. */ public function getParams(): array { $settingsPath = $this->getSettingsPath(); $paramsPath = "${settingsPath}/merged.params.local.yml"; $realParamsPath = realpath($paramsPath); if (empty($realParamsPath)) { return [ [], sprintf("Could not load parameters %s: no such file", $paramsPath), 3, ]; } $yaml = new Yaml(); try { return [$yaml->parseFile($realParamsPath), '', 0]; } catch (ParseException $e) { return [ [], sprintf("Could not parse %s: %s", $realParamsPath, $e->getMessage()), 4, ]; } } }