Browse Source

Docker image + compose

Maurits van der Schee 3 years ago
parent
commit
41fd7822f0
6 changed files with 66 additions and 1 deletions
  1. 5
    0
      .htaccess
  2. 8
    0
      Dockerfile
  3. 11
    0
      api.php
  4. 30
    0
      docker-compose.yml
  5. 11
    0
      src/Tqdev/PhpCrudApi/Config.php
  6. 1
    1
      tests/fixtures/blog_mysql.sql

+ 5
- 0
.htaccess View File

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

+ 8
- 0
Dockerfile View File

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

+ 11
- 0
api.php View File

10913
             ];
10913
             ];
10914
         }
10914
         }
10915
 
10915
 
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
+
10916
         public function __construct(array $values)
10926
         public function __construct(array $values)
10917
         {
10927
         {
10918
             $driver = $this->getDefaultDriver($values);
10928
             $driver = $this->getDefaultDriver($values);
10924
                 $key = array_keys($diff)[0];
10934
                 $key = array_keys($diff)[0];
10925
                 throw new \Exception("Config has invalid value '$key'");
10935
                 throw new \Exception("Config has invalid value '$key'");
10926
             }
10936
             }
10937
+            $newValues = $this->applyEnvironmentVariables($newValues);
10927
             $this->values = $newValues;
10938
             $this->values = $newValues;
10928
         }
10939
         }
10929
 
10940
 

+ 30
- 0
docker-compose.yml View File

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

+ 11
- 0
src/Tqdev/PhpCrudApi/Config.php View File

69
         ];
69
         ];
70
     }
70
     }
71
 
71
 
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
+
72
     public function __construct(array $values)
82
     public function __construct(array $values)
73
     {
83
     {
74
         $driver = $this->getDefaultDriver($values);
84
         $driver = $this->getDefaultDriver($values);
80
             $key = array_keys($diff)[0];
90
             $key = array_keys($diff)[0];
81
             throw new \Exception("Config has invalid value '$key'");
91
             throw new \Exception("Config has invalid value '$key'");
82
         }
92
         }
93
+        $newValues = $this->applyEnvironmentVariables($newValues);
83
         $this->values = $newValues;
94
         $this->values = $newValues;
84
     }
95
     }
85
 
96
 

+ 1
- 1
tests/fixtures/blog_mysql.sql View File

130
 ('Launch', '2016-01-01 13:01:01', 0);
130
 ('Launch', '2016-01-01 13:01:01', 0);
131
 
131
 
132
 DROP VIEW IF EXISTS `tag_usage`;
132
 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`;
134
 
134
 
135
 DROP TABLE IF EXISTS `products`;
135
 DROP TABLE IF EXISTS `products`;
136
 CREATE TABLE `products` (
136
 CREATE TABLE `products` (

Loading…
Cancel
Save