diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..06b8ae2 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,31 @@ +version: '3.3' + +# docker-compose -f docker-compose.yml up db adminer app + +services: + + db: + image: postgres:11-alpine + environment: + POSTGRES_PASSWORD: example + POSTGRES_DB: foo + volumes: + - ./mocked-db-table-users.sql:/docker-entrypoint-initdb.d/mocked-db-table-users.sql:ro + ports: + - 5432:5432/tcp + + adminer: + image: adminer + ports: + - 8081:8080/tcp + + app: + build: + dockerfile: php73.Dockerfile + context: ./ + ports: + - 80:80/tcp + volumes: + - ./:/var/www/html:ro + - ./etc/register.ini:/etc/matrix-register/register.ini:ro + #docker run --rm -it --name php7.3 -p 80:80/tcp -v "$PWD":/var/www/html:ro -v "$PWD"/etc/register.ini:/etc/matrix-register/register.ini:ro php7.3:local diff --git a/lib/base.php b/lib/base.php index ff5bde0..61d6a27 100644 --- a/lib/base.php +++ b/lib/base.php @@ -109,5 +109,3 @@ class BaseClass { } - -?> diff --git a/lib/db.php b/lib/db.php index 1dcd1ab..d95673c 100644 --- a/lib/db.php +++ b/lib/db.php @@ -24,7 +24,7 @@ function testDriver(): bool return true; } -function getDatabase(&$config, $log): Database { +function getDatabase(Config $config, Logger $log): Database { /** * Erstellt ein Datenbank Objekt und gibt dieses zurück. Im Fehlerfall @@ -49,7 +49,7 @@ class Connection { private static $conn; - public function connect(&$config) + public function connect(Config $config): PDO { $driver = $config->getDbDriver(); @@ -79,7 +79,7 @@ class Connection { return $pdo; } - public static function get() + public static function get(): self { if (null === static::$conn) { @@ -183,7 +183,8 @@ class Database { */ $this->log->n("Try to create table requests"); - if ($this->pdo->driver === "PDO_PGSQL") { + $driverName = $this->pdo->getAttribute(PDO::ATTR_DRIVER_NAME); + if ($driverName === "pgsql") { $stmt = "CREATE TABLE IF NOT EXISTS requests ( id serial PRIMARY KEY, nick varchar(80) NOT NULL UNIQUE, @@ -191,7 +192,7 @@ class Database { token char(32) NOT NULL UNIQUE, ip bytea, time integer NOT NULL);"; - } else if ($this->pdo->driver === "PDO_PSQLITE") { + } else if ($driverName === "sqlite") { // ungetestet $stmt = "CREATE TABLE IF NOT EXISTS request ( id INTEGER PRIMARY KEY AUTOINCREMENT, @@ -200,16 +201,23 @@ class Database { token TEXT NOT NULL UNIQUE, ip BLOB, time INTEGER NOT NULL);"; + } else { + $stmt = ''; } - try { - $this->pdo->exec($stmt); - } catch (PDOException $e) { - $this->log->e("Failed to create table requests"); - $this->log->e("Error: {$e->getMessage()}"); - return false; + if ($stmt) { + try { + $response = $this->pdo->exec($stmt); + } catch (PDOException $e) { + $this->log->e("Failed to create table requests"); + $this->log->e("Error: {$e->getMessage()}"); + return false; + } + if ($response !== false) { + $this->log->n("Table requests successfull created"); + } + $this->log->n("Could not create table requests"); } - $this->log->n("Table requests successfull created"); - return true; + return false; } diff --git a/mocked-db-table-users.sql b/mocked-db-table-users.sql new file mode 100644 index 0000000..236ea12 --- /dev/null +++ b/mocked-db-table-users.sql @@ -0,0 +1,5 @@ +CREATE TABLE users ( + "name" character varying(30) NOT NULL, + "id" serial NOT NULL, + PRIMARY KEY ("id") +); diff --git a/php73.Dockerfile b/php73.Dockerfile new file mode 100644 index 0000000..ec8cf8f --- /dev/null +++ b/php73.Dockerfile @@ -0,0 +1,14 @@ +FROM php:7.3-apache + +# docker run --rm -it --name php7.3 -p 80:80/tcp -v "$PWD":/var/www/html:ro -v "$PWD"/etc/register.ini:/etc/matrix-register/register.ini:ro php:7.3-apache + +RUN apt-get update && apt-get install -y libpq-dev \ + && docker-php-ext-configure pgsql -with-pgsql=/usr/local/pgsql \ + && docker-php-ext-install pdo pdo_pgsql pgsql \ + && apt clean + +# docker build --rm --file=php73.Dockerfile -t php7.3:local ./ + +# docker run --rm -it --name php7.3 -p 80:80/tcp -v "$PWD":/var/www/html:ro -v "$PWD"/etc/register.ini:/etc/matrix-register/register.ini:ro php:7.3-apache bash +# docker run --rm -it --name php7.3 -p 80:80/tcp -v "$PWD":/var/www/html:ro -v "$PWD"/etc/register.ini:/etc/matrix-register/register.ini:ro php7.3:local +