From 7f2fb2bbf380204453c0b421d7f1eb93b190344f Mon Sep 17 00:00:00 2001 From: Daniel Winning Date: Mon, 14 Apr 2025 04:32:30 +0100 Subject: [PATCH] Add basic database support with default integrated sqlite3 install --- config/php-fpm/Dockerfile | 2 +- config/php-fpm/Sqlite.Dockerfile | 4 ++++ config/php.yaml | 6 ++--- config/spinner.yaml | 5 +++- config/sqlite.yaml | 2 ++ src/Classes/Config/Config.php | 24 +++++++++++++++++++ src/Classes/File/DockerComposeFileBuilder.php | 11 +++++++++ src/Classes/File/PHPDockerFileBuilder.php | 5 ++++ src/Command/SpinCommand.php | 7 ++++++ 9 files changed, 61 insertions(+), 5 deletions(-) create mode 100644 config/php-fpm/Sqlite.Dockerfile create mode 100644 config/sqlite.yaml diff --git a/config/php-fpm/Dockerfile b/config/php-fpm/Dockerfile index 9a12dba..11d7778 100644 --- a/config/php-fpm/Dockerfile +++ b/config/php-fpm/Dockerfile @@ -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" diff --git a/config/php-fpm/Sqlite.Dockerfile b/config/php-fpm/Sqlite.Dockerfile new file mode 100644 index 0000000..8e4acc5 --- /dev/null +++ b/config/php-fpm/Sqlite.Dockerfile @@ -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 \ No newline at end of file diff --git a/config/php.yaml b/config/php.yaml index fdfb158..ff94687 100644 --- a/config/php.yaml +++ b/config/php.yaml @@ -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 \ No newline at end of file + container_name: ${PROJECT_NAME}-php + volumes: + - ${PROJECT_DIRECTORY}:/var/www/html:cached \ No newline at end of file diff --git a/config/spinner.yaml b/config/spinner.yaml index d8cf155..b0deb86 100644 --- a/config/spinner.yaml +++ b/config/spinner.yaml @@ -7,4 +7,7 @@ options: enabled: true version: 23 server: - enabled: true \ No newline at end of file + enabled: true + database: + enabled: true + driver: sqlite3 \ No newline at end of file diff --git a/config/sqlite.yaml b/config/sqlite.yaml new file mode 100644 index 0000000..811ceec --- /dev/null +++ b/config/sqlite.yaml @@ -0,0 +1,2 @@ +volumes: + - ${PROJECT_DIRECTORY}/sqlite:/var/www/html/sqlite:cached \ No newline at end of file diff --git a/src/Classes/Config/Config.php b/src/Classes/Config/Config.php index 2e82204..81736a1 100644 --- a/src/Classes/Config/Config.php +++ b/src/Classes/Config/Config.php @@ -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 */ diff --git a/src/Classes/File/DockerComposeFileBuilder.php b/src/Classes/File/DockerComposeFileBuilder.php index 60f39b0..2bb84d0 100644 --- a/src/Classes/File/DockerComposeFileBuilder.php +++ b/src/Classes/File/DockerComposeFileBuilder.php @@ -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; + } } \ No newline at end of file diff --git a/src/Classes/File/PHPDockerFileBuilder.php b/src/Classes/File/PHPDockerFileBuilder.php index b43863c..6583ca8 100644 --- a/src/Classes/File/PHPDockerFileBuilder.php +++ b/src/Classes/File/PHPDockerFileBuilder.php @@ -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()); diff --git a/src/Command/SpinCommand.php b/src/Command/SpinCommand.php index add4e87..c785541 100644 --- a/src/Command/SpinCommand.php +++ b/src/Command/SpinCommand.php @@ -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).'); }