Skip to content

Commit

Permalink
Merge pull request #1820 from doctrine/2.12.x-merge-up-into-2.13.x_od…
Browse files Browse the repository at this point in the history
…3vo4jA
  • Loading branch information
ostrolucky authored Sep 1, 2024
2 parents 9926a4a + f633ea3 commit ca59d84
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 32 deletions.
4 changes: 2 additions & 2 deletions config/messenger.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@
</service>

<service id="doctrine.orm.messenger.doctrine_schema_subscriber" class="Symfony\Bridge\Doctrine\SchemaListener\MessengerTransportDoctrineSchemaSubscriber">
<argument type="tagged" tag="messenger.receiver" />
<argument type="tagged_iterator" tag="messenger.receiver" />
<tag name="doctrine.event_subscriber" />
</service>
<service id="doctrine.orm.messenger.doctrine_schema_listener" class="Symfony\Bridge\Doctrine\SchemaListener\MessengerTransportDoctrineSchemaListener">
<argument type="tagged" tag="messenger.receiver" />
<argument type="tagged_iterator" tag="messenger.receiver" />
<tag name="doctrine.event_listener" event="postGenerateSchema" />
<tag name="doctrine.event_listener" event="onSchemaCreateTable" />
</service>
Expand Down
6 changes: 3 additions & 3 deletions config/orm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -148,19 +148,19 @@
<tag name="doctrine.event_subscriber" />
</service>
<service id="doctrine.orm.listeners.doctrine_token_provider_schema_subscriber" class="Symfony\Bridge\Doctrine\SchemaListener\RememberMeTokenProviderDoctrineSchemaSubscriber">
<argument type="tagged" tag="security.remember_me_handler" />
<argument type="tagged_iterator" tag="security.remember_me_handler" />
<tag name="doctrine.event_subscriber" />
</service>
<service id="doctrine.orm.listeners.doctrine_token_provider_schema_listener" class="Symfony\Bridge\Doctrine\SchemaListener\RememberMeTokenProviderDoctrineSchemaListener">
<argument type="tagged" tag="security.remember_me_handler" />
<argument type="tagged_iterator" tag="security.remember_me_handler" />
<tag name="doctrine.event_listener" event="postGenerateSchema" />
</service>
<service id="doctrine.orm.listeners.pdo_session_handler_schema_listener" class="Symfony\Bridge\Doctrine\SchemaListener\PdoSessionHandlerSchemaListener">
<argument type="service" id="session.handler" />
<tag name="doctrine.event_listener" event="postGenerateSchema" />
</service>
<service id="doctrine.orm.listeners.lock_store_schema_listener" class="Symfony\Bridge\Doctrine\SchemaListener\LockStoreSchemaListener">
<argument type="tagged" tag="lock.store" />
<argument type="tagged_iterator" tag="lock.store" />
<tag name="doctrine.event_listener" event="postGenerateSchema" />
</service>

Expand Down
2 changes: 1 addition & 1 deletion docs/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -740,7 +740,7 @@ configuration for the ORM and there are several configuration options that you
can control. The following configuration options exist for a mapping:

``type``
One of ``annotation``, ``xml``, ``yml``, ``php`` or ``staticphp``.
One of ``attribute``, ``xml``, ``yml``, ``php`` or ``staticphp``.
This specifies which type of metadata type your mapping uses.

``dir``
Expand Down
12 changes: 5 additions & 7 deletions docs/custom-id-generators.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ and implement the custom logic in the ``generate(EntityManager $em, $entity)``
method. Before Doctrine bundle 2.3, custom ID generators were always created
without any constructor arguments.

Starting with Doctrine bundle 2.3, the ``CustomIdGenerator`` annotation can be
Starting with Doctrine bundle 2.3, the ``CustomIdGenerator`` attribute can be
used to reference any services tagged with the ``doctrine.id_generator`` tag.
If you enable autoconfiguration (which is the default most of the time), Symfony
will add this tag for you automatically if you implement your own id-generators.
Expand All @@ -28,12 +28,10 @@ are provided: ``doctrine.ulid_generator`` to generate ULIDs, and
*/
class User
{
/**
* @Id
* @Column(type="uuid")
* @ORM\GeneratedValue(strategy="CUSTOM")
* @ORM\CustomIdGenerator("doctrine.uuid_generator")
*/
#[ORM\Id]
#[ORM\Column(type: 'uuid')]
#[ORM\GeneratedValue(strategy: 'CUSTOM')]
#[ORM\CustomIdGenerator('doctrine.uuid_generator')]
private $id;
// ....
Expand Down
2 changes: 1 addition & 1 deletion docs/entity-listeners.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Entity Listeners
================

Entity listeners that are services must be registered with the entity listener
resolver. On top of the annotation in the entity class, you have to tag the
resolver. On top of the annotation/attribute in the entity class, you have to tag the
service with ``doctrine.orm.entity_listener`` for it to be automatically added
to the resolver. Use the (optional) ``entity_manager`` attribute to specify
which entity manager it should be registered with.
Expand Down
1 change: 0 additions & 1 deletion src/Command/DropDatabaseDoctrineCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int

try {
if ($shouldDropDatabase) {
/** @psalm-suppress TypeDoesNotContainType Bogus error, Doctrine\DBAL\Schema\AbstractSchemaManager<Doctrine\DBAL\Platforms\AbstractPlatform> does contain Doctrine\DBAL\Schema\SQLiteSchemaManager */
if ($schemaManager instanceof SQLiteSchemaManager) {
// dropDatabase() is deprecated for Sqlite
$connection->close();
Expand Down
36 changes: 19 additions & 17 deletions src/DependencyInjection/Compiler/MiddlewaresPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Symfony\Component\DependencyInjection\ChildDefinition;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

use function array_key_exists;
use function array_keys;
Expand Down Expand Up @@ -44,20 +45,21 @@ public function process(ContainerBuilder $container): void
}

foreach (array_keys($container->getParameter('doctrine.connections')) as $name) {
$middlewareDefs = [];
$middlewareRefs = [];
$i = 0;
foreach ($middlewareAbstractDefs as $id => $abstractDef) {
if (isset($middlewareConnections[$id]) && ! array_key_exists($name, $middlewareConnections[$id])) {
continue;
}

$middlewareDefs[$id] = [
$childDef = $container->setDefinition(
sprintf('%s.%s', $id, $name),
new ChildDefinition($id),
),
++$i,
];
$childDef = $container->setDefinition(
$childId = sprintf('%s.%s', $id, $name),
(new ChildDefinition($id))
->setTags($abstractDef->getTags())->clearTag('doctrine.middleware')
->setAutoconfigured($abstractDef->isAutoconfigured())
->setAutowired($abstractDef->isAutowired()),
);
$middlewareRefs[$id] = [new Reference($childId), ++$i];

if (! is_subclass_of($abstractDef->getClass(), ConnectionNameAwareInterface::class)) {
continue;
Expand All @@ -66,21 +68,21 @@ public function process(ContainerBuilder $container): void
$childDef->addMethodCall('setConnectionName', [$name]);
}

$middlewareDefs = array_map(
static fn ($id, $def) => [
$middlewareRefs = array_map(
static fn (string $id, array $ref) => [
$middlewareConnections[$id][$name] ?? $middlewarePriorities[$id] ?? 0,
$def[1],
$def[0],
$ref[1],
$ref[0],
],
array_keys($middlewareDefs),
array_values($middlewareDefs),
array_keys($middlewareRefs),
array_values($middlewareRefs),
);
uasort($middlewareDefs, static fn ($a, $b) => $b[0] <=> $a[0] ?: $a[1] <=> $b[1]);
$middlewareDefs = array_map(static fn ($value) => $value[2], $middlewareDefs);
uasort($middlewareRefs, static fn (array $a, array $b): int => $b[0] <=> $a[0] ?: $a[1] <=> $b[1]);
$middlewareRefs = array_map(static fn (array $value): Reference => $value[2], $middlewareRefs);

$container
->getDefinition(sprintf('doctrine.dbal.%s_connection.configuration', $name))
->addMethodCall('setMiddlewares', [$middlewareDefs]);
->addMethodCall('setMiddlewares', [$middlewareRefs]);
}
}
}
27 changes: 27 additions & 0 deletions tests/DependencyInjection/Compiler/MiddlewarePassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
use Symfony\Component\DependencyInjection\Reference;

use function array_map;
use function class_exists;
Expand Down Expand Up @@ -306,6 +307,28 @@ public function testAddMiddlewareOrderingWithInheritedPriorityPerConnection(): v
$this->assertMiddlewareOrdering($container, 'conn2', $expectedMiddlewares);
}

public function testInjectedMiddlewaresPreserveParentDefinition(): void
{
$container = $this->createContainer(static function (ContainerBuilder $container): void {
$container
->register('middleware', PHP7Middleware::class)
->setPublic(true)
->setAutowired(true)
->setAutoconfigured(true)
->addTag('doctrine.middleware')->addTag('custom.tag');
$container
->setAlias('conf_conn', 'doctrine.dbal.conn1_connection.configuration')
->setPublic(true); // Avoid removal and inlining
});
$this->assertMiddlewareInjected($container, 'conn', PHP7Middleware::class);
$this->assertNotNull($definition = $container->getDefinition('middleware.conn1'));
$this->assertCount(1, $middlewares = $this->getMiddlewaresForConn($container, 'conn', PHP7Middleware::class));
$this->assertSame(true, $definition->isAutowired());
$this->assertSame(true, $definition->isAutoconfigured());
$this->assertSame(['custom.tag' => [[]]], $definition->getTags());
$this->assertSame($middlewares[0], $definition);
}

/** @requires PHP 8 */
public function testAddMiddlewareOrderingWithAttributeForAutoconfiguration(): void
{
Expand Down Expand Up @@ -466,6 +489,10 @@ private function getMiddlewaresForConn(ContainerBuilder $container, string $conn
}

foreach ($call[1][0] as $middlewareDef) {
if ($middlewareDef instanceof Reference) {
$middlewareDef = $container->getDefinition($middlewareDef->__toString());
}

if (isset($middlewareClass) && $middlewareDef->getClass() !== $middlewareClass) {
continue;
}
Expand Down

0 comments on commit ca59d84

Please sign in to comment.