Skip to content

Commit

Permalink
Keep FIFO order for prioritized registry
Browse files Browse the repository at this point in the history
  • Loading branch information
pamil committed May 25, 2020
1 parent b4492a5 commit 20a856e
Showing 1 changed file with 8 additions and 11 deletions.
19 changes: 8 additions & 11 deletions src/PrioritizedServiceRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,6 @@ final class PrioritizedServiceRegistry implements PrioritizedServiceRegistryInte
*/
private $registry = [];

/**
* @psalm-var array<int, array{service: object, priority: int}>
*
* @var array
*/
private $sortedRegistry = [];

/** @var bool */
private $sorted = true;

Expand All @@ -55,15 +48,19 @@ public function __construct(string $interface, string $context = 'service')
public function all(): iterable
{
if ($this->sorted === false) {
$this->sortedRegistry = array_reverse($this->registry);

/** @psalm-suppress InvalidPassByReference Doing PHP magic, it works this way */
array_multisort(array_column($this->sortedRegistry, 'priority'), \SORT_DESC, $this->sortedRegistry);
array_multisort(
array_column($this->registry, 'priority'),
\SORT_DESC,
array_keys($this->registry),
\SORT_ASC,
$this->registry
);

$this->sorted = true;
}

foreach ($this->sortedRegistry as $record) {
foreach ($this->registry as $record) {
yield $record['service'];
}
}
Expand Down

0 comments on commit 20a856e

Please sign in to comment.