Skip to content

Commit

Permalink
bug #58865 Dynamically fix compatibility with doctrine/data-fixtures …
Browse files Browse the repository at this point in the history
…v2 (greg0ire)

This PR was merged into the 5.4 branch.

Discussion
----------

Dynamically fix compatibility with doctrine/data-fixtures v2

| Q             | A
| ------------- | ---
| Branch?       | 5.4
| Bug fix?      | yes
| New feature?  | no <!-- please update src/**/CHANGELOG.md files -->
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Issues        | see explanation below
| License       | MIT

While working on [allowing v2 of doctrine/data-fixtures in the bundle](doctrine/DoctrineFixturesBundle#457), I stumbled upon an issue that only affects some versions of Symfony that still have a `ContainerAwareLoader` class.

The signature of `ContainerAwareLoader::addFixture()` is not compatible with the v2 signature of the `Loader` interface from `doctrine/data-fixtures`, as per this fatal error:

```
Declaration of Symfony\Bridge\Doctrine\DataFixtures\ContainerAwareLoader::addFixture(Doctrine\Common\DataFixtures\FixtureInterface $fixture)
             must be compatible with Doctrine\Common\DataFixtures\Loader::addFixture(Doctrine\Common\DataFixtures\FixtureInterface $fixture): void
```

Classes that extend ContainerAwareLoader have to also extend Loader, meaning this is no breaking change because it can be argued that the incompatibility of the extending class would be with the Loader interface.

Closes #58861, Closes #58863

Commits
-------

1812aafe065 Dynamically fix compatibility with doctrine/data-fixtures v2
  • Loading branch information
nicolas-grekas committed Nov 20, 2024
2 parents 253aef2 + 9c7bc53 commit 43ed5e3
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 deletions.
35 changes: 35 additions & 0 deletions DataFixtures/AddFixtureImplementation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bridge\Doctrine\DataFixtures;

use Doctrine\Common\DataFixtures\FixtureInterface;
use Doctrine\Common\DataFixtures\ReferenceRepository;

if (method_exists(ReferenceRepository::class, 'getReferences')) {
/** @internal */
trait AddFixtureImplementation
{
public function addFixture(FixtureInterface $fixture)
{
$this->doAddFixture($fixture);
}
}
} else {
/** @internal */
trait AddFixtureImplementation
{
public function addFixture(FixtureInterface $fixture): void
{
$this->doAddFixture($fixture);
}
}
}
7 changes: 3 additions & 4 deletions DataFixtures/ContainerAwareLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,16 @@
*/
class ContainerAwareLoader extends Loader
{
use AddFixtureImplementation;

private $container;

public function __construct(ContainerInterface $container)
{
$this->container = $container;
}

/**
* {@inheritdoc}
*/
public function addFixture(FixtureInterface $fixture)
private function doAddFixture(FixtureInterface $fixture): void
{
if ($fixture instanceof ContainerAwareInterface) {
$fixture->setContainer($this->container);
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"symfony/var-dumper": "^4.4|^5.0|^6.0",
"doctrine/annotations": "^1.10.4|^2",
"doctrine/collections": "^1.0|^2.0",
"doctrine/data-fixtures": "^1.1",
"doctrine/data-fixtures": "^1.1|^2",
"doctrine/dbal": "^2.13.1|^3|^4",
"doctrine/orm": "^2.7.4|^3",
"psr/log": "^1|^2|^3"
Expand Down

0 comments on commit 43ed5e3

Please sign in to comment.