Skip to content

Commit 6e9253a

Browse files
committed
Add allCandidates() and discoveries() methods
1 parent cc6ff3c commit 6e9253a

File tree

4 files changed

+86
-9
lines changed

4 files changed

+86
-9
lines changed

src/Contracts/Implementations/Psr17/FactoriesContract.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,23 @@ interface FactoriesContract extends ImplementationContract
1717
*/
1818
public static function add(CandidateEntity $candidate): void;
1919

20+
/**
21+
* Return all potential candidates, including those that cannot be instantiated automatically.
22+
*/
23+
public static function allCandidates(): CandidatesCollection;
24+
2025
/**
2126
* Return the candidates collection.
2227
*/
2328
public static function candidates(): CandidatesCollection;
2429

30+
/**
31+
* Returns an array with all discovered implementations.
32+
*
33+
* @return CandidateEntity[]
34+
*/
35+
public static function discoveries(): array;
36+
2537
/**
2638
* Prefer a package over all others.
2739
*

src/Implementations/Psr17/RequestFactories.php

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@
1313

1414
final class RequestFactories extends Implementation implements RequestFactoriesContract
1515
{
16-
private static ?CandidatesCollection $candidates = null;
17-
private static ?RequestFactoryInterface $singleton = null;
18-
private static ?RequestFactoryInterface $using = null;
16+
private static ?CandidatesCollection $candidates = null;
17+
private static ?CandidatesCollection $extendedCandidates = null;
18+
private static ?RequestFactoryInterface $singleton = null;
19+
private static ?RequestFactoryInterface $using = null;
1920

2021
public static function add(CandidateEntity $candidate): void
2122
{
@@ -24,6 +25,21 @@ public static function add(CandidateEntity $candidate): void
2425
self::use(null);
2526
}
2627

28+
/**
29+
* @psalm-suppress MixedInferredReturnType,MixedReturnStatement
30+
*/
31+
public static function allCandidates(): CandidatesCollection
32+
{
33+
if (null !== self::$extendedCandidates) {
34+
return self::$extendedCandidates;
35+
}
36+
37+
self::$extendedCandidates = CandidatesCollection::create();
38+
self::$extendedCandidates->set(self::candidates());
39+
40+
return self::$extendedCandidates;
41+
}
42+
2743
/**
2844
* @psalm-suppress MixedInferredReturnType,MixedReturnStatement
2945
*/
@@ -122,6 +138,11 @@ public static function discover(): ?RequestFactoryInterface
122138
return Discover::httpRequestFactory();
123139
}
124140

141+
public static function discoveries(): array
142+
{
143+
return Discover::httpRequestFactories();
144+
}
145+
125146
public static function prefer(string $package): void
126147
{
127148
self::$candidates ??= CandidatesCollection::create();

src/Implementations/Psr17/ResponseFactories.php

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,33 @@
1313

1414
final class ResponseFactories extends Implementation implements ResponseFactoriesContract
1515
{
16-
private static ?CandidatesCollection $candidates = null;
17-
private static ?ResponseFactoryInterface $singleton = null;
18-
private static ?ResponseFactoryInterface $using = null;
16+
private static ?CandidatesCollection $candidates = null;
17+
private static ?CandidatesCollection $extendedCandidates = null;
18+
private static ?ResponseFactoryInterface $singleton = null;
19+
private static ?ResponseFactoryInterface $using = null;
1920

2021
public static function add(CandidateEntity $candidate): void
2122
{
23+
self::$candidates ??= CandidatesCollection::create();
2224
parent::add($candidate);
2325
self::use(null);
2426
}
2527

28+
/**
29+
* @psalm-suppress MixedInferredReturnType,MixedReturnStatement
30+
*/
31+
public static function allCandidates(): CandidatesCollection
32+
{
33+
if (null !== self::$extendedCandidates) {
34+
return self::$extendedCandidates;
35+
}
36+
37+
self::$extendedCandidates = CandidatesCollection::create();
38+
self::$extendedCandidates->set(self::candidates());
39+
40+
return self::$extendedCandidates;
41+
}
42+
2643
/**
2744
* @psalm-suppress MixedInferredReturnType,MixedReturnStatement
2845
*/
@@ -121,6 +138,11 @@ public static function discover(): ?ResponseFactoryInterface
121138
return Discover::httpResponseFactory();
122139
}
123140

141+
public static function discoveries(): array
142+
{
143+
return Discover::httpResponseFactories();
144+
}
145+
124146
public static function prefer(string $package): void
125147
{
126148
self::$candidates ??= CandidatesCollection::create();

src/Implementations/Psr17/StreamFactories.php

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,33 @@
1313

1414
final class StreamFactories extends Implementation implements StreamFactoriesContract
1515
{
16-
private static ?CandidatesCollection $candidates = null;
17-
private static ?StreamFactoryInterface $singleton = null;
18-
private static ?StreamFactoryInterface $using = null;
16+
private static ?CandidatesCollection $candidates = null;
17+
private static ?CandidatesCollection $extendedCandidates = null;
18+
private static ?StreamFactoryInterface $singleton = null;
19+
private static ?StreamFactoryInterface $using = null;
1920

2021
public static function add(CandidateEntity $candidate): void
2122
{
23+
self::$candidates ??= CandidatesCollection::create();
2224
parent::add($candidate);
2325
self::use(null);
2426
}
2527

28+
/**
29+
* @psalm-suppress MixedInferredReturnType,MixedReturnStatement
30+
*/
31+
public static function allCandidates(): CandidatesCollection
32+
{
33+
if (null !== self::$extendedCandidates) {
34+
return self::$extendedCandidates;
35+
}
36+
37+
self::$extendedCandidates = CandidatesCollection::create();
38+
self::$extendedCandidates->set(self::candidates());
39+
40+
return self::$extendedCandidates;
41+
}
42+
2643
/**
2744
* @psalm-suppress MixedInferredReturnType,MixedReturnStatement
2845
*/
@@ -121,6 +138,11 @@ public static function discover(): ?StreamFactoryInterface
121138
return Discover::httpStreamFactory();
122139
}
123140

141+
public static function discoveries(): array
142+
{
143+
return Discover::httpStreamFactories();
144+
}
145+
124146
public static function prefer(string $package): void
125147
{
126148
self::$candidates ??= CandidatesCollection::create();

0 commit comments

Comments
 (0)