BaseBuilderCommand.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. declare(strict_types = 1);
  3. namespace Fgm\Drupal\Composer;
  4. use Composer\Command\BaseCommand;
  5. use Symfony\Component\Console\Input\InputInterface;
  6. use Symfony\Component\Console\Output\OutputInterface;
  7. abstract class BaseBuilderCommand extends BaseCommand
  8. {
  9. /**
  10. * The name of the argument defining the file to generate.
  11. */
  12. const ARG_FILE = 'file';
  13. protected function getBuilderConfig(): array
  14. {
  15. $conf = $this->getComposer()->getPackage()->getExtra()[Builder::NAME] ?? [];
  16. return $conf;
  17. }
  18. protected function getSettingsPath(): string
  19. {
  20. $settingsPath = getcwd() . "/settings";
  21. return $settingsPath;
  22. }
  23. /**
  24. * @param \Symfony\Component\Console\Input\InputInterface $input
  25. * @param \Symfony\Component\Console\Output\OutputInterface $output
  26. *
  27. * @return array
  28. * - string Template name
  29. * - int Error
  30. */
  31. public function validateTemplateName(InputInterface $input, OutputInterface $output): array {
  32. $file = $input->getArgument(static::ARG_FILE);
  33. $conf = $this->getBuilderConfig();
  34. $templateName = $conf['templates'][$file] ?? '';
  35. if (empty($templateName)) {
  36. $output->writeln(sprintf(
  37. 'Could not build file %s: no such template in composer.json extra section',
  38. $file
  39. ));
  40. return ["", 1];
  41. }
  42. return [$templateName, 0];
  43. }
  44. }