Skip to content

Commit

Permalink
Introduce the driver middleware interface
Browse files Browse the repository at this point in the history
  • Loading branch information
morozov committed Jul 9, 2020
1 parent 386309c commit d3fd4f6
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
27 changes: 25 additions & 2 deletions src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,19 @@
namespace Doctrine\DBAL;

use Doctrine\Common\Cache\Cache;
use Doctrine\DBAL\Driver\Middleware;
use Doctrine\DBAL\Logging\SQLLogger;

/**
* Configuration container for the Doctrine DBAL.
*
* @internal When adding a new configuration option just write a getter/setter
* pair and add the option to the _attributes array with a proper default value.
* @internal
*/
class Configuration
{
/** @var Middleware[] */
private $middlewares = [];

/**
* The attributes that are contained in the configuration.
* Values are default values.
Expand Down Expand Up @@ -108,4 +111,24 @@ public function getAutoCommit()
{
return $this->_attributes['autoCommit'] ?? true;
}

/**
* @param Middleware[] $middlewares
*
* @return $this
*/
public function setMiddlewares(array $middlewares): self
{
$this->middlewares = $middlewares;

return $this;
}

/**
* @return Middleware[]
*/
public function getMiddlewares(): array
{
return $this->middlewares;
}
}
12 changes: 12 additions & 0 deletions src/Driver/Middleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Doctrine\DBAL\Driver;

use Doctrine\DBAL\Driver;

interface Middleware
{
public function wrap(Driver $driver): Driver;
}
4 changes: 4 additions & 0 deletions src/DriverManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ public static function getConnection(

$driver = new $className();

foreach ($config->getMiddlewares() as $middleware) {
$driver = $middleware->wrap($driver);
}

$wrapperClass = Connection::class;
if (isset($params['wrapperClass'])) {
if (! is_subclass_of($params['wrapperClass'], $wrapperClass)) {
Expand Down

0 comments on commit d3fd4f6

Please sign in to comment.