Skip to content

Commit

Permalink
Add docker features
Browse files Browse the repository at this point in the history
  • Loading branch information
pifou25 committed Apr 22, 2024
1 parent f31a98d commit 459320f
Show file tree
Hide file tree
Showing 6 changed files with 1,153 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@
/temp
/composer.lock
.phpunit.result.cache
/node_modules
/docker/entities
/docker/vendor
22 changes: 22 additions & 0 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
FROM composer:latest AS vendor
WORKDIR /vendor
RUN composer require dodo-it/entity-generator

FROM php:8.2-cli-bookworm

RUN docker-php-ext-install pdo pdo_mysql && docker-php-ext-enable pdo_mysql

WORKDIR /app

COPY index.php .
COPY --from=vendor vendor/ .

RUN mkdir entities
VOLUME /app/entities

ENV MYSQL_HOSTNAME=db
ENV MYSQL_DATABASE=database
ENV MYSQL_USERNAME=username
ENV MYSQL_PASSWORD=password

CMD php index.php --hostname $MYSQL_HOSTNAME --database $MYSQL_DATABASE --username $MYSQL_USERNAME --password $MYSQL_PASSWORD
30 changes: 30 additions & 0 deletions docker/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Docker integration

## Entity Generator image

![Docker Pulls](https://img.shields.io/docker/pulls/pifou25/entity-generator)
https://hub.docker.com/r/pifou25/entity-generator

## Generation from existing database
The database should be accessible from the docker network, the hostname is either
the name of the Mysql running container, or `localhost` if db is running on host.
The below command will generate entities into `./entities` directory.

```
docker run --rm -v $PWD/entities:/app/entities --network some-network \
-e MYSQL_HOSTNAME=some-mariadb \
-e MYSQL_DATABASE=exmple-database \
-e MYSQL_USERNAME=example-user \
-e MYSQL_PASSWORD=my_cool_secret \
pifou25/entity-generator
```

## Generation from flat plain SQL file

The `docker compose` file create a new database and initialize it with SQL data,
you have to put SQL init file into `./init` directory. When db is ready,
the entity-generator start on it to generate PHP entities.

It is as simple as running this command from your working directory :
`docker compose up`

42 changes: 42 additions & 0 deletions docker/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
version: '3.8'

networks:
default:
name: default_network

services:
db:
image: mariadb # for rpi3/4: ghcr.io/linuxserver/mariadb
restart: unless-stopped
environment:
- MYSQL_ROOT_PASSWORD=my-admin-secret
- TZ=${TZ:-Europe/Paris}
- MYSQL_DATABASE=my-database
- MYSQL_USER=my-username
- MYSQL_PASSWORD=my-password
volumes:
# directory for init sql and scripts
- ./init:/docker-entrypoint-initdb.d
healthcheck:
test: ["CMD", "healthcheck.sh", "--su-mysql", "--connect", "--innodb_initialized"]
interval: 1m30s
timeout: 30s
retries: 5
# the start_period is long enough to ensure that init data is fully loaded
start_period: 30s
command: --skip-innodb-use-native-aio


php:
build: .
depends_on:
db:
condition: service_healthy
environment:
- TZ=${TZ:-Europe/Paris}
- MYSQL_HOSTNAME=db
- MYSQL_DATABASE=my-database
- MYSQL_USERNAME=my-username
- MYSQL_PASSWORD=my-password
volumes:
- ./entities:/app/entities
32 changes: 32 additions & 0 deletions docker/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

// get args from command line
$longopts = [
'hostname:',
'database:',
'username:',
'password:'
];

$params = getopt('h:d:u:p:', $longopts);
// var_dump($params);
if($params === false){
die('Missing or bad command line parameters.');
}

include('vendor/autoload.php');

$config = new \DodoIt\EntityGenerator\Generator\Config([
'path' => __DIR__ . '/entities',
'extends' => \Examples\Pdo\Entities\Entity::class,
'namespace' => 'Examples\Pdo\Entities'
]);

echo "connect to {$params['hostname']} {$params['database']} ...\n";
$pdo = new \PDO("mysql:dbname={$params['database']};host={$params['hostname']}", $params['username'], $params['password']);

echo "ok! Generating entities...\n";
$generatorFactory = new \DodoIt\EntityGenerator\Factory\GeneratorPdoFactory($pdo);
$generator = $generatorFactory->create($config);
$generator->generate();
echo "job done ! nb entities generated : " . count(glob('entities/*'));
Loading

0 comments on commit 459320f

Please sign in to comment.