Add basic database support with default integrated sqlite3 install

This commit is contained in:
Daniel Winning 2025-04-14 04:32:30 +01:00
parent df4fdbde3c
commit 7f2fb2bbf3
9 changed files with 61 additions and 5 deletions

View file

@ -15,7 +15,7 @@ RUN apt-get -qq update && apt-get -qq install -y \
> /dev/null 2>&1
RUN docker-php-ext-configure intl > /dev/null 2>&1
RUN docker-php-ext-install mysqli pdo pdo_mysql sockets intl exif bcmath > /dev/null
RUN docker-php-ext-install sockets intl exif bcmath > /dev/null 2>&1
RUN docker-php-ext-install opcache > /dev/null 2>&1
COPY ./opcache.ini "${PHP_INI_DIR}/conf.d"

View file

@ -0,0 +1,4 @@
RUN mkdir -p /var/www/html/sqlite \
&& chown www-data:www-data /var/www/html/sqlite
RUN apt-get install sqlite3

View file

@ -7,8 +7,8 @@ services:
working_dir: /var/www/html
extra_hosts:
- host.docker.internal:host-gateway
volumes:
- ${PROJECT_DIRECTORY}:/var/www/html:cached
ports:
- ${PHP_PORT}:9003
container_name: ${PROJECT_NAME}-php
container_name: ${PROJECT_NAME}-php
volumes:
- ${PROJECT_DIRECTORY}:/var/www/html:cached

View file

@ -7,4 +7,7 @@ options:
enabled: true
version: 23
server:
enabled: true
enabled: true
database:
enabled: true
driver: sqlite3

2
config/sqlite.yaml Normal file
View file

@ -0,0 +1,2 @@
volumes:
- ${PROJECT_DIRECTORY}/sqlite:/var/www/html/sqlite:cached

View file

@ -83,6 +83,30 @@ class Config
return $this->getEnvironmentOption('php', 'xdebug');
}
/**
* @throws \Exception
*/
public function isDatabaseEnabled(InputInterface $input): bool
{
if ($input->getOption('disable-database')) {
return false;
}
return $this->getEnvironmentOption('database', 'enabled');
}
/**
* @throws \Exception
*/
public function getDatabaseDriver(InputInterface $input): ?string
{
if ($input->getOption('database')) {
return (string) $input->getOption('database');
}
return (string) $this->getEnvironmentOption('database', 'driver');
}
/**
* @throws \Exception
*/

View file

@ -32,6 +32,10 @@ class DockerComposeFileBuilder extends AbstractFileBuilder
$this->config->getFilePaths()->get('phpYamlTemplate')->getAbsolutePath()
);
if ($this->config->isDatabaseEnabled($input) && in_array($this->config->getDatabaseDriver($input), ['sqlite3', 'sqlite'])) {
$this->addSqliteDatabaseConfig();
}
if ($this->config->isServerEnabled($input)) {
$this->addNginxConfig();
}
@ -54,4 +58,11 @@ class DockerComposeFileBuilder extends AbstractFileBuilder
$this->content
);
}
private function addSqliteDatabaseConfig(): void
{
$sqlLiteConfig = file_get_contents((new SpinnerFilePath('config/sqlite.yaml'))->getAbsolutePath());
$sqlLiteConfig = str_replace('volumes:', '', $sqlLiteConfig);
$this->content .= $sqlLiteConfig;
}
}

View file

@ -38,6 +38,11 @@ class PHPDockerFileBuilder extends AbstractFileBuilder
file_get_contents($this->config->getFilePaths()->get('opcacheIniTemplate')->getAbsolutePath())
);
if ($this->config->isDatabaseEnabled($input) && in_array($this->config->getDatabaseDriver($input), ['sqlite3', 'sqlite'])) {
$this->addNewLine();
$this->content .= file_get_contents((new SpinnerFilePath('config/php-fpm/Sqlite.Dockerfile'))->getAbsolutePath());
}
if ($this->config->isNodeEnabled($input)) {
$this->addNewLine();
$this->content .= file_get_contents($this->config->getFilePaths()->get('nodeDockerfileTemplate')->getAbsolutePath());

View file

@ -65,6 +65,13 @@ class SpinCommand extends AbstractSpinnerCommand
InputOption::VALUE_NONE,
'Set this flag to disable XDebug for your environment.'
)
->addOption(
'disable-database',
null,
InputOption::VALUE_NONE,
'Set this flag to not include a database for your environment.'
)
->addOption('database', null, InputOption::VALUE_REQUIRED, 'The type of database to use (e.g., mysql, postgresql, sqlite).', null, ['sqlite'])
->addOption('node', null, InputOption::VALUE_OPTIONAL, 'The Node.js version to use (e.g. 20).');
}