From 4e09285ca4f882d1785864001d57d295daf64cb6 Mon Sep 17 00:00:00 2001 From: Eldad Fux Date: Thu, 2 Jul 2020 01:34:05 +0300 Subject: [PATCH] Added PDO proxy --- app/init.php | 20 ++++---- app/server.php | 34 ++++--------- docker-compose.yml | 2 +- src/Appwrite/Database/Adapter/MySQL.php | 2 +- src/Appwrite/Extend/PDO.php | 66 ++++++++++++++++++++++++ src/Appwrite/Extend/PDOStatement.php | 67 +++++++++++++++++++++++++ 6 files changed, 156 insertions(+), 35 deletions(-) create mode 100644 src/Appwrite/Extend/PDO.php create mode 100644 src/Appwrite/Extend/PDOStatement.php diff --git a/app/init.php b/app/init.php index 3f8ab700fd..1ed47d5b52 100644 --- a/app/init.php +++ b/app/init.php @@ -12,18 +12,20 @@ if (\file_exists(__DIR__.'/../vendor/autoload.php')) { } use Appwrite\Auth\Auth; -use Utopia\App; -use Utopia\Config\Config; -use Utopia\Locale\Locale; -use Utopia\Registry\Registry; use Appwrite\Database\Database; use Appwrite\Database\Adapter\MySQL as MySQLAdapter; use Appwrite\Database\Adapter\Redis as RedisAdapter; use Appwrite\Database\Document; use Appwrite\Database\Validator\Authorization; use Appwrite\Event\Event; -use PHPMailer\PHPMailer\PHPMailer; +use Appwrite\Extend\PDO; +use Utopia\App; use Utopia\View; +use Utopia\Config\Config; +use Utopia\Locale\Locale; +use Utopia\Registry\Registry; +use PHPMailer\PHPMailer\PHPMailer; +use PDO as PDONative; const APP_NAME = 'Appwrite'; const APP_DOMAIN = 'appwrite.io'; @@ -87,13 +89,13 @@ $register->set('db', function () { // Register DB connection $dbScheme = App::getEnv('_APP_DB_SCHEMA', ''); $pdo = new PDO("mysql:host={$dbHost};dbname={$dbScheme};charset=utf8mb4", $dbUser, $dbPass, array( - PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8mb4', - PDO::ATTR_TIMEOUT => 5, // Seconds + PDONative::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8mb4', + PDONative::ATTR_TIMEOUT => 5, // Seconds )); // Connection settings - $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); // Return arrays - $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // Handle all errors with exceptions + $pdo->setAttribute(PDONative::ATTR_DEFAULT_FETCH_MODE, PDONative::FETCH_ASSOC); // Return arrays + $pdo->setAttribute(PDONative::ATTR_ERRMODE, PDONative::ERRMODE_EXCEPTION); // Handle all errors with exceptions return $pdo; }); diff --git a/app/server.php b/app/server.php index 453357a823..e02d2e3aca 100644 --- a/app/server.php +++ b/app/server.php @@ -15,7 +15,7 @@ ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL); -$http = new Server("localhost", 9501); +$http = new Server("localhost", 80); $http ->set([ @@ -49,39 +49,25 @@ $http->on('start', function (Server $http) { }); }); -$data = file_get_contents('../public/test.html'); - -// $register = new Registry(); -// $utopia = new App('Asia/Tel_Aviv'); -// /** -// * @var $request Request -// */ -// $request &= null; -// $response &= null; - -// include 'init.php'; -// include 'app.php'; - -$counter = 0; - include __DIR__ . '/app.php'; - -$http->on('request', function (SwooleRequest $swooleRequest, SwooleResponse $swooleResponse) use (&$counter) { +$http->on('request', function (SwooleRequest $swooleRequest, SwooleResponse $swooleResponse) { $request = new Request($swooleRequest); $response = new Response($swooleResponse); - //$swooleResponse->write($counter++); - $app = new App('Asia/Tel_Aviv'); try { $app->run($request, $response); } catch (\Throwable $th) { - var_dump($th->getMessage()); - var_dump($th->getFile()); - var_dump($th->getLine()); - $swooleResponse->end('error: '.$th->getMessage()); + if(App::isDevelopment()) { + var_dump($th->getMessage()); + var_dump($th->getFile()); + var_dump($th->getLine()); + $swooleResponse->end('error: '.$th->getMessage()); + } + + $swooleResponse->end('500: Server Error'); } }); diff --git a/docker-compose.yml b/docker-compose.yml index 2c1ae56dd2..b9a8424dc7 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -37,7 +37,7 @@ services: - VERSION=dev restart: unless-stopped ports: - - 9501:9501 + - 9501:80 networks: - appwrite labels: diff --git a/src/Appwrite/Database/Adapter/MySQL.php b/src/Appwrite/Database/Adapter/MySQL.php index ccf94e333e..dce3933315 100644 --- a/src/Appwrite/Database/Adapter/MySQL.php +++ b/src/Appwrite/Database/Adapter/MySQL.php @@ -906,7 +906,7 @@ class MySQL extends Adapter * * @throws Exception */ - protected function getPDO():PDO + protected function getPDO() { return $this->register->get('db'); } diff --git a/src/Appwrite/Extend/PDO.php b/src/Appwrite/Extend/PDO.php new file mode 100644 index 0000000000..338d1e392d --- /dev/null +++ b/src/Appwrite/Extend/PDO.php @@ -0,0 +1,66 @@ +dsn = $dsn; + $this->username = $username; + $this->passwd = $passwd; + $this->options = $options; + + $this->pdo = new PDONative($dsn, $username, $passwd, $options); + } + + public function setAttribute($attribute, $value) + { + return $this->pdo->setAttribute($attribute, $value); + } + + public function prepare($statement, array $driver_options = []) + { + return new PDOStatement($this->pdo, $this->pdo->prepare($statement, $driver_options)); + } + + public function quote($string, $parameter_type = PDONative::PARAM_STR) + { + return $this->pdo->quote($string, $parameter_type); + } + + public function reconnect() + { + return $this->pdo = new PDONative($this->dsn, $this->username, $this->passwd, $this->options); + } +} \ No newline at end of file diff --git a/src/Appwrite/Extend/PDOStatement.php b/src/Appwrite/Extend/PDOStatement.php new file mode 100644 index 0000000000..86ed8879cd --- /dev/null +++ b/src/Appwrite/Extend/PDOStatement.php @@ -0,0 +1,67 @@ +pdo = $pdo; + $this->PDOStatement = $PDOStatement; + } + + public function bindValue($parameter, $value, $data_type = PDONative::PARAM_STR) + { + $result = $this->PDOStatement->bindValue($parameter, $value, $data_type); + + return $result; + } + + public function bindParam($parameter, &$variable, $data_type = PDONative::PARAM_STR, $length = null, $driver_options = null) + { + $result = $this->PDOStatement->bindParam($parameter, $variable, $data_type, $length, $driver_options); + + return $result; + } + + public function bindColumn($column, &$param, $type = null, $maxlen = null, $driverdata = null) + { + $result = $this->PDOStatement->bindColumn($column, $param, $type, $maxlen, $driverdata); + + return $result; + } + + public function execute($input_parameters = null) + { + $result = $this->PDOStatement->execute($input_parameters); + + return $result; + } + + public function fetch($fetch_style = PDONative::FETCH_ASSOC, $cursor_orientation = PDONative::FETCH_ORI_NEXT, $cursor_offset = 0) + { + $result = $this->PDOStatement->fetch($fetch_style, $cursor_orientation, $cursor_offset); + + return $result; + } + + public function fetchAll() + { + $result = $this->PDOStatement->fetchAll(); + + return $result; + } +} \ No newline at end of file