|
@@ -1,6 +1,6 @@
|
|
|
<?php
|
|
|
|
|
|
-declare(strict_types = 1);
|
|
|
+declare(strict_types=1);
|
|
|
|
|
|
namespace Fgm\Drupal\Composer;
|
|
|
|
|
@@ -49,14 +49,13 @@ abstract class BaseBuilderCommand extends BaseCommand {
|
|
|
* - string Template name
|
|
|
* - int Error
|
|
|
*/
|
|
|
- public function validateTemplateName(InputInterface $input, OutputInterface $output): array {
|
|
|
- $file = $input->getArgument(static::ARG_FILE);
|
|
|
+ public function validateTemplateName(InputInterface $input, OutputInterface $output, string $buildName): array {
|
|
|
$conf = $this->getComposer()->getPackage()->getExtra()[Builder::NAME] ?? [];
|
|
|
- $templateName = $conf['templates'][$file] ?? '';
|
|
|
+ $templateName = $conf['templates'][$buildName] ?? '';
|
|
|
if (empty($templateName)) {
|
|
|
$output->writeln(sprintf(
|
|
|
'Could not build file %s: no such template in composer.json extra section',
|
|
|
- $file
|
|
|
+ $buildName
|
|
|
));
|
|
|
return ["", 1];
|
|
|
}
|
|
@@ -105,6 +104,8 @@ abstract class BaseBuilderCommand extends BaseCommand {
|
|
|
* 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
|
|
@@ -116,8 +117,11 @@ abstract class BaseBuilderCommand extends BaseCommand {
|
|
|
* @throws \Twig\Error\RuntimeError
|
|
|
* @throws \Twig\Error\SyntaxError
|
|
|
*/
|
|
|
- protected function prepare(InputInterface $input, OutputInterface $output): array {
|
|
|
- [$templateName, $err] = $this->validateTemplateName($input, $output);
|
|
|
+ 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];
|
|
|
};
|
|
@@ -126,16 +130,46 @@ abstract class BaseBuilderCommand extends BaseCommand {
|
|
|
$templatePath = "${settingsPath}/${templateName}";
|
|
|
$realTemplatePath = realpath($templatePath);
|
|
|
if (empty($realTemplatePath)) {
|
|
|
- return [NULL, [],
|
|
|
+ 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() {
|
|
|
+ $settingsPath = $this->getSettingsPath();
|
|
|
$paramsPath = "${settingsPath}/merged.params.local.yml";
|
|
|
$realParamsPath = realpath($paramsPath);
|
|
|
if (empty($realParamsPath)) {
|
|
|
- return [NULL, [],
|
|
|
+ return [
|
|
|
+ [],
|
|
|
sprintf("Could not load parameters %s: no such file", $paramsPath),
|
|
|
3,
|
|
|
];
|
|
@@ -143,25 +177,15 @@ abstract class BaseBuilderCommand extends BaseCommand {
|
|
|
|
|
|
$yaml = new Yaml();
|
|
|
try {
|
|
|
- $params = $yaml->parseFile($realParamsPath);
|
|
|
+ return [$yaml->parseFile($realParamsPath), '', 0];
|
|
|
}
|
|
|
catch (ParseException $e) {
|
|
|
- return [NULL, [],
|
|
|
+ return [
|
|
|
+ [],
|
|
|
sprintf("Could not parse %s: %s", $realParamsPath, $e->getMessage()),
|
|
|
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);
|
|
|
- return [$wrapper, $params, "", 0];
|
|
|
}
|
|
|
|
|
|
}
|