From 014b6dabeb3ed75eb7bbd9040a1f9c53eea0aaa8 Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Thu, 19 Jan 2023 18:53:42 +0000 Subject: [PATCH] Refactor --- build-config.php | 14 ++++++++---- .../Command/BuildConfigurationCommand.php | 22 ++++++++++++------- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/build-config.php b/build-config.php index e138419..2577bb9 100644 --- a/build-config.php +++ b/build-config.php @@ -2,13 +2,19 @@ use OliverDaviesLtd\BuildConfiguration\Console\Command\BuildConfigurationCommand; use Symfony\Component\Console\Application; +use Symfony\Component\Filesystem\Filesystem; +use Twig\Environment; +use Twig\Loader\FilesystemLoader; require __DIR__ . '/vendor/autoload.php'; -$app = new Application(); +$filesystem = new Filesystem(); +$twig = new Environment(new FilesystemLoader([__DIR__.'/templates'])); -$app->addCommands([ - new BuildConfigurationCommand(), +$application = new Application(); + +$application->addCommands([ + new BuildConfigurationCommand($twig, $filesystem), ]); -$app->run(); +$application->run(); diff --git a/src/Console/Command/BuildConfigurationCommand.php b/src/Console/Command/BuildConfigurationCommand.php index e6f41d4..253039c 100644 --- a/src/Console/Command/BuildConfigurationCommand.php +++ b/src/Console/Command/BuildConfigurationCommand.php @@ -12,7 +12,6 @@ use Symfony\Component\Console\Style\SymfonyStyle; use Symfony\Component\Filesystem\Filesystem; use Symfony\Component\Yaml\Yaml; use Twig\Environment; -use Twig\Loader\FilesystemLoader; #[AsCommand( description: 'Build configuration files', @@ -20,20 +19,27 @@ use Twig\Loader\FilesystemLoader; )] final class BuildConfigurationCommand extends Command { + private const LANGUAGE_PHP = 'php'; + + public function __construct( + private Environment $twig, + private Filesystem $filesystem, + ) { + parent::__construct(); + } + public function execute(InputInterface $input, OutputInterface $output) { - // Find a build.yaml file. + $io = new SymfonyStyle($input, $output); + $buildYaml = Yaml::parseFile(getcwd().'/build.yaml'); - $io = new SymfonyStyle($input, $output); $io->info("Building configuration for {$buildYaml['name']}."); - $twig = new Environment(new FilesystemLoader([__DIR__.'/../../../templates'])); - $output = $twig->render('test.twig', $buildYaml); + if ($buildYaml['language'] === self::LANGUAGE_PHP) { + $this->filesystem->dumpFile('Dockerfile', $this->twig->render('php/Dockerfile.twig', $buildYaml)); + } - $fs = new Filesystem(); - $fs->dumpFile('test.txt', $output); - // return Command::SUCCESS; } }