From dd0f8ec97cd195957554d2f4826a6ff8bbe5265e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20B=C3=B6hmer?= Date: Tue, 5 Dec 2023 23:50:07 +0100 Subject: [PATCH] Implement the SQLite extension for doctrine via a middleware instead of an deprecated event listener --- composer.json | 1 + composer.lock | 81 ++++++++++++++++++- .../SQLiteRegexExtensionMiddlewareDriver.php} | 36 +++++---- .../SQLiteRegexExtensionMiddlewareWrapper.php | 35 ++++++++ .../SetSQLModeMiddlewareDriver.php | 2 +- .../SetSQLModeMiddlewareWrapper.php | 3 +- 6 files changed, 137 insertions(+), 21 deletions(-) rename src/Doctrine/{SQLiteRegexExtension.php => Middleware/SQLiteRegexExtensionMiddlewareDriver.php} (83%) create mode 100644 src/Doctrine/Middleware/SQLiteRegexExtensionMiddlewareWrapper.php rename src/Doctrine/{SetSQLMode => Middleware}/SetSQLModeMiddlewareDriver.php (98%) rename src/Doctrine/{SetSQLMode => Middleware}/SetSQLModeMiddlewareWrapper.php (97%) diff --git a/composer.json b/composer.json index 562adecb..3296f037 100644 --- a/composer.json +++ b/composer.json @@ -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.*", diff --git a/composer.lock b/composer.lock index 4af6059f..fb00fec2 100644 --- a/composer.lock +++ b/composer.lock @@ -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", diff --git a/src/Doctrine/SQLiteRegexExtension.php b/src/Doctrine/Middleware/SQLiteRegexExtensionMiddlewareDriver.php similarity index 83% rename from src/Doctrine/SQLiteRegexExtension.php rename to src/Doctrine/Middleware/SQLiteRegexExtensionMiddlewareDriver.php index 2407880d..71c0ba2b 100644 --- a/src/Doctrine/SQLiteRegexExtension.php +++ b/src/Doctrine/Middleware/SQLiteRegexExtensionMiddlewareDriver.php @@ -1,11 +1,8 @@ . */ -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; } -} +} \ No newline at end of file diff --git a/src/Doctrine/Middleware/SQLiteRegexExtensionMiddlewareWrapper.php b/src/Doctrine/Middleware/SQLiteRegexExtensionMiddlewareWrapper.php new file mode 100644 index 00000000..42aafaad --- /dev/null +++ b/src/Doctrine/Middleware/SQLiteRegexExtensionMiddlewareWrapper.php @@ -0,0 +1,35 @@ +. + */ + +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); + } +} \ No newline at end of file diff --git a/src/Doctrine/SetSQLMode/SetSQLModeMiddlewareDriver.php b/src/Doctrine/Middleware/SetSQLModeMiddlewareDriver.php similarity index 98% rename from src/Doctrine/SetSQLMode/SetSQLModeMiddlewareDriver.php rename to src/Doctrine/Middleware/SetSQLModeMiddlewareDriver.php index 5501b231..f526dcbd 100644 --- a/src/Doctrine/SetSQLMode/SetSQLModeMiddlewareDriver.php +++ b/src/Doctrine/Middleware/SetSQLModeMiddlewareDriver.php @@ -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 . */ -namespace App\Doctrine\SetSQLMode; +namespace App\Doctrine\Middleware; use Doctrine\DBAL\Driver\Connection; use Doctrine\DBAL\Driver\Middleware\AbstractDriverMiddleware; diff --git a/src/Doctrine/SetSQLMode/SetSQLModeMiddlewareWrapper.php b/src/Doctrine/Middleware/SetSQLModeMiddlewareWrapper.php similarity index 97% rename from src/Doctrine/SetSQLMode/SetSQLModeMiddlewareWrapper.php rename to src/Doctrine/Middleware/SetSQLModeMiddlewareWrapper.php index 34320fa5..3307bc7f 100644 --- a/src/Doctrine/SetSQLMode/SetSQLModeMiddlewareWrapper.php +++ b/src/Doctrine/Middleware/SetSQLModeMiddlewareWrapper.php @@ -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 . */ -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);