Skip to content

Commit

Permalink
allow custom sorter as service in DI
Browse files Browse the repository at this point in the history
  • Loading branch information
goetas committed Dec 1, 2019
1 parent e11f3a8 commit aea823d
Showing 1 changed file with 17 additions and 16 deletions.
33 changes: 17 additions & 16 deletions lib/Doctrine/Migrations/DependencyFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Symfony\Component\Stopwatch\Stopwatch as SymfonyStopwatch;
use function array_key_exists;
use function preg_quote;
use function sprintf;

Expand All @@ -46,10 +47,12 @@
*/
class DependencyFactory
{
public const MIGRATIONS_SORTER = 'Doctrine\Migrations\MigrationsSorter';

/** @var Configuration */
private $configuration;

/** @var object[] */
/** @var object[]|callable[] */
private $dependencies = [];

/** @var LoggerInterface */
Expand All @@ -58,9 +61,6 @@ class DependencyFactory
/** @var Connection */
private $connection;

/** @var callable */
private $sorter;

/** @var EntityManagerInterface|null */
private $em;

Expand Down Expand Up @@ -98,6 +98,13 @@ private function getConnection() : Connection
return $this->connection;
}

public function getSorter() : ?callable
{
return $this->getDependency(self::MIGRATIONS_SORTER, static function () {
return null;
});
}

public function getEventDispatcher() : EventDispatcher
{
return $this->getDependency(EventDispatcher::class, function () : EventDispatcher {
Expand Down Expand Up @@ -196,12 +203,6 @@ public function getMigrationsFinder() : MigrationFinder
});
}

public function setSorter(callable $sorter) : void
{
$this->assertNotFrozen();
$this->sorter = $sorter;
}

public function getMigrationRepository() : MigrationRepository
{
return $this->getDependency(MigrationRepository::class, function () : MigrationRepository {
Expand All @@ -210,13 +211,13 @@ public function getMigrationRepository() : MigrationRepository
$this->getConfiguration()->getMigrationDirectories(),
$this->getMigrationsFinder(),
new MigrationFactory($this->getConnection(), $this->getLogger()),
$this->sorter
$this->getSorter()
);
});
}

/**
* @param mixed $service
* @param object|callable $service
*/
public function setService(string $id, $service) : void
{
Expand Down Expand Up @@ -369,12 +370,12 @@ public function getRollup() : Rollup
/**
* @return mixed
*/
private function getDependency(string $className, callable $callback)
private function getDependency(string $id, callable $callback)
{
if (! isset($this->dependencies[$className])) {
$this->dependencies[$className] = $callback();
if (! array_key_exists($id, $this->dependencies)) {
$this->dependencies[$id] = $callback();
}

return $this->dependencies[$className];
return $this->dependencies[$id];
}
}

0 comments on commit aea823d

Please sign in to comment.