Skip to content

Commit 7bf7de8

Browse files
committed
Add artisan optimize choice class
1 parent d82139b commit 7bf7de8

File tree

8 files changed

+130
-49
lines changed

8 files changed

+130
-49
lines changed

bin/docker-build

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ fi
3030
echo "Building [${NGINX_TAG}]"
3131
docker build \
3232
--tag "${NGINX_TAG}" \
33-
--file "${PACKAGE}/docker/nginx.Dockerfile" \
33+
--file "${PWD}/.docker/nginx.dockerfile" \
3434
"${PWD}"
3535

3636
echo "Building [${PHP_TAG}]"
3737
docker build \
3838
--tag "${PHP_TAG}" \
39-
--file "${PACKAGE}/docker/php.Dockerfile" \
39+
--file "${PWD}/.docker/php.dockerfile" \
4040
"${PWD}"

composer.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@
1414
],
1515
"require": {
1616
"php": "^8.0",
17-
"illuminate/console": "^9.47",
1817
"illuminate/contracts": "^9.47",
19-
"illuminate/support": "^9.47",
2018
"twig/twig": "^3.0"
2119
},
2220
"extra": {
@@ -27,6 +25,9 @@
2725
}
2826
},
2927
"autoload": {
28+
"files": [
29+
"src/helpers.php"
30+
],
3031
"psr-4": {
3132
"BlameButton\\LaravelDockerBuilder\\": "src/"
3233
}
@@ -35,5 +36,8 @@
3536
"optimize-autoloader": true,
3637
"preferred-install": "dist",
3738
"sort-packages": true
39+
},
40+
"require-dev": {
41+
"orchestra/testbench": "^7.19"
3842
}
3943
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace BlameButton\LaravelDockerBuilder\Commands\Choices;
4+
5+
class ArtisanOptimize
6+
{
7+
public const YES = 'yes';
8+
public const NO = 'no';
9+
10+
public static function values(): array
11+
{
12+
return [
13+
self::YES,
14+
self::NO,
15+
];
16+
}
17+
}

src/Commands/DockerBuildCommand.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace BlameButton\LaravelDockerBuilder\Commands;
4+
5+
use BlameButton\LaravelDockerBuilder\DockerServiceProvider;
6+
use Symfony\Component\Process\Process;
7+
8+
class DockerBuildCommand extends BaseCommand
9+
{
10+
protected $name = 'docker:build';
11+
12+
public function handle(): int
13+
{
14+
$command = package_path('bin/docker-build');
15+
16+
$process = new Process(
17+
command: [$command],
18+
cwd: base_path(),
19+
);
20+
21+
$process->run(function ($type, $buffer) {
22+
match ($type) {
23+
Process::OUT => fwrite(STDOUT, $buffer),
24+
Process::ERR => fwrite(STDERR, $buffer),
25+
};
26+
});
27+
28+
return self::SUCCESS;
29+
}
30+
}

src/Commands/DockerGenerateCommand.php

Lines changed: 51 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace BlameButton\LaravelDockerBuilder\Commands;
44

5+
use BlameButton\LaravelDockerBuilder\Commands\Choices\ArtisanOptimize;
56
use BlameButton\LaravelDockerBuilder\Commands\Choices\NodeBuildTool;
67
use BlameButton\LaravelDockerBuilder\Commands\Choices\NodePackageManager;
78
use BlameButton\LaravelDockerBuilder\Commands\Choices\PhpVersion;
@@ -12,10 +13,6 @@ class DockerGenerateCommand extends BaseCommand
1213
{
1314
use InteractsWithTwig;
1415

15-
const YES = 'yes';
16-
const NO = 'no';
17-
const NONE = 'none';
18-
1916
protected $name = 'docker:generate';
2017

2118
protected $description = 'Generate Dockerfiles';
@@ -41,42 +38,23 @@ public function handle(): int
4138
]),
4239
]);
4340

44-
$dockerfiles->each(function ($value, $key) {
45-
$this->info($key);
46-
});
41+
if (!is_dir($dir = base_path('.docker'))) {
42+
mkdir($dir);
43+
}
4744

48-
return self::SUCCESS;
49-
}
45+
foreach ($dockerfiles as $file => $content) {
46+
// Example: $PWD/.docker/{php,nginx}.dockerfile
47+
$dockerfile = sprintf("%s/%s", $dir, $file);
5048

51-
protected function getOptions(): array
52-
{
49+
// Save Dockerfile contents
50+
file_put_contents($dockerfile, $content);
5351

54-
return [
55-
new InputOption(
56-
name: 'php-version',
57-
shortcut: 'p',
58-
mode: InputOption::VALUE_REQUIRED,
59-
description: sprintf("PHP version (supported: %s)", join(', ', PhpVersion::values())),
60-
),
61-
new InputOption(
62-
name: 'optimize',
63-
shortcut: 'o',
64-
mode: InputOption::VALUE_NONE,
65-
description: 'Add "php artisan optimize" to entrypoint',
66-
),
67-
new InputOption(
68-
name: 'node-package-manager',
69-
shortcut: 'm',
70-
mode: InputOption::VALUE_REQUIRED,
71-
description: sprintf('Node Package Manager (supported: %s)', join(', ', NodePackageManager::values())),
72-
),
73-
new InputOption(
74-
name: 'node-build-tool',
75-
shortcut: 'b',
76-
mode: InputOption::VALUE_REQUIRED,
77-
description: sprintf('Node Build Tool (supported: %s)', join(', ', NodeBuildTool::values())),
78-
),
79-
];
52+
// Output saved Dockerfile location
53+
$filename = str($dockerfile)->after(base_path())->trim('/');
54+
$this->info(sprintf('Saved: %s', $filename));
55+
}
56+
57+
return self::SUCCESS;
8058
}
8159

8260
private function getPhpVersion(): string
@@ -102,10 +80,11 @@ public function getArtisanOptimize(): bool
10280

10381
$choice = $this->choice(
10482
question: 'Do you want to run "php artisan optimize" when the image boots?',
105-
choices: [self::YES, self::NO],
106-
default: self::YES,
83+
choices: ArtisanOptimize::values(),
84+
default: ArtisanOptimize::YES,
10785
);
108-
return $choice === self::YES;
86+
87+
return ArtisanOptimize::YES === $choice;
10988
}
11089

11190
private function getNodePackageManager(): string|false
@@ -123,7 +102,6 @@ private function getNodePackageManager(): string|false
123102
);
124103
}
125104

126-
127105
private function getNodeBuildTool(): string
128106
{
129107
if ($option = $this->option('node-build-tool')) {
@@ -138,4 +116,36 @@ private function getNodeBuildTool(): string
138116
default: NodeBuildTool::VITE,
139117
);
140118
}
119+
120+
121+
protected function getOptions(): array
122+
{
123+
124+
return [
125+
new InputOption(
126+
name: 'php-version',
127+
shortcut: 'p',
128+
mode: InputOption::VALUE_REQUIRED,
129+
description: sprintf("PHP version (supported: %s)", join(', ', PhpVersion::values())),
130+
),
131+
new InputOption(
132+
name: 'optimize',
133+
shortcut: 'o',
134+
mode: InputOption::VALUE_NONE,
135+
description: 'Add "php artisan optimize" to entrypoint',
136+
),
137+
new InputOption(
138+
name: 'node-package-manager',
139+
shortcut: 'm',
140+
mode: InputOption::VALUE_REQUIRED,
141+
description: sprintf('Node Package Manager (supported: %s)', join(', ', NodePackageManager::values())),
142+
),
143+
new InputOption(
144+
name: 'node-build-tool',
145+
shortcut: 'b',
146+
mode: InputOption::VALUE_REQUIRED,
147+
description: sprintf('Node Build Tool (supported: %s)', join(', ', NodeBuildTool::values())),
148+
),
149+
];
150+
}
141151
}

src/DockerServiceProvider.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace BlameButton\LaravelDockerBuilder;
44

5+
use BlameButton\LaravelDockerBuilder\Commands\DockerBuildCommand;
56
use BlameButton\LaravelDockerBuilder\Commands\DockerGenerateCommand;
67
use Illuminate\Support\ServiceProvider;
78

@@ -11,13 +12,18 @@ public function boot(): void
1112
{
1213
if ($this->app->runningInConsole()) {
1314
$this->commands([
15+
DockerBuildCommand::class,
1416
DockerGenerateCommand::class,
1517
]);
1618
}
1719
}
1820

19-
public function register(): void
21+
public static function getPackagePath(string $path = null): string
2022
{
21-
$this->app->instance('laravel-docker-builder.base_path', dirname(__DIR__));
23+
$dir = dirname(__FILE__, 2);
24+
if ($path) {
25+
return $dir . DIRECTORY_SEPARATOR . $path;
26+
}
27+
return $dir;
2228
}
2329
}

src/Traits/InteractsWithTwig.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace BlameButton\LaravelDockerBuilder\Traits;
44

5-
use Illuminate\Support\Facades\App;
65
use Twig\Environment as TwigEnvironment;
76
use Twig\Loader\FilesystemLoader;
87

@@ -15,7 +14,8 @@ private function twig(): TwigEnvironment
1514
if (!is_null($this->twig)) {
1615
return $this->twig;
1716
}
18-
$loader = new FilesystemLoader(App::get('laravel-docker-builder.base_path') . '/docker/template');
17+
$path = package_path('docker/template');
18+
$loader = new FilesystemLoader($path);
1919
return $this->twig = new TwigEnvironment($loader);
2020
}
2121

src/helpers.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
if (!function_exists('package_path')) {
4+
function package_path(string $path = null): string
5+
{
6+
$dir = dirname(__FILE__, 2);
7+
8+
if ($path = str($path)->ltrim(DIRECTORY_SEPARATOR)) {
9+
return $dir . DIRECTORY_SEPARATOR . $path;
10+
}
11+
12+
return $dir;
13+
}
14+
}

0 commit comments

Comments
 (0)