Implement the SQLite extension for doctrine via a middleware instead of an deprecated event listener

This commit is contained in:
Jan Böhmer 2023-12-05 23:50:07 +01:00
parent 641b47b189
commit dd0f8ec97c
6 changed files with 137 additions and 21 deletions

View file

@ -62,6 +62,7 @@
"symfony/http-kernel": "6.4.*",
"symfony/mailer": "6.4.*",
"symfony/monolog-bundle": "^3.1",
"symfony/polyfill-php82": "^1.28",
"symfony/process": "6.4.*",
"symfony/property-access": "6.4.*",
"symfony/property-info": "6.4.*",

81
composer.lock generated
View file

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "933822c8b0473aeb6ac94e3d7f74f6dd",
"content-hash": "2379491b49e2abf92312a1623292b620",
"packages": [
{
"name": "api-platform/core",
@ -10425,6 +10425,85 @@
],
"time": "2023-01-26T09:26:14+00:00"
},
{
"name": "symfony/polyfill-php82",
"version": "v1.28.0",
"source": {
"type": "git",
"url": "https://github.com/symfony/polyfill-php82.git",
"reference": "7716bea9c86776fb3362d6b52fe1fc9471056a49"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/symfony/polyfill-php82/zipball/7716bea9c86776fb3362d6b52fe1fc9471056a49",
"reference": "7716bea9c86776fb3362d6b52fe1fc9471056a49",
"shasum": ""
},
"require": {
"php": ">=7.1"
},
"type": "library",
"extra": {
"branch-alias": {
"dev-main": "1.28-dev"
},
"thanks": {
"name": "symfony/polyfill",
"url": "https://github.com/symfony/polyfill"
}
},
"autoload": {
"files": [
"bootstrap.php"
],
"psr-4": {
"Symfony\\Polyfill\\Php82\\": ""
},
"classmap": [
"Resources/stubs"
]
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Nicolas Grekas",
"email": "p@tchwork.com"
},
{
"name": "Symfony Community",
"homepage": "https://symfony.com/contributors"
}
],
"description": "Symfony polyfill backporting some PHP 8.2+ features to lower PHP versions",
"homepage": "https://symfony.com",
"keywords": [
"compatibility",
"polyfill",
"portable",
"shim"
],
"support": {
"source": "https://github.com/symfony/polyfill-php82/tree/v1.28.0"
},
"funding": [
{
"url": "https://symfony.com/sponsor",
"type": "custom"
},
{
"url": "https://github.com/fabpot",
"type": "github"
},
{
"url": "https://tidelift.com/funding/github/packagist/symfony/symfony",
"type": "tidelift"
}
],
"time": "2023-08-25T17:27:25+00:00"
},
{
"name": "symfony/polyfill-php83",
"version": "v1.28.0",

View file

@ -1,11 +1,8 @@
<?php
declare(strict_types=1);
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2022 Jan Böhmer (https://github.com/jbtronics)
* Copyright (C) 2019 - 2023 Jan Böhmer (https://github.com/jbtronics)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
@ -20,29 +17,31 @@ declare(strict_types=1);
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
namespace App\Doctrine;
declare(strict_types=1);
namespace App\Doctrine\Middleware;
use App\Exceptions\InvalidRegexException;
use Doctrine\Bundle\DoctrineBundle\Attribute\AsDoctrineListener;
use Doctrine\Bundle\DoctrineBundle\EventSubscriber\EventSubscriberInterface;
use Doctrine\DBAL\Event\ConnectionEventArgs;
use Doctrine\DBAL\Events;
use Doctrine\DBAL\Driver\Connection;
use Doctrine\DBAL\Driver\Middleware\AbstractDriverMiddleware;
use Doctrine\DBAL\Platforms\SqlitePlatform;
/**
* This subscriber is used to add the regexp operator to the SQLite platform.
* This middleware is used to add the regexp operator to the SQLite platform.
* As a PHP callback is called for every entry to compare it is most likely much slower than using regex on MySQL.
* But as regex is not often used, this should be fine for most use cases, also it is almost impossible to implement a better solution.
*/
#[AsDoctrineListener(Events::postConnect)]
class SQLiteRegexExtension
class SQLiteRegexExtensionMiddlewareDriver extends AbstractDriverMiddleware
{
public function postConnect(ConnectionEventArgs $eventArgs): void
public function connect(#[\SensitiveParameter] array $params): Connection
{
$connection = $eventArgs->getConnection();
//Do connect process first
$connection = parent::connect($params); // TODO: Change the autogenerated stub
//We only execute this on SQLite databases
if ($connection->getDatabasePlatform() instanceof SqlitePlatform) {
//Then add the functions if we are on SQLite
if ($this->getDatabasePlatform() instanceof SqlitePlatform) {
$native_connection = $connection->getNativeConnection();
//Ensure that the function really exists on the connection, as it is marked as experimental according to PHP documentation
@ -52,6 +51,9 @@ class SQLiteRegexExtension
$native_connection->sqliteCreateFunction('FIELD2', self::field2(...), 2, \PDO::SQLITE_DETERMINISTIC);
}
}
return $connection;
}
/**
@ -107,4 +109,4 @@ class SQLiteRegexExtension
return $index + 1;
}
}
}

View file

@ -0,0 +1,35 @@
<?php
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2023 Jan Böhmer (https://github.com/jbtronics)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace App\Doctrine\Middleware;
use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Driver\Middleware;
class SQLiteRegexExtensionMiddlewareWrapper implements Middleware
{
public function wrap(Driver $driver): Driver
{
return new SQLiteRegexExtensionMiddlewareDriver($driver);
}
}

View file

@ -20,7 +20,7 @@ declare(strict_types=1);
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
namespace App\Doctrine\SetSQLMode;
namespace App\Doctrine\Middleware;
use Doctrine\DBAL\Driver\Connection;
use Doctrine\DBAL\Driver\Middleware\AbstractDriverMiddleware;

View file

@ -20,7 +20,7 @@ declare(strict_types=1);
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
namespace App\Doctrine\SetSQLMode;
namespace App\Doctrine\Middleware;
use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Driver\Middleware;
@ -30,7 +30,6 @@ use Doctrine\DBAL\Driver\Middleware;
*/
class SetSQLModeMiddlewareWrapper implements Middleware
{
public function wrap(Driver $driver): Driver
{
return new SetSQLModeMiddlewareDriver($driver);