Skip to content

Commit b66f55c

Browse files
authored
Merge pull request #5786 from morozov/deprecate-event-manager
Deprecate extension via Doctrine Event Manager
2 parents 946db1d + cc8a35e commit b66f55c

File tree

19 files changed

+191
-101
lines changed

19 files changed

+191
-101
lines changed

UPGRADE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,27 @@ awareness about deprecated code.
88

99
# Upgrade to 3.5
1010

11+
## Deprecated extension via Doctrine Event Manager
12+
13+
Extension of the library behavior via Doctrine Event Manager has been deprecated.
14+
15+
The following methods and properties have been deprecated:
16+
- `AbstractPlatform::$_eventManager`,
17+
- `AbstractPlatform::getEventManager()`,
18+
- `AbstractPlatform::setEventManager()`,
19+
- `Connection::$_eventManager`,
20+
- `Connection::getEventManager()`.
21+
22+
## Deprecated extension via connection events
23+
24+
Subscription to the `postConnect` event has been deprecated. Use one of the following replacements for the standard
25+
event listeners or implement a custom middleware instead.
26+
27+
The following `postConnect` event listeners have been deprecated:
28+
1. `OracleSessionInit`. Use `Doctrine\DBAL\Driver\OCI8\Middleware\InitializeSession`.
29+
2. `SQLiteSessionInit`. Use `Doctrine\DBAL\Driver\AbstractSQLiteDriver\Middleware\EnableForeignKeys`.
30+
3. `SQLSessionInit`. Implement a custom middleware.
31+
1132
## Deprecated extension via transaction events
1233

1334
Subscription to the following events has been deprecated:

ci/github/phpunit/oci8.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
<var name="db_password" value="oracle"/>
1818
<var name="db_dbname" value="XE"/>
1919
<var name="db_charset" value="AL32UTF8" />
20-
<var name="db_event_subscribers" value="Doctrine\DBAL\Event\Listeners\OracleSessionInit"/>
2120

2221
<var name="tmpdb_driver" value="oci8"/>
2322
<var name="tmpdb_host" value="localhost"/>

ci/github/phpunit/pdo_oci.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
<var name="db_password" value="oracle"/>
1818
<var name="db_dbname" value="XE"/>
1919
<var name="db_charset" value="AL32UTF8" />
20-
<var name="db_event_subscribers" value="Doctrine\DBAL\Event\Listeners\OracleSessionInit"/>
2120

2221
<var name="tmpdb_driver" value="pdo_oci"/>
2322
<var name="tmpdb_host" value="localhost"/>

ci/github/phpunit/pdo_sqlite.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
<var name="db_driver" value="pdo_sqlite"/>
1515
<var name="db_memory" value="true"/>
16-
<var name="db_event_subscribers" value="Doctrine\DBAL\Event\Listeners\SQLiteSessionInit"/>
1716
</php>
1817

1918
<testsuites>

ci/github/phpunit/sqlite3.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
<var name="db_driver" value="sqlite3"/>
1515
<var name="db_memory" value="true"/>
16-
<var name="db_event_subscribers" value="Doctrine\DBAL\Event\Listeners\SQLiteSessionInit"/>
1716
</php>
1817

1918
<testsuites>

docs/en/reference/events.rst

Lines changed: 0 additions & 63 deletions
This file was deleted.

docs/en/sidebar.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
reference/types
1212
reference/schema-manager
1313
reference/schema-representation
14-
reference/events
1514
reference/security
1615
reference/supporting-other-databases
1716
reference/portability

psalm.xml.dist

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,11 @@
118118
<referencedClass name="Doctrine\DBAL\Event\TransactionBeginEventArgs"/>
119119
<referencedClass name="Doctrine\DBAL\Event\TransactionCommitEventArgs"/>
120120
<referencedClass name="Doctrine\DBAL\Event\TransactionRollBackEventArgs"/>
121+
<referencedClass name="Doctrine\DBAL\Event\ConnectionEventArgs"/>
122+
<referencedClass name="Doctrine\DBAL\Event\Listeners\OracleSessionInit"/>
123+
<referencedClass name="Doctrine\DBAL\Event\Listeners\SQLSessionInit"/>
124+
<referencedClass name="Doctrine\DBAL\Event\Listeners\SQLiteSessionInit"/>
125+
<referencedClass name="Doctrine\DBAL\Events"/>
121126
</errorLevel>
122127
</DeprecatedClass>
123128
<DeprecatedConstant>
@@ -142,9 +147,15 @@
142147
TODO: remove in 4.0.0
143148
-->
144149
<file name="src/Connection.php"/>
150+
<file name="src/Connections/PrimaryReadReplicaConnection.php"/>
151+
<file name="src/Event/Listeners/OracleSessionInit.php"/>
152+
<file name="src/Event/Listeners/SQLSessionInit.php"/>
153+
<file name="src/Event/Listeners/SQLiteSessionInit.php"/>
145154
<file name="src/Platforms/AbstractPlatform.php"/>
146155
<file name="src/Schema/AbstractSchemaManager.php"/>
147156
<file name="tests/ConnectionTest.php"/>
157+
<file name="tests/Events/OracleSessionInitTest.php"/>
158+
<file name="tests/Events/SQLSessionInitTest.php"/>
148159
<file name="tests/Functional/Schema/SchemaManagerFunctionalTestCase.php"/>
149160
<file name="tests/Platforms/AbstractPlatformTestCase.php"/>
150161
</errorLevel>
@@ -481,6 +492,11 @@
481492
TODO: remove in 4.0.0
482493
-->
483494
<referencedMethod name="Doctrine\DBAL\Query\QueryBuilder::getConnection"/>
495+
<!--
496+
TODO: remove in 4.0.0
497+
-->
498+
<referencedMethod name="Doctrine\DBAL\Connection::getEventManager"/>
499+
<referencedMethod name="Doctrine\DBAL\Platforms\AbstractPlatform::setEventManager"/>
484500
</errorLevel>
485501
</DeprecatedMethod>
486502
<DeprecatedProperty>
@@ -534,6 +550,11 @@
534550
TODO: remove in 4.0.0
535551
-->
536552
<referencedProperty name="Doctrine\DBAL\Schema\SchemaDiff::$orphanedForeignKeys"/>
553+
<!--
554+
TODO: remove in 4.0.0
555+
-->
556+
<referencedProperty name="Doctrine\DBAL\Connection::$_eventManager"/>
557+
<referencedProperty name="Doctrine\DBAL\Platforms\AbstractPlatform::$_eventManager"/>
537558
</errorLevel>
538559
</DeprecatedProperty>
539560
<DocblockTypeContradiction>

src/Connection.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,11 @@ class Connection
8080
/** @var Configuration */
8181
protected $_config;
8282

83-
/** @var EventManager */
83+
/**
84+
* @deprecated
85+
*
86+
* @var EventManager
87+
*/
8488
protected $_eventManager;
8589

8690
/**
@@ -256,10 +260,19 @@ public function getConfiguration()
256260
/**
257261
* Gets the EventManager used by the Connection.
258262
*
263+
* @deprecated
264+
*
259265
* @return EventManager
260266
*/
261267
public function getEventManager()
262268
{
269+
Deprecation::triggerIfCalledFromOutside(
270+
'doctrine/dbal',
271+
'https://github.com/doctrine/dbal/issues/5784',
272+
'%s is deprecated.',
273+
__METHOD__,
274+
);
275+
263276
return $this->_eventManager;
264277
}
265278

@@ -340,6 +353,13 @@ public function connect()
340353
}
341354

342355
if ($this->_eventManager->hasListeners(Events::postConnect)) {
356+
Deprecation::trigger(
357+
'doctrine/dbal',
358+
'https://github.com/doctrine/dbal/issues/5784',
359+
'Subscribing to %s events is deprecated. Implement a middleware instead.',
360+
Events::postConnect,
361+
);
362+
343363
$eventArgs = new Event\ConnectionEventArgs($this);
344364
$this->_eventManager->dispatchEvent(Events::postConnect, $eventArgs);
345365
}

src/Connections/PrimaryReadReplicaConnection.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Doctrine\DBAL\Events;
1414
use Doctrine\DBAL\Exception;
1515
use Doctrine\DBAL\Statement;
16+
use Doctrine\Deprecations\Deprecation;
1617
use InvalidArgumentException;
1718

1819
use function array_rand;
@@ -199,6 +200,13 @@ protected function performConnect(?string $connectionName = null): bool
199200
}
200201

201202
if ($this->_eventManager->hasListeners(Events::postConnect)) {
203+
Deprecation::trigger(
204+
'doctrine/dbal',
205+
'https://github.com/doctrine/dbal/issues/5784',
206+
'Subscribing to %s events is deprecated. Implement a middleware instead.',
207+
Events::postConnect,
208+
);
209+
202210
$eventArgs = new ConnectionEventArgs($this);
203211
$this->_eventManager->dispatchEvent(Events::postConnect, $eventArgs);
204212
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Doctrine\DBAL\Driver\AbstractSQLiteDriver\Middleware;
4+
5+
use Doctrine\DBAL\Driver;
6+
use Doctrine\DBAL\Driver\Connection;
7+
use Doctrine\DBAL\Driver\Middleware;
8+
use Doctrine\DBAL\Driver\Middleware\AbstractDriverMiddleware;
9+
10+
class EnableForeignKeys implements Middleware
11+
{
12+
public function wrap(Driver $driver): Driver
13+
{
14+
return new class ($driver) extends AbstractDriverMiddleware {
15+
/**
16+
* {@inheritDoc}
17+
*/
18+
public function connect(array $params): Connection
19+
{
20+
$connection = parent::connect($params);
21+
22+
$connection->exec('PRAGMA foreign_keys=ON');
23+
24+
return $connection;
25+
}
26+
};
27+
}
28+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Doctrine\DBAL\Driver\OCI8\Middleware;
4+
5+
use Doctrine\DBAL\Driver;
6+
use Doctrine\DBAL\Driver\Connection;
7+
use Doctrine\DBAL\Driver\Middleware;
8+
use Doctrine\DBAL\Driver\Middleware\AbstractDriverMiddleware;
9+
10+
class InitializeSession implements Middleware
11+
{
12+
public function wrap(Driver $driver): Driver
13+
{
14+
return new class ($driver) extends AbstractDriverMiddleware {
15+
/**
16+
* {@inheritDoc}
17+
*/
18+
public function connect(array $params): Connection
19+
{
20+
$connection = parent::connect($params);
21+
22+
$connection->exec(
23+
'ALTER SESSION SET'
24+
. " NLS_DATE_FORMAT = 'YYYY-MM-DD HH24:MI:SS'"
25+
. " NLS_TIME_FORMAT = 'HH24:MI:SS'"
26+
. " NLS_DATE_FORMAT = 'YYYY-MM-DD HH24:MI:SS'"
27+
. " NLS_TIMESTAMP_FORMAT = 'YYYY-MM-DD HH24:MI:SS'"
28+
. " NLS_TIMESTAMP_TZ_FORMAT = 'YYYY-MM-DD HH24:MI:SS TZH:TZM'"
29+
. " NLS_NUMERIC_CHARACTERS = '.,'",
30+
);
31+
32+
return $connection;
33+
}
34+
};
35+
}
36+
}

src/Event/ConnectionEventArgs.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
/**
99
* Event Arguments used when a Driver connection is established inside Doctrine\DBAL\Connection.
10+
*
11+
* @deprecated
1012
*/
1113
class ConnectionEventArgs extends EventArgs
1214
{

src/Event/Listeners/OracleSessionInit.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
* NLS_DATE_FORMAT="YYYY-MM-DD HH24:MI:SS"
2424
* NLS_TIMESTAMP_FORMAT="YYYY-MM-DD HH24:MI:SS"
2525
* NLS_TIMESTAMP_TZ_FORMAT="YYYY-MM-DD HH24:MI:SS TZH:TZM"
26+
*
27+
* @deprecated Use {@see \Doctrine\DBAL\Driver\OCI8\Middleware\InitializeSession} instead.
2628
*/
2729
class OracleSessionInit implements EventSubscriber
2830
{

src/Event/Listeners/SQLSessionInit.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
/**
1111
* Session init listener for executing a single SQL statement right after a connection is opened.
12+
*
13+
* @deprecated Implement a middleware instead.
1214
*/
1315
class SQLSessionInit implements EventSubscriber
1416
{

src/Event/Listeners/SQLiteSessionInit.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Doctrine\DBAL\Events;
88
use Doctrine\DBAL\Exception;
99

10+
/** @deprecated Use {@see \Doctrine\DBAL\Driver\AbstractSQLiteDriver\Middleware\EnableForeignKeys} instead. */
1011
class SQLiteSessionInit implements EventSubscriber
1112
{
1213
/**

src/Events.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
* Container for all DBAL events.
77
*
88
* This class cannot be instantiated.
9+
*
10+
* @deprecated
911
*/
1012
final class Events
1113
{
@@ -18,6 +20,7 @@ private function __construct()
1820
{
1921
}
2022

23+
/** @deprecated */
2124
public const postConnect = 'postConnect';
2225

2326
/** @deprecated */

0 commit comments

Comments
 (0)