Skip to content

Commit

Permalink
Merge pull request #1 from WhereGroup/work/symfony-6-update
Browse files Browse the repository at this point in the history
Update to middleware instead of EventSubscriber
  • Loading branch information
Phocacius authored May 15, 2024
2 parents 52ab0f2 + 9fe535e commit 63dce45
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 62 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# v1.2.0
* Use middleware instead of listener
* Require doctrine/dbal >= 3.2
* Require symfony >= 6.x

# v1.1.0
* Drop compatibility workaround for PostgreSQL 10 on PHP 5.x
* Require doctrine/dbal >= 2.7 (for proper upstream PostgreSQL >= 10 support)
Expand Down
4 changes: 0 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
NOTE: [older versions](https://github.com/WhereGroup/doctrine-dbal-shims/tree/v1.0.1) contained compatibility workarounds for
PostgreSQL >= 10 on unmaintained DBAL (<=2.6.x). This support has been removed. Only the Oracle
session init logic remains.

## Oracle session variable preinitialization
Included is an event subscriber that automatically sets Oracle session variables required by DBAL and Doctrine ORM to
work properly (date formats etc).
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
"type": "library",
"license": "MIT",
"require": {
"php": ">=5.5",
"doctrine/dbal": "^2.7 || ^3"
"php": ">=8.1",
"doctrine/dbal": "^3.2"
},
"autoload": {
"psr-4": { "Wheregroup\\DoctrineDbalShims\\": "lib" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,27 @@
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;

class AddOracleSesssionInitPass extends ShimPass
class AddOracleSessionInitPass extends ShimPass
{
/** @var FileLocator */
protected $fileLocator;
protected FileLocator $fileLocator;

public function __construct()
{
$this->fileLocator = new FileLocator(realpath(__DIR__ . '/../../Resources/config'));
}

public static function isShimRequired(ContainerBuilder $container)
public static function isShimRequired(ContainerBuilder $container): bool
{
// Can't hurt
return true;
}

public function process(ContainerBuilder $container)
public function process(ContainerBuilder $container): void
{
$loader = new XmlFileLoader($container, $this->fileLocator);
$loader->import('oracle-listener-init-session-vars.xml');
}

public static function register(ContainerBuilder $container)
public static function register(ContainerBuilder $container): void
{
/**
* Register for execution BEFORE doctrine event listeners are collected and
Expand Down
2 changes: 1 addition & 1 deletion lib/DependencyInjection/Compiler/PassIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class PassIndex
public static function getAllPasses()
{
return array(
new AddOracleSesssionInitPass(),
new AddOracleSessionInitPass(),
);
}

Expand Down
18 changes: 7 additions & 11 deletions lib/DependencyInjection/Compiler/ShimPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,26 @@
namespace Wheregroup\DoctrineDbalShims\DependencyInjection\Compiler;


use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

abstract class ShimPass implements ShimPassInterface
abstract class ShimPass implements CompilerPassInterface
{
public abstract static function isShimRequired(ContainerBuilder $container): bool;

/**
* Registers the compiler pass (invariantly) to the container builder.
* @param ContainerBuilder $container
*/
public static function register(ContainerBuilder $container)
public static function register(ContainerBuilder $container): void
{
$container->addCompilerPass(new static());
}

/**
* Registers the compiler pass to the container builder ONLY IF it
* is required / applicable to current DBAL setup.
*
* @param ContainerBuilder $container
*/
public static function autoRegister(ContainerBuilder $container)
public static function autoRegister(ContainerBuilder $container): void
{
if (static::isShimRequired($container)) {
static::register($container);
Expand All @@ -35,12 +35,8 @@ public static function autoRegister(ContainerBuilder $container)
* before a certain other pass (by class name).
* Performs no action unless a compiler pass of given $className was already
* added to the builder.
*
* @param ContainerBuilder $container
* @param string $className
* @return bool if pass was injected
*/
protected static function registerBefore(ContainerBuilder $container, $className)
protected static function registerBefore(ContainerBuilder $container, string $className): bool
{
$passConfig = $container->getCompilerPassConfig();
$searchPasses = $passConfig->getBeforeOptimizationPasses();
Expand Down
30 changes: 0 additions & 30 deletions lib/DependencyInjection/Compiler/ShimPassInterface.php

This file was deleted.

16 changes: 10 additions & 6 deletions lib/Event/Listeners/OnDemandOracleSessionInit.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
namespace Wheregroup\DoctrineDbalShims\Event\Listeners;


use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Driver\AbstractOracleDriver;
use Doctrine\DBAL\Event\ConnectionEventArgs;
use Doctrine\DBAL\Event\Listeners\OracleSessionInit;
use Doctrine\DBAL\Driver\OCI8\Middleware\InitializeSession;
use Doctrine\DBAL\Platforms\OraclePlatform;

/**
* Ensures DBAL-required session variables (like date format) are set on
Expand All @@ -15,12 +16,15 @@
* multiple connections to different database servers, without the need to
* preconfigure the listener to only process certain connections by name.
*/
class OnDemandOracleSessionInit extends OracleSessionInit
class OnDemandOracleSessionInit extends InitializeSession
{
public function postConnect(ConnectionEventArgs $args)
public function wrap(Driver $driver): Driver
{
if ($args->getConnection()->getDriver() instanceof AbstractOracleDriver) {
parent::postConnect($args);
$platform = $driver->getDatabasePlatform();
if ($platform instanceof OraclePlatform) {
return parent::wrap($driver);
}

return $driver;
}
}
2 changes: 1 addition & 1 deletion lib/Resources/config/oracle-listener-init-session-vars.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="dbal.listener.oracle.init_session_vars" class="Wheregroup\DoctrineDbalShims\Event\Listeners\OnDemandOracleSessionInit">
<tag name="doctrine.event_subscriber" />
<tag name="doctrine.middleware" />
</service>
</services>
</container>

0 comments on commit 63dce45

Please sign in to comment.