Skip to content

Commit f8a2f89

Browse files
authored
Add support for UploadedFileFactoryInterface and UriFactoryInterface (#4)
This PR adds support for two missing factories defined by PSR-17: * [UploadedFileFactoryInterface](https://www.php-fig.org/psr/psr-17/#25-uploadedfilefactoryinterface) * [UriFactoryInterface](https://www.php-fig.org/psr/psr-17/#26-urifactoryinterface)
1 parent 0731c73 commit f8a2f89

File tree

2 files changed

+106
-0
lines changed

2 files changed

+106
-0
lines changed

src/Contracts/DiscoverContract.php

+36
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,42 @@ public static function httpStreamFactories(): array;
134134
*/
135135
public static function httpStreamFactory(): ?object;
136136

137+
/**
138+
* Returns an array with all PSR-17 HTTP Uploaded File Factory implementations discovered. No implementations are instantiated by the discovery process.
139+
*
140+
* Compatible providers: https://packagist.org/providers/psr/http-factory-implementation
141+
*
142+
* @return CandidateEntity[] An array of CandidateEntity objects representing all implementations discovered.
143+
*/
144+
public static function httpUploadedFileFactories(): array;
145+
146+
/**
147+
* Returns a PSR-17 HTTP Uploaded File factory, or null if one is not found.
148+
*
149+
* Compatible libraries: https://packagist.org/providers/psr/http-factory-implementation
150+
*
151+
* @return null|\Psr\Http\Message\UploadedFileFactoryInterface A PSR-17 HTTP UploadedFile factory, or null if one cannot be found.
152+
*/
153+
public static function httpUploadedFileFactory(): ?object;
154+
155+
/**
156+
* Returns an array with all PSR-17 HTTP Uri Factory implementations discovered. No implementations are instantiated by the discovery process.
157+
*
158+
* Compatible providers: https://packagist.org/providers/psr/http-factory-implementation
159+
*
160+
* @return CandidateEntity[] An array of CandidateEntity objects representing all implementations discovered.
161+
*/
162+
public static function httpUriFactories(): array;
163+
164+
/**
165+
* Returns a PSR-17 HTTP Uri factory, or null if one is not found.
166+
*
167+
* Compatible libraries: https://packagist.org/providers/psr/http-factory-implementation
168+
*
169+
* @return null|\Psr\Http\Message\UriFactoryInterface A PSR-17 HTTP Uri factory, or null if one cannot be found.
170+
*/
171+
public static function httpUriFactory(): ?object;
172+
137173
/**
138174
* Returns a PSR-3 Logger, or null if one is not found.
139175
*

src/Discover.php

+70
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,16 @@ final class Discover implements DiscoverContract
5050
*/
5151
private const PSR_HTTP_STREAM_FACTORY = '\Psr\Http\Message\StreamFactoryInterface';
5252

53+
/**
54+
* @var string
55+
*/
56+
private const PSR_HTTP_UPLOADED_FILE_FACTORY = '\Psr\Http\Message\UploadedFileFactoryInterface';
57+
58+
/**
59+
* @var string
60+
*/
61+
private const PSR_HTTP_URI_FACTORY = '\Psr\Http\Message\UriFactoryInterface';
62+
5363
/**
5464
* @var string
5565
*/
@@ -285,6 +295,66 @@ public static function httpStreamFactory(bool $singleton = false): ?object
285295
return self::discover(self::PSR_HTTP_STREAM_FACTORY);
286296
}
287297

298+
public static function httpUploadedFileFactories(): array
299+
{
300+
$implementationsPackage = '\PsrDiscovery\Implementations\Psr17\UploadedFileFactories';
301+
302+
if (! class_exists($implementationsPackage)) {
303+
throw new SupportPackageNotFoundException('PSR-17 HTTP UploadedFile Factory', 'psr-discovery/http-factory-implementations');
304+
}
305+
306+
self::$extendedCandidates[self::PSR_HTTP_UPLOADED_FILE_FACTORY] ??= $implementationsPackage::allCandidates();
307+
308+
return self::discoveries(self::PSR_HTTP_UPLOADED_FILE_FACTORY);
309+
}
310+
311+
public static function httpUploadedFileFactory(bool $singleton = false): ?object
312+
{
313+
$implementationsPackage = '\PsrDiscovery\Implementations\Psr17\UploadedFileFactories';
314+
315+
if (! class_exists($implementationsPackage)) {
316+
throw new SupportPackageNotFoundException('PSR-17 HTTP UploadedFile Factory', 'psr-discovery/http-factory-implementations');
317+
}
318+
319+
self::$candidates[self::PSR_HTTP_UPLOADED_FILE_FACTORY] ??= $implementationsPackage::candidates();
320+
321+
if ($singleton) {
322+
return self::singleton(self::PSR_HTTP_UPLOADED_FILE_FACTORY);
323+
}
324+
325+
return self::discover(self::PSR_HTTP_UPLOADED_FILE_FACTORY);
326+
}
327+
328+
public static function httpUriFactories(): array
329+
{
330+
$implementationsPackage = '\PsrDiscovery\Implementations\Psr17\UriFactories';
331+
332+
if (! class_exists($implementationsPackage)) {
333+
throw new SupportPackageNotFoundException('PSR-17 HTTP Uri Factory', 'psr-discovery/http-factory-implementations');
334+
}
335+
336+
self::$extendedCandidates[self::PSR_HTTP_URI_FACTORY] ??= $implementationsPackage::allCandidates();
337+
338+
return self::discoveries(self::PSR_HTTP_URI_FACTORY);
339+
}
340+
341+
public static function httpUriFactory(bool $singleton = false): ?object
342+
{
343+
$implementationsPackage = '\PsrDiscovery\Implementations\Psr17\UriFactories';
344+
345+
if (! class_exists($implementationsPackage)) {
346+
throw new SupportPackageNotFoundException('PSR-17 HTTP Uri Factory', 'psr-discovery/http-factory-implementations');
347+
}
348+
349+
self::$candidates[self::PSR_HTTP_URI_FACTORY] ??= $implementationsPackage::candidates();
350+
351+
if ($singleton) {
352+
return self::singleton(self::PSR_HTTP_URI_FACTORY);
353+
}
354+
355+
return self::discover(self::PSR_HTTP_URI_FACTORY);
356+
}
357+
288358
public static function log(bool $singleton = false): ?object
289359
{
290360
$implementationsPackage = '\PsrDiscovery\Implementations\Psr3\Logs';

0 commit comments

Comments
 (0)