Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add docker features #20

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
26 changes: 26 additions & 0 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
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
# ENV BASECLASS=
# ENV ENTITY_NAMESPACE=

SHELL ["/bin/bash", "-c"]
ENTRYPOINT ["php", "index.php"]
CMD ["php", "index.php"]
43 changes: 43 additions & 0 deletions docker/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# 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
```

## namespace and extends example
The base class must exist into /entities to be declared
```
docker run --rm -v $PWD/include/entities:/app/entities --network scripts_default \
-e MYSQL_HOSTNAME=myhost \
-e MYSQL_DATABASE=mydb \
-e MYSQL_USERNAME=myuser \
-e MYSQL_PASSWORD=mypwd \
-e ENTITY_NAMESPACE=Example\\Pdo\\Entities \
-e BASECLASS=Example\\Pdo\\Entities\Entity \
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
46 changes: 46 additions & 0 deletions docker/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

const BASEPATH = '/entities';

// get variable from system environment
$params['hostname'] = getenv('MYSQL_HOSTNAME');
$params['database'] = getenv('MYSQL_DATABASE');
$params['username'] = getenv('MYSQL_USERNAME');
$params['password'] = getenv('MYSQL_PASSWORD');
$params['baseclass'] = getenv('BASECLASS');
$params['namespace'] = getenv('ENTITY_NAMESPACE');

// vérifier que les paramètres requis pour mysql sont présents
if(!isset($params['hostname']) || !isset($params['database']) || !isset($params['username']) || !isset($params['password'])) {
var_dump($params);
die('Missing or bad command line parameters.');
}
if($params['baseclass']) {
// include file and transform string into ::class
include __DIR__ . BASEPATH . '/' . $params['baseclass'] . '.php';
} else {
// Default empty entity class
class Entity {}
$params['baseclass'] = Entity::class;
}
echo "baseclass is defined: {$params['baseclass']}\n";
if(!$params['namespace']) {
$params['namespace'] = 'Examples\Pdo\Entities';
}

include('vendor/autoload.php');

$config = new \DodoIt\EntityGenerator\Generator\Config([
'path' => __DIR__ . BASEPATH,
'extends' => $params['baseclass'], // \Examples\Pdo\Entities\Entity::class,
'namespace' => $params['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/*')) . "\n";
Loading