Skip to content

Commit 793c0ba

Browse files
authored
Merge pull request #1 from geekstuff-it/level-up
Level up
2 parents 2aeaad4 + 8e49b64 commit 793c0ba

File tree

13 files changed

+143
-72
lines changed

13 files changed

+143
-72
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,7 @@ It's meant to be used with image geekstuffreal/php-fpm-alpine and the
77
`php-init` script in it that will download this project and use it.
88

99
To fully see how it's used, head over to https://github.com/geekstuff-it/docker-php-fpm-alpine
10+
11+
## TODO
12+
- add laravel specifics
13+
- add other installers (silex?)

src/Base/Config.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ class Config
1010
/** @var string */
1111
public $appVersion = 'alpha';
1212

13+
/** @var string */
14+
public $appDir = '/app';
15+
1316
/** @var string */
1417
public $rootDir;
1518

src/Command/Common.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,12 @@ protected function getTwig(string $templateDir): TwigEnvironment
149149
{
150150
static $twigs = [];
151151
if (! isset($twigs[$templateDir])) {
152-
$twigs[$templateDir] = new TwigEnvironment(new TwigFilesystemLoader($templateDir, $this->config->rootDir));
152+
$twigs[$templateDir] = new TwigEnvironment(
153+
new TwigFilesystemLoader($templateDir, $this->config->rootDir),
154+
[
155+
'autoescape' => false,
156+
]
157+
);
153158
}
154159

155160
return $twigs[$templateDir];
@@ -162,7 +167,7 @@ protected function isRoot(): bool
162167

163168
protected function detectFramework(): string
164169
{
165-
if (file_exists($this->config->rootDir.'/symfony.lock')) {
170+
if (file_exists($this->config->appDir.'/symfony.lock')) {
166171
return $this->data::FRAMEWORK_SYMFONY;
167172
} else {
168173
return $this->data::FRAMEWORK_NONE;
@@ -191,6 +196,7 @@ protected function getDockerizeTemplateFiles()
191196
$finder
192197
->ignoreDotFiles(false)
193198
->files()
199+
->depth("< 2")
194200
->in($this->getDockerizeTemplateDir());
195201

196202
// check if there are any results
@@ -239,15 +245,9 @@ protected function getAppsAndTools(): array
239245

240246
$apps['php'] = ['name' => 'php', 'version' => PHP_VERSION];
241247

242-
$matches = [];
243-
if (preg_match('/Composer version (\d+(?:\.\d+)+).*/', $this->getComposerVersion(), $matches)) {
244-
$apps['composer'] = ['name' => 'composer', 'version' => $matches[1]];
245-
}
246-
247-
$matches = [];
248-
if (preg_match('/Symfony CLI version v(\d+(?:\.\d+)+).*/', $this->getSymfonyVersion(), $matches)) {
249-
$apps['symfony'] = ['name' => 'symfony cli', 'version' => $matches[1]];
250-
}
248+
$apps['composer'] = ['name' => 'composer', 'version' => $this->getComposerVersion()];
249+
$apps['laravel'] = ['name' => 'laravel cli', 'version' => 'install with `install-laravel-cli`'];
250+
$apps['symfony'] = ['name' => 'symfony cli', 'version' => 'install with `install-symfony-cli`'];
251251

252252
return $apps;
253253
}
@@ -265,7 +265,7 @@ protected function getComposerVersion(): ?string
265265
protected function getSymfonyVersion(): ?string
266266
{
267267
$matches = [];
268-
if (preg_match('/Symfony CLI version v(\d+(?:\.\d+)+).*/', trim(shell_exec('symfony version --no-ansi')), $matches)) {
268+
if (preg_match('/Symfony CLI version v(\d+(?:\.\d+)+).*/', trim(shell_exec('symfony version --no-ansi') ?: ''), $matches)) {
269269
return $matches[1];
270270
}
271271

src/Command/Dockerize.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,24 @@ protected function configure(): void
5555
'Timezone to use',
5656
'UTC'
5757
)
58+
->addOption(
59+
'framework', null, InputOption::VALUE_REQUIRED,
60+
'The framework templates to use. (will try to auto detect)'
61+
)
5862
;
5963
}
6064

6165
protected function interact(InputInterface $input, OutputInterface $output): void
6266
{
6367
parent::interact($input, $output);
68+
69+
if (is_null($input->getOption('framework'))) {
70+
$framework = $this->detectFramework();
71+
72+
if ($framework !== $this->data::FRAMEWORK_NONE) {
73+
$input->setOption('framework', $framework);
74+
}
75+
}
6476
}
6577

6678
/**
@@ -115,16 +127,25 @@ protected function writeFiles(InputInterface $input, OutputInterface $output): b
115127
'IDE_SERVERNAME' => $input->getOption('ide-servername'),
116128
'TIMEZONE' => $input->getOption('timezone'),
117129
'COMPOSER_VERSION' => $input->getOption('composer-version'),
130+
'GENERATED_AT' => date(DATE_RFC850),
118131
];
119132

120133
$twig = $this->getTwig($this->getDockerizeTemplateDir());
121134
foreach ($this->getDockerizeTemplateFiles() as $templateFile) {
135+
$template = $templateFile->getFilename();
136+
if ($framework = $input->getOption('framework')) {
137+
$frameworkVersion = sprintf('framework/%s/%s', $framework, $templateFile->getFilename());
138+
if ($fs->exists($this->config->rootDir . '/templates/dockerizer/' . $frameworkVersion)) {
139+
$template = $frameworkVersion;
140+
}
141+
}
142+
122143
$newFile = sprintf('/app/%s', $templateFile->getFilenameWithoutExtension());
123144
if ($fs->exists($newFile)) {
124145
throw new Exception("Destination file already exists");
125146
}
126147

127-
$fs->dumpFile($newFile, $twig->render($templateFile->getFilename(), $params));
148+
$fs->dumpFile($newFile, trim($twig->render($template, $params)).PHP_EOL);
128149
}
129150

130151
return true;

src/Command/Init.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ protected function createPhpUser(InputInterface $input, OutputInterface $output)
9090
if (! $process->isSuccessful()) {
9191
throw new ProcessFailedException($process);
9292
}
93+
94+
// change PS1
95+
$fp = fopen(sprintf('/home/%s/.profile', getenv('PHP_USER_NAME')), 'a');
96+
fwrite($fp, 'export PS1="(init) \h:\w\$ "');
97+
fclose($fp);
9398
}
9499

95100
/**
@@ -113,6 +118,6 @@ protected function dropToUser(OutputInterface $output): void
113118
{
114119
$output->writeln('# Starting shell as php user.');
115120
$output->writeln('');
116-
pcntl_exec('/bin/su', [getenv('PHP_USER_NAME')]);
121+
pcntl_exec('/bin/su', ['-l', '-c', 'cd /app; bash --login -i', getenv('PHP_USER_NAME')]);
117122
}
118123
}
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1-
/vendor
2-
/var/*
1+
{% block content %}
2+
# generic
33
.git
4+
.idea
5+
/vendor
6+
{% block extras %}{% endblock %}
7+
{% endblock %}

templates/dockerizer/Dockerfile.twig

Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,35 @@ ARG NGINX_VERSION={{ NGINX_VERSION }}
44
##############
55
# BASE #
66
##############
7+
# things that needs to happen both in dev and prod stage
78
FROM {{ FPM_FROM }}:${PHP_GEEKSTUFF_VERSION} AS base
8-
# Add things that needs to happen both in dev and prod stage
99

10+
{% block base %}
11+
{% block base_extras %}{% endblock %}
12+
{% endblock %}
1013

1114
##############
1215
# BUILDTOOLS #
1316
##############
14-
FROM base AS buildtools
15-
# Add things needed during both dev and build phase.
17+
# things needed during both dev and build phase.
1618
# Example: composer, npm, grunt, gulp, etc.
19+
FROM base AS buildtools
1720

21+
{% block buildtools %}
1822
# few basic tools
1923
ENV COMPOSER_VERSION={{ COMPOSER_VERSION }}
2024
RUN /scripts/install-buildtools
2125

26+
{% block buildtools_extras %}{% endblock %}
27+
{% endblock %}
2228

2329
##############
2430
# DEV #
2531
##############
2632
FROM buildtools AS dev
2733

34+
{% block dev %}
35+
{% block dev_ini_xdebug %}
2836
## Env
2937
ENV APP_ENV=dev \
3038
PHP_IDE_CONFIG="serverName=php-docker-dev" \
@@ -54,20 +62,30 @@ RUN apk add --update --no-cache --virtual .build-dependencies $PHPIZE_DEPS \
5462
&& docker-php-ext-enable xdebug \
5563
&& pecl clear-cache \
5664
&& apk del .build-dependencies
65+
{% endblock %}
5766

58-
## Switch to php user
59-
RUN create-php-user ${PHP_USER_ID}
67+
## Create php user, specify stage in PS1 prompt
68+
RUN create-php-user ${PHP_USER_ID} \
69+
&& echo 'export PS1="(dev) \h:\w\$ "' >> /home/${PHP_USER_NAME}/.bashrc
70+
71+
# Switch to user
6072
USER ${PHP_USER_NAME}
6173

74+
{% block dev_extras %}{% endblock %}
75+
{% endblock %}
6276

6377
##############
6478
# BUILD #
6579
##############
6680
FROM buildtools AS build
6781

82+
{% block build %}
6883
## ENV
84+
{% block build_env %}
6985
ENV APP_ENV=prod \
7086
TZ={{ TIMEZONE }}
87+
{% block build_env_extras %}{% endblock %}
88+
{% endblock %}
7189

7290
## Create php user
7391
RUN create-php-user ${PHP_USER_ID} \
@@ -76,43 +94,53 @@ RUN create-php-user ${PHP_USER_ID} \
7694
## Copy code
7795
COPY --chown=${PHP_USER_NAME}:${PHP_USER_NAME} . /app
7896

79-
## Init & Optimise Symfony app
97+
## Init & Optimise app
8098
USER ${PHP_USER_NAME}
81-
RUN composer install --no-dev \
82-
&& bin/console cache:clear --env=prod \
83-
&& bin/console assets:install public \
84-
&& composer dump-env prod \
85-
&& composer dump-autoload --no-dev --classmap-authoritative
99+
{% block build_framework %}
100+
RUN composer install --no-dev
101+
{% endblock %}
86102

103+
{% block build_extras %}{% endblock %}
104+
{% endblock %}
87105

88106
##############
89107
# NGINX #
90108
##############
91109
FROM {{ NGINX_FROM }}:${NGINX_VERSION} AS nginx
92110

111+
{% block nginx %}
93112
WORKDIR /app
94113

95114
## Copy some generated files from our build
96115
COPY --from=build --chown=nginx:nginx /app/public /app/public
97116

117+
{% block nginx_extras %}{% endblock %}
118+
{% endblock %}
98119

99120
##############
100121
# PROD #
101122
##############
102123
FROM base AS prod
103124

125+
{% block prod %}
104126
## Create php user
105127
RUN create-php-user ${PHP_USER_ID} \
106128
&& rm -f /app/.empty
107129

108-
## Copy generated files from our build (and we could exclude some from public/)
130+
## Copy generated files from our build
131+
{% block prod_copy %}
109132
COPY --from=build --chown=${PHP_USER_NAME}:${PHP_USER_NAME} /app .
133+
{% endblock %}
110134

111135
## ENV
136+
{% block prod_env %}
112137
ENV APP_ENV=prod \
113138
TZ={{ TIMEZONE }}
139+
{% block prod_env_extras %}{% endblock %}
140+
{% endblock %}
114141

115142
## Php ini
143+
{% block prod_ini %}
116144
RUN { \
117145
echo "[PHP]"; \
118146
echo "display_errors = 0"; \
@@ -121,10 +149,16 @@ RUN { \
121149
echo "expose_php = 0"; \
122150
echo ""; \
123151
echo "[opcache]"; \
124-
echo "opcache.preload = /app/var/cache/prod/App_KernelProdContainer.preload.php"; \
152+
echo "{% block prod_opcache_preload %}{% endblock %}"; \
125153
echo "opcache.validate_timestamps = 0"; \
126154
} > "$PHP_INI_DIR/php.ini"
127155
#RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini"
156+
{% block prod_ini_extras %}{% endblock %}
157+
{% endblock %}
128158

129159
## Switch to php user
130160
USER ${PHP_USER_NAME}
161+
{% endblock %}
162+
163+
164+
# Generated {{ GENERATED_AT }} by [Dockerizer](https://github.com/geekstuff-it/php-fpm-nginx-alpine-dockerizer)

templates/dockerizer/docker-compose.prod-test.yml.twig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{% block content %}
12
version: '3.7'
23
services:
34
php:
@@ -15,3 +16,4 @@ services:
1516
environment:
1617
- PHP_FPM_HOST=php
1718
ports: ["${LOCAL_HTTP_PORT-8080}:8080"]
19+
{% endblock %}

templates/dockerizer/docker-compose.yml.twig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{% block content %}
12
version: '3.7'
23
services:
34
php:
@@ -14,3 +15,4 @@ services:
1415
- PHP_FPM_PORT=9000
1516
ports: ["${LOCAL_HTTP_PORT-8080}:8080"]
1617
volumes: ['./public:/app/public:cached']
18+
{% endblock %}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{% extends ".dockerignore.twig" %}
2+
{% block extras %}
3+
# symfony
4+
/var/*
5+
{% endblock %}

0 commit comments

Comments
 (0)