-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
290 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
FROM debian:10 | ||
|
||
ARG DEBIAN_FRONTEND=noninteractive | ||
|
||
# install: php / mysql / postgres / tools / mssql deps | ||
RUN apt-get update && apt-get -y install \ | ||
php-cli php-xml \ | ||
mariadb-server mariadb-client php-mysql \ | ||
postgresql php-pgsql \ | ||
postgresql-11-postgis-2.5 \ | ||
git wget | ||
|
||
# install run script | ||
ADD run.sh /usr/sbin/docker-run | ||
CMD docker-run |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#!/bin/bash | ||
echo "================================================" | ||
echo " Debian 10 (PHP 7.3)" | ||
echo "================================================" | ||
|
||
echo -n "[1/4] Starting MariaDB 10.3 ..... " | ||
# make sure mysql can create socket and lock | ||
mkdir /var/run/mysqld && chmod 777 /var/run/mysqld | ||
# run mysql server | ||
nohup mysqld > /root/mysql.log 2>&1 & | ||
# wait for mysql to become available | ||
while ! mysqladmin ping -hlocalhost >/dev/null 2>&1; do | ||
sleep 1 | ||
done | ||
# create database and user on mysql | ||
mysql -u root >/dev/null << 'EOF' | ||
CREATE DATABASE `php-crud-api` CHARACTER SET utf8 COLLATE utf8_general_ci; | ||
CREATE USER 'php-crud-api'@'localhost' IDENTIFIED BY 'php-crud-api'; | ||
GRANT ALL PRIVILEGES ON `php-crud-api`.* TO 'php-crud-api'@'localhost' WITH GRANT OPTION; | ||
FLUSH PRIVILEGES; | ||
EOF | ||
echo "done" | ||
|
||
echo -n "[2/4] Starting PostgreSQL 11.4 .. " | ||
# run postgres server | ||
nohup su - -c "/usr/lib/postgresql/11/bin/postgres -D /etc/postgresql/11/main" postgres > /root/postgres.log 2>&1 & | ||
# wait for postgres to become available | ||
until su - -c "psql -U postgres -c '\q'" postgres >/dev/null 2>&1; do | ||
sleep 1; | ||
done | ||
# create database and user on postgres | ||
su - -c "psql -U postgres >/dev/null" postgres << 'EOF' | ||
CREATE USER "php-crud-api" WITH PASSWORD 'php-crud-api'; | ||
CREATE DATABASE "php-crud-api"; | ||
GRANT ALL PRIVILEGES ON DATABASE "php-crud-api" to "php-crud-api"; | ||
\c "php-crud-api"; | ||
CREATE EXTENSION IF NOT EXISTS postgis; | ||
\q | ||
EOF | ||
echo "done" | ||
|
||
echo -n "[3/4] Starting SQLServer 2017 ... " | ||
echo "skipped" | ||
|
||
echo -n "[4/4] Cloning PHP-CRUD-API v2 ... " | ||
# install software | ||
if [ -d /php-crud-api ]; then | ||
echo "skipped" | ||
else | ||
git clone --quiet https://github.com/mevdschee/php-crud-api.git | ||
echo "done" | ||
fi | ||
|
||
echo "------------------------------------------------" | ||
|
||
# run the tests | ||
cd php-crud-api | ||
php test.php |
124 changes: 124 additions & 0 deletions
124
vendor/mevdschee/php-crud-api/src/Tqdev/PhpCrudApi/Database/LazyPdo.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
<?php | ||
|
||
namespace Tqdev\PhpCrudApi\Database; | ||
|
||
class LazyPdo extends \PDO | ||
{ | ||
private $dsn; | ||
private $user; | ||
private $password; | ||
private $options; | ||
private $commands; | ||
|
||
private $pdo = null; | ||
|
||
public function __construct(string $dsn, /*?string*/ $user = null, /*?string*/ $password = null, array $options = array()) | ||
{ | ||
$this->dsn = $dsn; | ||
$this->user = $user; | ||
$this->password = $password; | ||
$this->options = $options; | ||
$this->commands = array(); | ||
// explicitly NOT calling super::__construct | ||
} | ||
|
||
public function addInitCommand(string $command)/*: void*/ | ||
{ | ||
$this->commands[] = $command; | ||
} | ||
|
||
private function pdo() | ||
{ | ||
if (!$this->pdo) { | ||
$this->pdo = new \PDO($this->dsn, $this->user, $this->password, $this->options); | ||
foreach ($this->commands as $command) { | ||
$this->pdo->query($command); | ||
} | ||
} | ||
return $this->pdo; | ||
} | ||
|
||
public function reconstruct(string $dsn, /*?string*/ $user = null, /*?string*/ $password = null, array $options = array()): bool | ||
{ | ||
$this->dsn = $dsn; | ||
$this->user = $user; | ||
$this->password = $password; | ||
$this->options = $options; | ||
$this->commands = array(); | ||
if ($this->pdo) { | ||
$this->pdo = null; | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
public function inTransaction(): bool | ||
{ | ||
// Do not call parent method if there is no pdo object | ||
return $this->pdo && parent::inTransaction(); | ||
} | ||
|
||
public function setAttribute($attribute, $value): bool | ||
{ | ||
if ($this->pdo) { | ||
return $this->pdo()->setAttribute($attribute, $value); | ||
} | ||
$this->options[$attribute] = $value; | ||
return true; | ||
} | ||
|
||
public function getAttribute($attribute): mixed | ||
{ | ||
return $this->pdo()->getAttribute($attribute); | ||
} | ||
|
||
public function beginTransaction(): bool | ||
{ | ||
return $this->pdo()->beginTransaction(); | ||
} | ||
|
||
public function commit(): bool | ||
{ | ||
return $this->pdo()->commit(); | ||
} | ||
|
||
public function rollBack(): bool | ||
{ | ||
return $this->pdo()->rollBack(); | ||
} | ||
|
||
public function errorCode(): mixed | ||
{ | ||
return $this->pdo()->errorCode(); | ||
} | ||
|
||
public function errorInfo(): array | ||
{ | ||
return $this->pdo()->errorInfo(); | ||
} | ||
|
||
public function exec($query): int | ||
{ | ||
return $this->pdo()->exec($query); | ||
} | ||
|
||
public function prepare($statement, $options = array()) | ||
{ | ||
return $this->pdo()->prepare($statement, $options); | ||
} | ||
|
||
public function quote($string, $parameter_type = null): string | ||
{ | ||
return $this->pdo()->quote($string, $parameter_type); | ||
} | ||
|
||
public function lastInsertId(/* ?string */$name = null): string | ||
{ | ||
return $this->pdo()->lastInsertId($name); | ||
} | ||
|
||
public function query(string $statement): \PDOStatement | ||
{ | ||
return call_user_func_array(array($this->pdo(), 'query'), func_get_args()); | ||
} | ||
} |
93 changes: 93 additions & 0 deletions
93
vendor/mevdschee/php-crud-api/src/Tqdev/PhpCrudApi/Middleware/ReconnectMiddleware.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<?php | ||
|
||
namespace Tqdev\PhpCrudApi\Middleware; | ||
|
||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Psr\Http\Server\RequestHandlerInterface; | ||
use Tqdev\PhpCrudApi\Column\ReflectionService; | ||
use Tqdev\PhpCrudApi\Controller\Responder; | ||
use Tqdev\PhpCrudApi\Database\GenericDB; | ||
use Tqdev\PhpCrudApi\Middleware\Base\Middleware; | ||
use Tqdev\PhpCrudApi\Middleware\Router\Router; | ||
|
||
class ReconnectMiddleware extends Middleware | ||
{ | ||
private $reflection; | ||
private $db; | ||
|
||
public function __construct(Router $router, Responder $responder, array $properties, ReflectionService $reflection, GenericDB $db) | ||
{ | ||
parent::__construct($router, $responder, $properties); | ||
$this->reflection = $reflection; | ||
$this->db = $db; | ||
} | ||
|
||
private function getDriver(): string | ||
{ | ||
$driverHandler = $this->getProperty('driverHandler', ''); | ||
if ($driverHandler) { | ||
return call_user_func($driverHandler); | ||
} | ||
return ''; | ||
} | ||
|
||
private function getAddress(): string | ||
{ | ||
$addressHandler = $this->getProperty('addressHandler', ''); | ||
if ($addressHandler) { | ||
return call_user_func($addressHandler); | ||
} | ||
return ''; | ||
} | ||
|
||
private function getPort(): int | ||
{ | ||
$portHandler = $this->getProperty('portHandler', ''); | ||
if ($portHandler) { | ||
return call_user_func($portHandler); | ||
} | ||
return 0; | ||
} | ||
|
||
private function getDatabase(): string | ||
{ | ||
$databaseHandler = $this->getProperty('databaseHandler', ''); | ||
if ($databaseHandler) { | ||
return call_user_func($databaseHandler); | ||
} | ||
return ''; | ||
} | ||
|
||
private function getUsername(): string | ||
{ | ||
$usernameHandler = $this->getProperty('usernameHandler', ''); | ||
if ($usernameHandler) { | ||
return call_user_func($usernameHandler); | ||
} | ||
return ''; | ||
} | ||
|
||
private function getPassword(): string | ||
{ | ||
$passwordHandler = $this->getProperty('passwordHandler', ''); | ||
if ($passwordHandler) { | ||
return call_user_func($passwordHandler); | ||
} | ||
return ''; | ||
} | ||
|
||
public function process(ServerRequestInterface $request, RequestHandlerInterface $next): ResponseInterface | ||
{ | ||
$driver = $this->getDriver(); | ||
$address = $this->getAddress(); | ||
$port = $this->getPort(); | ||
$database = $this->getDatabase(); | ||
$username = $this->getUsername(); | ||
$password = $this->getPassword(); | ||
if ($driver || $address || $port || $database || $username || $password) { | ||
$this->db->reconstruct($driver, $address, $port, $database, $username, $password); | ||
} | ||
return $next->handle($request); | ||
} | ||
} |