Skip to content

Commit 5502cbf

Browse files
committed
Docker image + compose
1 parent 6d377ab commit 5502cbf

File tree

6 files changed

+66
-1
lines changed

6 files changed

+66
-1
lines changed

.htaccess

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<IfModule mod_rewrite.c>
2+
RewriteEngine on
3+
RewriteCond %{REQUEST_FILENAME} !-f
4+
RewriteRule ^(.*)$ api.php/$1 [QSA,L]
5+
</IfModule>

Dockerfile

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
FROM php:apache
2+
3+
RUN docker-php-ext-install pdo pdo_mysql
4+
5+
RUN a2enmod rewrite
6+
7+
COPY api.php /var/www/html/api.php
8+
COPY .htaccess /var/www/html/.htaccess

api.php

+11
Original file line numberDiff line numberDiff line change
@@ -10913,6 +10913,16 @@ private function getDriverDefaults(string $driver): array
1091310913
];
1091410914
}
1091510915

10916+
private function applyEnvironmentVariables(array $values): array
10917+
{
10918+
$newValues = array();
10919+
foreach ($values as $key => $value) {
10920+
$environmentKey = 'PHP_CRUD_API_' . strtoupper(preg_replace('/(?<!^)[A-Z]/', '_$0', str_replace('.', '_', $key)));
10921+
$newValues[$key] = getenv($environmentKey, true) ?: $value;
10922+
}
10923+
return $newValues;
10924+
}
10925+
1091610926
public function __construct(array $values)
1091710927
{
1091810928
$driver = $this->getDefaultDriver($values);
@@ -10924,6 +10934,7 @@ public function __construct(array $values)
1092410934
$key = array_keys($diff)[0];
1092510935
throw new \Exception("Config has invalid value '$key'");
1092610936
}
10937+
$newValues = $this->applyEnvironmentVariables($newValues);
1092710938
$this->values = $newValues;
1092810939
}
1092910940

docker-compose.yml

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
version: '3'
2+
services:
3+
mysql:
4+
image: mysql:8.0
5+
container_name: mysql
6+
command: --default-authentication-plugin=mysql_native_password
7+
restart: always
8+
environment:
9+
- MYSQL_ROOT_PASSWORD=php-crud-api
10+
- MYSQL_DATABASE=php-crud-api
11+
- MYSQL_USER=php-crud-api
12+
- MYSQL_PASSWORD=php-crud-api
13+
ports:
14+
- "3307:3306"
15+
volumes:
16+
- ./tests/fixtures/blog_mysql.sql:/docker-entrypoint-initdb.d/blog_mysql.sql
17+
apache:
18+
container_name: apache
19+
build:
20+
context: ./
21+
environment:
22+
- PHP_CRUD_API_DATABASE=php-crud-api
23+
- PHP_CRUD_API_USERNAME=php-crud-api
24+
- PHP_CRUD_API_PASSWORD=php-crud-api
25+
- PHP_CRUD_API_ADDRESS=mysql
26+
#- PHP_CRUD_API_DEBUG=1
27+
ports:
28+
- "8080:80"
29+
depends_on:
30+
- mysql

src/Tqdev/PhpCrudApi/Config.php

+11
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,16 @@ private function getDriverDefaults(string $driver): array
6969
];
7070
}
7171

72+
private function applyEnvironmentVariables(array $values): array
73+
{
74+
$newValues = array();
75+
foreach ($values as $key => $value) {
76+
$environmentKey = 'PHP_CRUD_API_' . strtoupper(preg_replace('/(?<!^)[A-Z]/', '_$0', str_replace('.', '_', $key)));
77+
$newValues[$key] = getenv($environmentKey, true) ?: $value;
78+
}
79+
return $newValues;
80+
}
81+
7282
public function __construct(array $values)
7383
{
7484
$driver = $this->getDefaultDriver($values);
@@ -80,6 +90,7 @@ public function __construct(array $values)
8090
$key = array_keys($diff)[0];
8191
throw new \Exception("Config has invalid value '$key'");
8292
}
93+
$newValues = $this->applyEnvironmentVariables($newValues);
8394
$this->values = $newValues;
8495
}
8596

tests/fixtures/blog_mysql.sql

+1-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ INSERT INTO `events` (`name`, `datetime`, `visitors`) VALUES
130130
('Launch', '2016-01-01 13:01:01', 0);
131131

132132
DROP VIEW IF EXISTS `tag_usage`;
133-
CREATE VIEW `tag_usage` AS select `tags`.`id` as `id`, `name`, count(`name`) AS `count` from `tags`, `post_tags` where `tags`.`id` = `post_tags`.`tag_id` group by `tags`.`id`, `name` order by `count` desc, `name`;
133+
CREATE DEFINER = 'php-crud-api' VIEW `tag_usage` AS select `tags`.`id` as `id`, `name`, count(`name`) AS `count` from `tags`, `post_tags` where `tags`.`id` = `post_tags`.`tag_id` group by `tags`.`id`, `name` order by `count` desc, `name`;
134134

135135
DROP TABLE IF EXISTS `products`;
136136
CREATE TABLE `products` (

0 commit comments

Comments
 (0)