diff --git a/README.md b/README.md index e70ec94f..8ee9ae97 100644 --- a/README.md +++ b/README.md @@ -60,6 +60,7 @@ These are all the configuration options and their default value between brackets - "command": Extra SQL to initialize the database connection (none) - "tables": Comma separated list of tables to publish (defaults to 'all') - "mapping": Comma separated list of table/column mappings (no mappping) +- "geometrySrid": SRID assumed when converting from WKT to geometry (`4326`) - "middlewares": List of middlewares to load (`cors`) - "controllers": List of controllers to load (`records,geojson,openapi,status`) - "customControllers": List of user custom controllers to load (no default) @@ -605,6 +606,7 @@ For spatial support there is an extra set of filters that can be applied on geom - "siv": spatial is valid (geometry is valid) These filters are based on OGC standards and so is the WKT specification in which the geometry columns are represented. +Note that the SRID that is assumed when converting from WKT to geometry is specified by the config variable `geometrySrid` and defaults to 4326 (WGS 84). #### GeoJSON diff --git a/api.include.php b/api.include.php index 31b01c93..d6b143b3 100644 --- a/api.include.php +++ b/api.include.php @@ -5037,10 +5037,12 @@ public function ping(ServerRequestInterface $request): ResponseInterface class ColumnConverter { private $driver; + private $geometrySrid; - public function __construct(string $driver) + public function __construct(string $driver, int $geometrySrid) { $this->driver = $driver; + $this->geometrySrid = $geometrySrid; } public function convertColumnValue(ReflectedColumn $column): string @@ -5066,12 +5068,13 @@ public function convertColumnValue(ReflectedColumn $column): string } } if ($column->isGeometry()) { + $srid = $this->geometrySrid; switch ($this->driver) { case 'mysql': case 'pgsql': - return "ST_GeomFromText(?)"; + return "ST_GeomFromText(?,$srid)"; case 'sqlsrv': - return "geometry::STGeomFromText(?,0)"; + return "geometry::STGeomFromText(?,$srid)"; } } return '?'; @@ -5114,10 +5117,10 @@ class ColumnsBuilder private $driver; private $converter; - public function __construct(string $driver) + public function __construct(string $driver, int $geometrySrid) { $this->driver = $driver; - $this->converter = new ColumnConverter($driver); + $this->converter = new ColumnConverter($driver, $geometrySrid); } public function getOffsetLimit(int $offset, int $limit): string @@ -5238,10 +5241,12 @@ public function getIncrement(ReflectedTable $table, array $columnValues): string class ConditionsBuilder { private $driver; + private $geometrySrid; - public function __construct(string $driver) + public function __construct(string $driver, int $geometrySrid) { $this->driver = $driver; + $this->geometrySrid = $geometrySrid; } private function getConditionSql(Condition $condition, array &$arguments): string @@ -5402,14 +5407,15 @@ private function hasSpatialArgument(string $operator): bool private function getSpatialFunctionCall(string $functionName, string $column, bool $hasArgument): string { + $srid = $this->geometrySrid; switch ($this->driver) { case 'mysql': case 'pgsql': - $argument = $hasArgument ? 'ST_GeomFromText(?)' : ''; + $argument = $hasArgument ? "ST_GeomFromText(?,$srid)" : ''; return "$functionName($column, $argument)=TRUE"; case 'sqlsrv': $functionName = str_replace('ST_', 'ST', $functionName); - $argument = $hasArgument ? 'geometry::STGeomFromText(?,0)' : ''; + $argument = $hasArgument ? "geometry::STGeomFromText(?,$srid)" : ''; return "$column.$functionName($argument)=1"; case 'sqlite': $argument = $hasArgument ? '?' : '0'; @@ -5572,6 +5578,7 @@ class GenericDB private $conditions; private $columns; private $converter; + private $geometrySrid; private function getDsn(): string { @@ -5655,13 +5662,13 @@ private function initPdo(): bool $this->mapper = new RealNameMapper($this->mapping); $this->reflection = new GenericReflection($this->pdo, $this->driver, $this->database, $this->tables, $this->mapper); $this->definition = new GenericDefinition($this->pdo, $this->driver, $this->database, $this->tables, $this->mapper); - $this->conditions = new ConditionsBuilder($this->driver); - $this->columns = new ColumnsBuilder($this->driver); + $this->conditions = new ConditionsBuilder($this->driver, $this->geometrySrid); + $this->columns = new ColumnsBuilder($this->driver, $this->geometrySrid); $this->converter = new DataConverter($this->driver); return $result; } - public function __construct(string $driver, string $address, int $port, string $database, string $command, array $tables, array $mapping, string $username, string $password) + public function __construct(string $driver, string $address, int $port, string $database, string $command, array $tables, array $mapping, string $username, string $password, int $geometrySrid) { $this->driver = $driver; $this->address = $address; @@ -5672,10 +5679,11 @@ public function __construct(string $driver, string $address, int $port, string $ $this->mapping = $mapping; $this->username = $username; $this->password = $password; + $this->geometrySrid = $geometrySrid; $this->initPdo(); } - public function reconstruct(string $driver, string $address, int $port, string $database, string $command, array $tables, array $mapping, string $username, string $password): bool + public function reconstruct(string $driver, string $address, int $port, string $database, string $command, array $tables, array $mapping, string $username, string $password, int $geometrySrid): bool { if ($driver) { $this->driver = $driver; @@ -5704,6 +5712,9 @@ public function reconstruct(string $driver, string $address, int $port, string $ if ($password) { $this->password = $password; } + if ($geometrySrid) { + $this->geometrySrid = $geometrySrid; + } return $this->initPdo(); } @@ -8817,13 +8828,13 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface class ReconnectMiddleware extends Middleware { - private $reflection; + private $config; private $db; public function __construct(Router $router, Responder $responder, Config $config, string $middleware, ReflectionService $reflection, GenericDB $db) { parent::__construct($router, $responder, $config, $middleware); - $this->reflection = $reflection; + $this->config = $config; $this->db = $db; } @@ -8833,7 +8844,7 @@ private function getDriver(): string if ($driverHandler) { return call_user_func($driverHandler); } - return ''; + return $this->config->getDriver(); } private function getAddress(): string @@ -8842,7 +8853,7 @@ private function getAddress(): string if ($addressHandler) { return call_user_func($addressHandler); } - return ''; + return $this->config->getAddress(); } private function getPort(): int @@ -8851,7 +8862,7 @@ private function getPort(): int if ($portHandler) { return call_user_func($portHandler); } - return 0; + return $this->config->getPort(); } private function getDatabase(): string @@ -8860,7 +8871,7 @@ private function getDatabase(): string if ($databaseHandler) { return call_user_func($databaseHandler); } - return ''; + return $this->config->getDatabase(); } private function getCommand(): string @@ -8869,7 +8880,7 @@ private function getCommand(): string if ($commandHandler) { return call_user_func($commandHandler); } - return ''; + return $this->config->getCommand(); } private function getTables(): array @@ -8878,7 +8889,7 @@ private function getTables(): array if ($tablesHandler) { return call_user_func($tablesHandler); } - return []; + return $this->config->getTables(); } private function getMapping(): array @@ -8887,7 +8898,7 @@ private function getMapping(): array if ($mappingHandler) { return call_user_func($mappingHandler); } - return []; + return $this->config->getMapping(); } private function getUsername(): string @@ -8896,7 +8907,7 @@ private function getUsername(): string if ($usernameHandler) { return call_user_func($usernameHandler); } - return ''; + return $this->config->getUsername(); } private function getPassword(): string @@ -8905,7 +8916,16 @@ private function getPassword(): string if ($passwordHandler) { return call_user_func($passwordHandler); } - return ''; + return $this->config->getPassword(); + } + + private function getGeometrySrid(): int + { + $geometrySridHandler = $this->getProperty('geometrySridHandler', ''); + if ($geometrySridHandler) { + return call_user_func($geometrySridHandler); + } + return $this->config->getGeometrySrid(); } public function process(ServerRequestInterface $request, RequestHandlerInterface $next): ResponseInterface @@ -8919,9 +8939,8 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface $mapping = $this->getMapping(); $username = $this->getUsername(); $password = $this->getPassword(); - if ($driver || $address || $port || $database || $command || $tables || $mapping || $username || $password) { - $this->db->reconstruct($driver, $address, $port, $database, $command, $tables, $mapping, $username, $password); - } + $geometrySrid = $this->getGeometrySrid(); + $this->db->reconstruct($driver, $address, $port, $database, $command, $tables, $mapping, $username, $password, $geometrySrid); return $next->handle($request); } } @@ -11634,7 +11653,8 @@ public function __construct(Config $config) $config->getTables(), $config->getMapping(), $config->getUsername(), - $config->getPassword() + $config->getPassword(), + $config->getGeometrySrid() ); $prefix = sprintf('phpcrudapi-%s-', substr(md5(__FILE__), 0, 8)); $cache = CacheFactory::create($config->getCacheType(), $prefix, $config->getCachePath()); @@ -11833,6 +11853,7 @@ class Config 'debug' => false, 'basePath' => '', 'openApiBase' => '{"info":{"title":"PHP-CRUD-API","version":"1.0.0"}}', + 'geometrySrid' => 4326, ]; private function getDefaultDriver(array $values): string @@ -12022,6 +12043,11 @@ public function getOpenApiBase(): array { return json_decode($this->values['openApiBase'], true); } + + public function getGeometrySrid(): int + { + return $this->values['geometrySrid']; + } } } diff --git a/api.php b/api.php index 7b3c8b81..aa5e138e 100644 --- a/api.php +++ b/api.php @@ -5037,10 +5037,12 @@ public function ping(ServerRequestInterface $request): ResponseInterface class ColumnConverter { private $driver; + private $geometrySrid; - public function __construct(string $driver) + public function __construct(string $driver, int $geometrySrid) { $this->driver = $driver; + $this->geometrySrid = $geometrySrid; } public function convertColumnValue(ReflectedColumn $column): string @@ -5066,12 +5068,13 @@ public function convertColumnValue(ReflectedColumn $column): string } } if ($column->isGeometry()) { + $srid = $this->geometrySrid; switch ($this->driver) { case 'mysql': case 'pgsql': - return "ST_GeomFromText(?)"; + return "ST_GeomFromText(?,$srid)"; case 'sqlsrv': - return "geometry::STGeomFromText(?,0)"; + return "geometry::STGeomFromText(?,$srid)"; } } return '?'; @@ -5114,10 +5117,10 @@ class ColumnsBuilder private $driver; private $converter; - public function __construct(string $driver) + public function __construct(string $driver, int $geometrySrid) { $this->driver = $driver; - $this->converter = new ColumnConverter($driver); + $this->converter = new ColumnConverter($driver, $geometrySrid); } public function getOffsetLimit(int $offset, int $limit): string @@ -5238,10 +5241,12 @@ public function getIncrement(ReflectedTable $table, array $columnValues): string class ConditionsBuilder { private $driver; + private $geometrySrid; - public function __construct(string $driver) + public function __construct(string $driver, int $geometrySrid) { $this->driver = $driver; + $this->geometrySrid = $geometrySrid; } private function getConditionSql(Condition $condition, array &$arguments): string @@ -5402,14 +5407,15 @@ private function hasSpatialArgument(string $operator): bool private function getSpatialFunctionCall(string $functionName, string $column, bool $hasArgument): string { + $srid = $this->geometrySrid; switch ($this->driver) { case 'mysql': case 'pgsql': - $argument = $hasArgument ? 'ST_GeomFromText(?)' : ''; + $argument = $hasArgument ? "ST_GeomFromText(?,$srid)" : ''; return "$functionName($column, $argument)=TRUE"; case 'sqlsrv': $functionName = str_replace('ST_', 'ST', $functionName); - $argument = $hasArgument ? 'geometry::STGeomFromText(?,0)' : ''; + $argument = $hasArgument ? "geometry::STGeomFromText(?,$srid)" : ''; return "$column.$functionName($argument)=1"; case 'sqlite': $argument = $hasArgument ? '?' : '0'; @@ -5572,6 +5578,7 @@ class GenericDB private $conditions; private $columns; private $converter; + private $geometrySrid; private function getDsn(): string { @@ -5655,13 +5662,13 @@ private function initPdo(): bool $this->mapper = new RealNameMapper($this->mapping); $this->reflection = new GenericReflection($this->pdo, $this->driver, $this->database, $this->tables, $this->mapper); $this->definition = new GenericDefinition($this->pdo, $this->driver, $this->database, $this->tables, $this->mapper); - $this->conditions = new ConditionsBuilder($this->driver); - $this->columns = new ColumnsBuilder($this->driver); + $this->conditions = new ConditionsBuilder($this->driver, $this->geometrySrid); + $this->columns = new ColumnsBuilder($this->driver, $this->geometrySrid); $this->converter = new DataConverter($this->driver); return $result; } - public function __construct(string $driver, string $address, int $port, string $database, string $command, array $tables, array $mapping, string $username, string $password) + public function __construct(string $driver, string $address, int $port, string $database, string $command, array $tables, array $mapping, string $username, string $password, int $geometrySrid) { $this->driver = $driver; $this->address = $address; @@ -5672,10 +5679,11 @@ public function __construct(string $driver, string $address, int $port, string $ $this->mapping = $mapping; $this->username = $username; $this->password = $password; + $this->geometrySrid = $geometrySrid; $this->initPdo(); } - public function reconstruct(string $driver, string $address, int $port, string $database, string $command, array $tables, array $mapping, string $username, string $password): bool + public function reconstruct(string $driver, string $address, int $port, string $database, string $command, array $tables, array $mapping, string $username, string $password, int $geometrySrid): bool { if ($driver) { $this->driver = $driver; @@ -5704,6 +5712,9 @@ public function reconstruct(string $driver, string $address, int $port, string $ if ($password) { $this->password = $password; } + if ($geometrySrid) { + $this->geometrySrid = $geometrySrid; + } return $this->initPdo(); } @@ -8817,13 +8828,13 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface class ReconnectMiddleware extends Middleware { - private $reflection; + private $config; private $db; public function __construct(Router $router, Responder $responder, Config $config, string $middleware, ReflectionService $reflection, GenericDB $db) { parent::__construct($router, $responder, $config, $middleware); - $this->reflection = $reflection; + $this->config = $config; $this->db = $db; } @@ -8833,7 +8844,7 @@ private function getDriver(): string if ($driverHandler) { return call_user_func($driverHandler); } - return ''; + return $this->config->getDriver(); } private function getAddress(): string @@ -8842,7 +8853,7 @@ private function getAddress(): string if ($addressHandler) { return call_user_func($addressHandler); } - return ''; + return $this->config->getAddress(); } private function getPort(): int @@ -8851,7 +8862,7 @@ private function getPort(): int if ($portHandler) { return call_user_func($portHandler); } - return 0; + return $this->config->getPort(); } private function getDatabase(): string @@ -8860,7 +8871,7 @@ private function getDatabase(): string if ($databaseHandler) { return call_user_func($databaseHandler); } - return ''; + return $this->config->getDatabase(); } private function getCommand(): string @@ -8869,7 +8880,7 @@ private function getCommand(): string if ($commandHandler) { return call_user_func($commandHandler); } - return ''; + return $this->config->getCommand(); } private function getTables(): array @@ -8878,7 +8889,7 @@ private function getTables(): array if ($tablesHandler) { return call_user_func($tablesHandler); } - return []; + return $this->config->getTables(); } private function getMapping(): array @@ -8887,7 +8898,7 @@ private function getMapping(): array if ($mappingHandler) { return call_user_func($mappingHandler); } - return []; + return $this->config->getMapping(); } private function getUsername(): string @@ -8896,7 +8907,7 @@ private function getUsername(): string if ($usernameHandler) { return call_user_func($usernameHandler); } - return ''; + return $this->config->getUsername(); } private function getPassword(): string @@ -8905,7 +8916,16 @@ private function getPassword(): string if ($passwordHandler) { return call_user_func($passwordHandler); } - return ''; + return $this->config->getPassword(); + } + + private function getGeometrySrid(): int + { + $geometrySridHandler = $this->getProperty('geometrySridHandler', ''); + if ($geometrySridHandler) { + return call_user_func($geometrySridHandler); + } + return $this->config->getGeometrySrid(); } public function process(ServerRequestInterface $request, RequestHandlerInterface $next): ResponseInterface @@ -8919,9 +8939,8 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface $mapping = $this->getMapping(); $username = $this->getUsername(); $password = $this->getPassword(); - if ($driver || $address || $port || $database || $command || $tables || $mapping || $username || $password) { - $this->db->reconstruct($driver, $address, $port, $database, $command, $tables, $mapping, $username, $password); - } + $geometrySrid = $this->getGeometrySrid(); + $this->db->reconstruct($driver, $address, $port, $database, $command, $tables, $mapping, $username, $password, $geometrySrid); return $next->handle($request); } } @@ -11634,7 +11653,8 @@ public function __construct(Config $config) $config->getTables(), $config->getMapping(), $config->getUsername(), - $config->getPassword() + $config->getPassword(), + $config->getGeometrySrid() ); $prefix = sprintf('phpcrudapi-%s-', substr(md5(__FILE__), 0, 8)); $cache = CacheFactory::create($config->getCacheType(), $prefix, $config->getCachePath()); @@ -11833,6 +11853,7 @@ class Config 'debug' => false, 'basePath' => '', 'openApiBase' => '{"info":{"title":"PHP-CRUD-API","version":"1.0.0"}}', + 'geometrySrid' => 4326, ]; private function getDefaultDriver(array $values): string @@ -12022,6 +12043,11 @@ public function getOpenApiBase(): array { return json_decode($this->values['openApiBase'], true); } + + public function getGeometrySrid(): int + { + return $this->values['geometrySrid']; + } } } diff --git a/src/Tqdev/PhpCrudApi/Api.php b/src/Tqdev/PhpCrudApi/Api.php index 98dfc4ee..1661180f 100644 --- a/src/Tqdev/PhpCrudApi/Api.php +++ b/src/Tqdev/PhpCrudApi/Api.php @@ -59,7 +59,8 @@ public function __construct(Config $config) $config->getTables(), $config->getMapping(), $config->getUsername(), - $config->getPassword() + $config->getPassword(), + $config->getGeometrySrid() ); $prefix = sprintf('phpcrudapi-%s-', substr(md5(__FILE__), 0, 8)); $cache = CacheFactory::create($config->getCacheType(), $prefix, $config->getCachePath()); diff --git a/src/Tqdev/PhpCrudApi/Config.php b/src/Tqdev/PhpCrudApi/Config.php index 79bd238c..489ac766 100644 --- a/src/Tqdev/PhpCrudApi/Config.php +++ b/src/Tqdev/PhpCrudApi/Config.php @@ -25,6 +25,7 @@ class Config 'debug' => false, 'basePath' => '', 'openApiBase' => '{"info":{"title":"PHP-CRUD-API","version":"1.0.0"}}', + 'geometrySrid' => 4326, ]; private function getDefaultDriver(array $values): string @@ -215,4 +216,9 @@ public function getOpenApiBase(): array { return json_decode($this->values['openApiBase'], true); } + + public function getGeometrySrid(): int + { + return $this->values['geometrySrid']; + } } diff --git a/src/Tqdev/PhpCrudApi/Database/ColumnConverter.php b/src/Tqdev/PhpCrudApi/Database/ColumnConverter.php index 023e3a38..3a296e97 100644 --- a/src/Tqdev/PhpCrudApi/Database/ColumnConverter.php +++ b/src/Tqdev/PhpCrudApi/Database/ColumnConverter.php @@ -7,10 +7,12 @@ class ColumnConverter { private $driver; + private $geometrySrid; - public function __construct(string $driver) + public function __construct(string $driver, int $geometrySrid) { $this->driver = $driver; + $this->geometrySrid = $geometrySrid; } public function convertColumnValue(ReflectedColumn $column): string @@ -36,12 +38,13 @@ public function convertColumnValue(ReflectedColumn $column): string } } if ($column->isGeometry()) { + $srid = $this->geometrySrid; switch ($this->driver) { case 'mysql': case 'pgsql': - return "ST_GeomFromText(?)"; + return "ST_GeomFromText(?,$srid)"; case 'sqlsrv': - return "geometry::STGeomFromText(?,0)"; + return "geometry::STGeomFromText(?,$srid)"; } } return '?'; diff --git a/src/Tqdev/PhpCrudApi/Database/ColumnsBuilder.php b/src/Tqdev/PhpCrudApi/Database/ColumnsBuilder.php index 895cc031..d2042b0c 100644 --- a/src/Tqdev/PhpCrudApi/Database/ColumnsBuilder.php +++ b/src/Tqdev/PhpCrudApi/Database/ColumnsBuilder.php @@ -10,10 +10,10 @@ class ColumnsBuilder private $driver; private $converter; - public function __construct(string $driver) + public function __construct(string $driver, int $geometrySrid) { $this->driver = $driver; - $this->converter = new ColumnConverter($driver); + $this->converter = new ColumnConverter($driver, $geometrySrid); } public function getOffsetLimit(int $offset, int $limit): string diff --git a/src/Tqdev/PhpCrudApi/Database/ConditionsBuilder.php b/src/Tqdev/PhpCrudApi/Database/ConditionsBuilder.php index f4a47773..e536e941 100644 --- a/src/Tqdev/PhpCrudApi/Database/ConditionsBuilder.php +++ b/src/Tqdev/PhpCrudApi/Database/ConditionsBuilder.php @@ -14,10 +14,12 @@ class ConditionsBuilder { private $driver; + private $geometrySrid; - public function __construct(string $driver) + public function __construct(string $driver, int $geometrySrid) { $this->driver = $driver; + $this->geometrySrid = $geometrySrid; } private function getConditionSql(Condition $condition, array &$arguments): string @@ -178,14 +180,15 @@ private function hasSpatialArgument(string $operator): bool private function getSpatialFunctionCall(string $functionName, string $column, bool $hasArgument): string { + $srid = $this->geometrySrid; switch ($this->driver) { case 'mysql': case 'pgsql': - $argument = $hasArgument ? 'ST_GeomFromText(?)' : ''; + $argument = $hasArgument ? "ST_GeomFromText(?,$srid)" : ''; return "$functionName($column, $argument)=TRUE"; case 'sqlsrv': $functionName = str_replace('ST_', 'ST', $functionName); - $argument = $hasArgument ? 'geometry::STGeomFromText(?,0)' : ''; + $argument = $hasArgument ? "geometry::STGeomFromText(?,$srid)" : ''; return "$column.$functionName($argument)=1"; case 'sqlite': $argument = $hasArgument ? '?' : '0'; diff --git a/src/Tqdev/PhpCrudApi/Database/GenericDB.php b/src/Tqdev/PhpCrudApi/Database/GenericDB.php index f04c77b6..0a3e6247 100644 --- a/src/Tqdev/PhpCrudApi/Database/GenericDB.php +++ b/src/Tqdev/PhpCrudApi/Database/GenericDB.php @@ -25,6 +25,7 @@ class GenericDB private $conditions; private $columns; private $converter; + private $geometrySrid; private function getDsn(): string { @@ -108,13 +109,13 @@ private function initPdo(): bool $this->mapper = new RealNameMapper($this->mapping); $this->reflection = new GenericReflection($this->pdo, $this->driver, $this->database, $this->tables, $this->mapper); $this->definition = new GenericDefinition($this->pdo, $this->driver, $this->database, $this->tables, $this->mapper); - $this->conditions = new ConditionsBuilder($this->driver); - $this->columns = new ColumnsBuilder($this->driver); + $this->conditions = new ConditionsBuilder($this->driver, $this->geometrySrid); + $this->columns = new ColumnsBuilder($this->driver, $this->geometrySrid); $this->converter = new DataConverter($this->driver); return $result; } - public function __construct(string $driver, string $address, int $port, string $database, string $command, array $tables, array $mapping, string $username, string $password) + public function __construct(string $driver, string $address, int $port, string $database, string $command, array $tables, array $mapping, string $username, string $password, int $geometrySrid) { $this->driver = $driver; $this->address = $address; @@ -125,10 +126,11 @@ public function __construct(string $driver, string $address, int $port, string $ $this->mapping = $mapping; $this->username = $username; $this->password = $password; + $this->geometrySrid = $geometrySrid; $this->initPdo(); } - public function reconstruct(string $driver, string $address, int $port, string $database, string $command, array $tables, array $mapping, string $username, string $password): bool + public function reconstruct(string $driver, string $address, int $port, string $database, string $command, array $tables, array $mapping, string $username, string $password, int $geometrySrid): bool { if ($driver) { $this->driver = $driver; @@ -157,6 +159,9 @@ public function reconstruct(string $driver, string $address, int $port, string $ if ($password) { $this->password = $password; } + if ($geometrySrid) { + $this->geometrySrid = $geometrySrid; + } return $this->initPdo(); } diff --git a/src/Tqdev/PhpCrudApi/Middleware/ReconnectMiddleware.php b/src/Tqdev/PhpCrudApi/Middleware/ReconnectMiddleware.php index eb7ef323..78137a5c 100644 --- a/src/Tqdev/PhpCrudApi/Middleware/ReconnectMiddleware.php +++ b/src/Tqdev/PhpCrudApi/Middleware/ReconnectMiddleware.php @@ -14,13 +14,13 @@ class ReconnectMiddleware extends Middleware { - private $reflection; + private $config; private $db; public function __construct(Router $router, Responder $responder, Config $config, string $middleware, ReflectionService $reflection, GenericDB $db) { parent::__construct($router, $responder, $config, $middleware); - $this->reflection = $reflection; + $this->config = $config; $this->db = $db; } @@ -30,7 +30,7 @@ private function getDriver(): string if ($driverHandler) { return call_user_func($driverHandler); } - return ''; + return $this->config->getDriver(); } private function getAddress(): string @@ -39,7 +39,7 @@ private function getAddress(): string if ($addressHandler) { return call_user_func($addressHandler); } - return ''; + return $this->config->getAddress(); } private function getPort(): int @@ -48,7 +48,7 @@ private function getPort(): int if ($portHandler) { return call_user_func($portHandler); } - return 0; + return $this->config->getPort(); } private function getDatabase(): string @@ -57,7 +57,7 @@ private function getDatabase(): string if ($databaseHandler) { return call_user_func($databaseHandler); } - return ''; + return $this->config->getDatabase(); } private function getCommand(): string @@ -66,7 +66,7 @@ private function getCommand(): string if ($commandHandler) { return call_user_func($commandHandler); } - return ''; + return $this->config->getCommand(); } private function getTables(): array @@ -75,7 +75,7 @@ private function getTables(): array if ($tablesHandler) { return call_user_func($tablesHandler); } - return []; + return $this->config->getTables(); } private function getMapping(): array @@ -84,7 +84,7 @@ private function getMapping(): array if ($mappingHandler) { return call_user_func($mappingHandler); } - return []; + return $this->config->getMapping(); } private function getUsername(): string @@ -93,7 +93,7 @@ private function getUsername(): string if ($usernameHandler) { return call_user_func($usernameHandler); } - return ''; + return $this->config->getUsername(); } private function getPassword(): string @@ -102,7 +102,16 @@ private function getPassword(): string if ($passwordHandler) { return call_user_func($passwordHandler); } - return ''; + return $this->config->getPassword(); + } + + private function getGeometrySrid(): int + { + $geometrySridHandler = $this->getProperty('geometrySridHandler', ''); + if ($geometrySridHandler) { + return call_user_func($geometrySridHandler); + } + return $this->config->getGeometrySrid(); } public function process(ServerRequestInterface $request, RequestHandlerInterface $next): ResponseInterface @@ -116,9 +125,8 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface $mapping = $this->getMapping(); $username = $this->getUsername(); $password = $this->getPassword(); - if ($driver || $address || $port || $database || $command || $tables || $mapping || $username || $password) { - $this->db->reconstruct($driver, $address, $port, $database, $command, $tables, $mapping, $username, $password); - } + $geometrySrid = $this->getGeometrySrid(); + $this->db->reconstruct($driver, $address, $port, $database, $command, $tables, $mapping, $username, $password, $geometrySrid); return $next->handle($request); } } diff --git a/test.php b/test.php index c99f6e4d..bccfeb44 100644 --- a/test.php +++ b/test.php @@ -163,7 +163,8 @@ function loadFixture(string $dir, Config $config) getTables($config), getMapping($config), getUsername($config), - getPassword($config) + getPassword($config), + $config->getGeometrySrid() ); $pdo = $db->pdo(); $file = preg_replace('/--.*$/m', '', $file); diff --git a/tests/fixtures/blog_mysql.sql b/tests/fixtures/blog_mysql.sql index 13db2e20..0e3e44ba 100644 --- a/tests/fixtures/blog_mysql.sql +++ b/tests/fixtures/blog_mysql.sql @@ -106,17 +106,17 @@ CREATE TABLE `countries` ( ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci; INSERT INTO `countries` (`name`, `shape`) VALUES -('Left', ST_GeomFromText('POLYGON ((30 10, 40 40, 20 40, 10 20, 30 10))')), -('Right', ST_GeomFromText('POLYGON ((70 10, 80 40, 60 40, 50 20, 70 10))')), -('Point', ST_GeomFromText('POINT (30 10)')), -('Line', ST_GeomFromText('LINESTRING (30 10, 10 30, 40 40)')), -('Poly1', ST_GeomFromText('POLYGON ((30 10, 40 40, 20 40, 10 20, 30 10))')), -('Poly2', ST_GeomFromText('POLYGON ((35 10, 45 45, 15 40, 10 20, 35 10),(20 30, 35 35, 30 20, 20 30))')), -('Mpoint', ST_GeomFromText('MULTIPOINT (10 40, 40 30, 20 20, 30 10)')), -('Mline', ST_GeomFromText('MULTILINESTRING ((10 10, 20 20, 10 40),(40 40, 30 30, 40 20, 30 10))')), -('Mpoly1', ST_GeomFromText('MULTIPOLYGON (((30 20, 45 40, 10 40, 30 20)),((15 5, 40 10, 10 20, 5 10, 15 5)))')), -('Mpoly2', ST_GeomFromText('MULTIPOLYGON (((40 40, 20 45, 45 30, 40 40)),((20 35, 10 30, 10 10, 30 5, 45 20, 20 35),(30 20, 20 15, 20 25, 30 20)))')), -('Gcoll', ST_GeomFromText('GEOMETRYCOLLECTION(POINT(4 6),LINESTRING(4 6,7 10))')); +('Left', ST_GeomFromText('POLYGON ((30 10, 40 40, 20 40, 10 20, 30 10))', 4326)), +('Right', ST_GeomFromText('POLYGON ((70 10, 80 40, 60 40, 50 20, 70 10))', 4326)), +('Point', ST_GeomFromText('POINT (30 10)', 4326)), +('Line', ST_GeomFromText('LINESTRING (30 10, 10 30, 40 40)', 4326)), +('Poly1', ST_GeomFromText('POLYGON ((30 10, 40 40, 20 40, 10 20, 30 10))', 4326)), +('Poly2', ST_GeomFromText('POLYGON ((35 10, 45 45, 15 40, 10 20, 35 10),(20 30, 35 35, 30 20, 20 30))', 4326)), +('Mpoint', ST_GeomFromText('MULTIPOINT (10 40, 40 30, 20 20, 30 10)', 4326)), +('Mline', ST_GeomFromText('MULTILINESTRING ((10 10, 20 20, 10 40),(40 40, 30 30, 40 20, 30 10))', 4326)), +('Mpoly1', ST_GeomFromText('MULTIPOLYGON (((30 20, 45 40, 10 40, 30 20)),((15 5, 40 10, 10 20, 5 10, 15 5)))', 4326)), +('Mpoly2', ST_GeomFromText('MULTIPOLYGON (((40 40, 20 45, 45 30, 40 40)),((20 35, 10 30, 10 10, 30 5, 45 20, 20 35),(30 20, 20 15, 20 25, 30 20)))', 4326)), +('Gcoll', ST_GeomFromText('GEOMETRYCOLLECTION(POINT(4 6),LINESTRING(4 6,7 10))', 4326)); DROP TABLE IF EXISTS `events`; CREATE TABLE `events` ( diff --git a/tests/fixtures/blog_pgsql.sql b/tests/fixtures/blog_pgsql.sql index 7f80774a..683a2300 100644 --- a/tests/fixtures/blog_pgsql.sql +++ b/tests/fixtures/blog_pgsql.sql @@ -241,17 +241,17 @@ INSERT INTO "users" ("username", "password", "api_key", "location") VALUES -- INSERT INTO "countries" ("name", "shape") VALUES -('Left', ST_GeomFromText('POLYGON ((30 10, 40 40, 20 40, 10 20, 30 10))')), -('Right', ST_GeomFromText('POLYGON ((70 10, 80 40, 60 40, 50 20, 70 10))')), -('Point', ST_GeomFromText('POINT (30 10)')), -('Line', ST_GeomFromText('LINESTRING (30 10, 10 30, 40 40)')), -('Poly1', ST_GeomFromText('POLYGON ((30 10, 40 40, 20 40, 10 20, 30 10))')), -('Poly2', ST_GeomFromText('POLYGON ((35 10, 45 45, 15 40, 10 20, 35 10),(20 30, 35 35, 30 20, 20 30))')), -('Mpoint', ST_GeomFromText('MULTIPOINT (10 40, 40 30, 20 20, 30 10)')), -('Mline', ST_GeomFromText('MULTILINESTRING ((10 10, 20 20, 10 40),(40 40, 30 30, 40 20, 30 10))')), -('Mpoly1', ST_GeomFromText('MULTIPOLYGON (((30 20, 45 40, 10 40, 30 20)),((15 5, 40 10, 10 20, 5 10, 15 5)))')), -('Mpoly2', ST_GeomFromText('MULTIPOLYGON (((40 40, 20 45, 45 30, 40 40)),((20 35, 10 30, 10 10, 30 5, 45 20, 20 35),(30 20, 20 15, 20 25, 30 20)))')), -('Gcoll', ST_GeomFromText('GEOMETRYCOLLECTION(POINT(4 6),LINESTRING(4 6,7 10))')); +('Left', ST_GeomFromText('POLYGON ((30 10, 40 40, 20 40, 10 20, 30 10))', 4326)), +('Right', ST_GeomFromText('POLYGON ((70 10, 80 40, 60 40, 50 20, 70 10))', 4326)), +('Point', ST_GeomFromText('POINT (30 10)', 4326)), +('Line', ST_GeomFromText('LINESTRING (30 10, 10 30, 40 40)', 4326)), +('Poly1', ST_GeomFromText('POLYGON ((30 10, 40 40, 20 40, 10 20, 30 10))', 4326)), +('Poly2', ST_GeomFromText('POLYGON ((35 10, 45 45, 15 40, 10 20, 35 10),(20 30, 35 35, 30 20, 20 30))', 4326)), +('Mpoint', ST_GeomFromText('MULTIPOINT (10 40, 40 30, 20 20, 30 10)', 4326)), +('Mline', ST_GeomFromText('MULTILINESTRING ((10 10, 20 20, 10 40),(40 40, 30 30, 40 20, 30 10))', 4326)), +('Mpoly1', ST_GeomFromText('MULTIPOLYGON (((30 20, 45 40, 10 40, 30 20)),((15 5, 40 10, 10 20, 5 10, 15 5)))', 4326)), +('Mpoly2', ST_GeomFromText('MULTIPOLYGON (((40 40, 20 45, 45 30, 40 40)),((20 35, 10 30, 10 10, 30 5, 45 20, 20 35),(30 20, 20 15, 20 25, 30 20)))', 4326)), +('Gcoll', ST_GeomFromText('GEOMETRYCOLLECTION(POINT(4 6),LINESTRING(4 6,7 10))', 4326)); -- -- Data for Name: events; Type: TABLE DATA; Schema: public; Owner: postgres diff --git a/tests/fixtures/blog_sqlsrv.sql b/tests/fixtures/blog_sqlsrv.sql index 9bcfff1d..2219c851 100644 --- a/tests/fixtures/blog_sqlsrv.sql +++ b/tests/fixtures/blog_sqlsrv.sql @@ -342,27 +342,27 @@ GO INSERT [users] ([username], [password], [api_key], [location]) VALUES (N'user2', N'$2y$10$cg7/nswxVZ0cmVIsMB/pVOh1OfcHScBJGq7Xu4KF9dFEQgRZ8HWe.', NULL, NULL) GO -INSERT [countries] ([name], [shape]) VALUES (N'Left', N'POLYGON ((30 10, 40 40, 20 40, 10 20, 30 10))') +INSERT [countries] ([name], [shape]) VALUES (N'Left', geometry::STGeomFromText('POLYGON ((30 10, 40 40, 20 40, 10 20, 30 10))', 4326)) GO -INSERT [countries] ([name], [shape]) VALUES (N'Right', N'POLYGON ((70 10, 80 40, 60 40, 50 20, 70 10))') +INSERT [countries] ([name], [shape]) VALUES (N'Right', geometry::STGeomFromText('POLYGON ((70 10, 80 40, 60 40, 50 20, 70 10))', 4326)) GO -INSERT [countries] ([name], [shape]) VALUES (N'Point', N'POINT (30 10)') +INSERT [countries] ([name], [shape]) VALUES (N'Point', geometry::STGeomFromText('POINT (30 10)', 4326)) GO -INSERT [countries] ([name], [shape]) VALUES (N'Line', N'LINESTRING (30 10, 10 30, 40 40)') +INSERT [countries] ([name], [shape]) VALUES (N'Line', geometry::STGeomFromText('LINESTRING (30 10, 10 30, 40 40)', 4326)) GO -INSERT [countries] ([name], [shape]) VALUES (N'Poly1', N'POLYGON ((30 10, 40 40, 20 40, 10 20, 30 10))') +INSERT [countries] ([name], [shape]) VALUES (N'Poly1', geometry::STGeomFromText('POLYGON ((30 10, 40 40, 20 40, 10 20, 30 10))', 4326)) GO -INSERT [countries] ([name], [shape]) VALUES (N'Poly2', N'POLYGON ((35 10, 45 45, 15 40, 10 20, 35 10),(20 30, 35 35, 30 20, 20 30))') +INSERT [countries] ([name], [shape]) VALUES (N'Poly2', geometry::STGeomFromText('POLYGON ((35 10, 45 45, 15 40, 10 20, 35 10),(20 30, 35 35, 30 20, 20 30))', 4326)) GO -INSERT [countries] ([name], [shape]) VALUES (N'Mpoint', N'MULTIPOINT (10 40, 40 30, 20 20, 30 10)') +INSERT [countries] ([name], [shape]) VALUES (N'Mpoint', geometry::STGeomFromText('MULTIPOINT (10 40, 40 30, 20 20, 30 10)', 4326)) GO -INSERT [countries] ([name], [shape]) VALUES (N'Mline', N'MULTILINESTRING ((10 10, 20 20, 10 40),(40 40, 30 30, 40 20, 30 10))') +INSERT [countries] ([name], [shape]) VALUES (N'Mline', geometry::STGeomFromText('MULTILINESTRING ((10 10, 20 20, 10 40),(40 40, 30 30, 40 20, 30 10))', 4326)) GO -INSERT [countries] ([name], [shape]) VALUES (N'Mpoly1', N'MULTIPOLYGON (((30 20, 45 40, 10 40, 30 20)),((15 5, 40 10, 10 20, 5 10, 15 5)))') +INSERT [countries] ([name], [shape]) VALUES (N'Mpoly1', geometry::STGeomFromText('MULTIPOLYGON (((30 20, 45 40, 10 40, 30 20)),((15 5, 40 10, 10 20, 5 10, 15 5)))', 4326)) GO -INSERT [countries] ([name], [shape]) VALUES (N'Mpoly2', N'MULTIPOLYGON (((40 40, 20 45, 45 30, 40 40)),((20 35, 10 30, 10 10, 30 5, 45 20, 20 35),(30 20, 20 15, 20 25, 30 20)))') +INSERT [countries] ([name], [shape]) VALUES (N'Mpoly2', geometry::STGeomFromText('MULTIPOLYGON (((40 40, 20 45, 45 30, 40 40)),((20 35, 10 30, 10 10, 30 5, 45 20, 20 35),(30 20, 20 15, 20 25, 30 20)))', 4326)) GO -INSERT [countries] ([name], [shape]) VALUES (N'Gcoll', N'GEOMETRYCOLLECTION(POINT(4 6),LINESTRING(4 6,7 10))') +INSERT [countries] ([name], [shape]) VALUES (N'Gcoll', geometry::STGeomFromText('GEOMETRYCOLLECTION(POINT(4 6),LINESTRING(4 6,7 10))', 4326)) GO INSERT [events] ([name], [datetime], [visitors]) VALUES (N'Launch', N'2016-01-01 13:01:01', 0)