Skip to content

Commit

Permalink
allow to decorate dependency factory services
Browse files Browse the repository at this point in the history
  • Loading branch information
goetas committed Jan 31, 2020
1 parent 93927c3 commit 5f19e28
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/Doctrine/Migrations/DependencyFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,12 @@ public function setService(string $id, $service) : void
$this->dependencies[$id] = $service;
}

public function decorateService(string $id, callable $callback) : void
{
$this->assertNotFrozen();
$this->dependencies[$id] = $callback($this);
}

private function getMetadataStorageConfiguration() : MetadataStorageConfiguration
{
return $this->getDependency(MetadataStorageConfiguration::class, static function () : MetadataStorageConfiguration {
Expand Down
53 changes: 53 additions & 0 deletions tests/Doctrine/Migrations/Tests/DependencyFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
use Doctrine\Migrations\Exception\MissingDependency;
use Doctrine\Migrations\Finder\GlobFinder;
use Doctrine\Migrations\Finder\RecursiveRegexFinder;
use Doctrine\Migrations\Version\Comparator;
use Doctrine\Migrations\Version\Version;
use Doctrine\ORM\EntityManager;
use PHPUnit\Framework\MockObject\MockObject;
use stdClass;
Expand Down Expand Up @@ -41,6 +43,57 @@ public function setUp() : void
$this->configuration = new Configuration();
}

public function testTwoLevelDecorator() : void
{
$this->configuration->addMigrationsDirectory('foo', 'bar');

$di = DependencyFactory::fromConnection(new ExistingConfiguration($this->configuration), new ExistingConnection($this->connection));

$di->decorateService(Comparator::class, function (DependencyFactory $dependencyFactory) : Comparator {
$oldComparator = $dependencyFactory->getVersionComparator();

return new class($oldComparator) implements Comparator
{
/** @var Comparator */
private $comparator;

public function __construct(Comparator $comparator)
{
$this->comparator = $comparator;
}

public function compare(Version $a, Version $b) : int
{
return $this->comparator->compare($a, $b) * 10;
}
};
});

$di->decorateService(Comparator::class, function (DependencyFactory $dependencyFactory) : Comparator {
$oldComparator = $dependencyFactory->getVersionComparator();

return new class($oldComparator) implements Comparator
{
/** @var Comparator */
private $comparator;

public function __construct(Comparator $comparator)
{
$this->comparator = $comparator;
}

public function compare(Version $a, Version $b) : int
{
return $this->comparator->compare($a, $b) * -10;
}
};
});

$comparator = $di->getVersionComparator();

self::assertSame(100, $comparator->compare(new Version('1'), new Version('2')));
}

public function testFreeze() : void
{
$this->configuration->addMigrationsDirectory('foo', 'bar');
Expand Down

0 comments on commit 5f19e28

Please sign in to comment.