Skip to content

Commit b8ba1ce

Browse files
committed
Add configuration
1 parent 904454a commit b8ba1ce

File tree

2 files changed

+98
-32
lines changed

2 files changed

+98
-32
lines changed

src/Commands/DockerGenerateCommand.php

Lines changed: 45 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use BlameButton\LaravelDockerBuilder\Commands\GenerateQuestions\PhpExtensionsQuestion;
1313
use BlameButton\LaravelDockerBuilder\Commands\GenerateQuestions\PhpVersionQuestion;
1414
use BlameButton\LaravelDockerBuilder\Exceptions\InvalidOptionValueException;
15+
use BlameButton\LaravelDockerBuilder\Objects\Configuration;
1516
use BlameButton\LaravelDockerBuilder\Traits\InteractsWithTwig;
1617
use Symfony\Component\Console\Input\InputOption;
1718

@@ -26,25 +27,20 @@ class DockerGenerateCommand extends BaseCommand
2627
public function handle(): int
2728
{
2829
try {
29-
$phpVersion = app(PhpVersionQuestion::class)->getAnswer($this);
30-
$phpExtensions = app(PhpExtensionsQuestion::class)->getAnswer($this, $phpVersion);
31-
$artisanOptimize = app(ArtisanOptimizeQuestion::class)->getAnswer($this);
32-
$nodePackageManager = app(NodePackageManagerQuestion::class)->getAnswer($this);
33-
$nodeBuildTool = $nodePackageManager ? app(NodeBuildToolQuestion::class)->getAnswer($this) : false;
30+
$config = new Configuration(
31+
phpVersion: $phpVersion = app(PhpVersionQuestion::class)->getAnswer($this),
32+
phpExtensions: app(PhpExtensionsQuestion::class)->getAnswer($this, $phpVersion),
33+
artisanOptimize: app(ArtisanOptimizeQuestion::class)->getAnswer($this),
34+
nodePackageManager: $nodePackageManager = app(NodePackageManagerQuestion::class)->getAnswer($this),
35+
nodeBuildTool: $nodePackageManager ? app(NodeBuildToolQuestion::class)->getAnswer($this) : false,
36+
);
3437
} catch (InvalidOptionValueException $exception) {
3538
$this->error($exception->getMessage());
3639

3740
return self::INVALID;
3841
}
3942

40-
$this->info('Configuration:');
41-
$this->table(['Key', 'Value'], [
42-
['PHP version', "<comment>$phpVersion</comment>"],
43-
['PHP extensions', implode(', ', $phpExtensions)],
44-
['Artisan Optimize', '<comment>'.json_encode($artisanOptimize).'</comment>'],
45-
['Node Package Manager', NodePackageManager::name($nodePackageManager)],
46-
['Node Build Tool', $nodePackageManager ? NodeBuildTool::name($nodeBuildTool) : 'None'],
47-
]);
43+
$this->printConfigurationTable($config);
4844
$this->newLine();
4945

5046
if (! $this->option('no-interaction') && ! $this->confirm('Does this look correct?', true)) {
@@ -53,39 +49,56 @@ public function handle(): int
5349
return self::SUCCESS;
5450
}
5551

56-
$this->saveDockerfiles([
57-
'php_version' => $phpVersion,
58-
'php_extensions' => implode(' ', $phpExtensions),
59-
'artisan_optimize' => $artisanOptimize,
60-
'node_package_manager' => $nodePackageManager,
61-
'node_build_tool' => $nodeBuildTool,
62-
]);
52+
$this->saveDockerfiles($config);
6353
$this->newLine();
6454

65-
$command = array_filter([
66-
'php', 'artisan', 'docker:generate',
67-
'-n', // --no-interaction
68-
'-p '.$phpVersion, // --php-version
69-
'-e '.implode(',', $phpExtensions), // --php-extensions
70-
$artisanOptimize ? '-o' : null, // --optimize
71-
$nodePackageManager ? '-m '.$nodePackageManager : null, // --node-package-manager
72-
$nodePackageManager ? '-b '.$nodeBuildTool : null, // --node-build-tool
73-
]);
74-
7555
$this->info('Command to generate above configuration:');
76-
$this->comment(sprintf(' %s', implode(' ', $command)));
56+
$this->comment(sprintf(' %s', implode(' ', $config->getCommand())));
7757

7858
return self::SUCCESS;
7959
}
8060

81-
private function saveDockerfiles(array $context): void
61+
public function printConfigurationTable(Configuration $config): void
62+
{
63+
$this->info('Configuration:');
64+
65+
$this->table(['Key', 'Value'], [
66+
['PHP version',
67+
'<comment>'.$config->getPhpVersion().'</comment>',
68+
],
69+
['PHP extensions',
70+
implode(', ', $config->getPhpExtensions()),
71+
],
72+
['Artisan Optimize',
73+
'<comment>'.json_encode($config->isArtisanOptimize()).'</comment>',
74+
],
75+
['Node Package Manager',
76+
NodePackageManager::name($config->getNodePackageManager()),
77+
],
78+
['Node Build Tool',
79+
$config->getNodePackageManager()
80+
? NodeBuildTool::name($config->getNodeBuildTool())
81+
: 'None',
82+
],
83+
]);
84+
}
85+
86+
private function saveDockerfiles(Configuration $config): void
8287
{
8388
if (! is_dir($dir = base_path('.docker'))) {
8489
mkdir($dir);
8590
}
8691

8792
$this->info('Saving Dockerfiles:');
8893

94+
$context = [
95+
'php_version' => $config->getPhpVersion(),
96+
'php_extensions' => implode(' ', $config->getPhpExtensions()),
97+
'artisan_optimize' => $config->isArtisanOptimize(),
98+
'node_package_manager' => $config->getNodePackageManager(),
99+
'node_build_tool' => $config->getNodeBuildTool(),
100+
];
101+
89102
$dockerfiles = [
90103
'php.dockerfile' => $this->render('php.dockerfile.twig', $context),
91104
'nginx.dockerfile' => $this->render('nginx.dockerfile.twig', $context),

src/Objects/Configuration.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
namespace BlameButton\LaravelDockerBuilder\Objects;
4+
5+
class Configuration
6+
{
7+
public function __construct(
8+
private string $phpVersion,
9+
private array $phpExtensions,
10+
private bool $artisanOptimize,
11+
private string|false $nodePackageManager,
12+
private string|false $nodeBuildTool,
13+
) {
14+
}
15+
16+
public function getPhpVersion(): string
17+
{
18+
return $this->phpVersion;
19+
}
20+
21+
public function getPhpExtensions(): array
22+
{
23+
return $this->phpExtensions;
24+
}
25+
26+
public function isArtisanOptimize(): bool
27+
{
28+
return $this->artisanOptimize;
29+
}
30+
31+
public function getNodePackageManager(): string|false
32+
{
33+
return $this->nodePackageManager;
34+
}
35+
36+
public function getNodeBuildTool(): string|false
37+
{
38+
return $this->nodeBuildTool;
39+
}
40+
41+
public function getCommand(): array
42+
{
43+
return array_filter([
44+
'php', 'artisan', 'docker:generate',
45+
'-n', // --no-interaction
46+
'-p '.$this->getPhpVersion(), // --php-version
47+
'-e '.implode(',', $this->getPhpExtensions()), // --php-extensions
48+
$this->isArtisanOptimize() ? '-o' : null, // --optimize
49+
$this->getNodePackageManager() ? '-m '.$this->getNodePackageManager() : null, // --node-package-manager
50+
$this->getNodePackageManager() ? '-b '.$this->getNodeBuildTool() : null, // --node-build-tool
51+
]);
52+
}
53+
}

0 commit comments

Comments
 (0)