Skip to content

Commit

Permalink
Add support for UploadedFileFactoryInterface and UriFactoryInterface (#4
Browse files Browse the repository at this point in the history
)

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)
  • Loading branch information
flavioheleno committed Mar 4, 2024
1 parent 0731c73 commit f8a2f89
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/Contracts/DiscoverContract.php
Expand Up @@ -134,6 +134,42 @@ public static function httpStreamFactories(): array;
*/
public static function httpStreamFactory(): ?object;

/**
* Returns an array with all PSR-17 HTTP Uploaded File Factory implementations discovered. No implementations are instantiated by the discovery process.
*
* Compatible providers: https://packagist.org/providers/psr/http-factory-implementation
*
* @return CandidateEntity[] An array of CandidateEntity objects representing all implementations discovered.
*/
public static function httpUploadedFileFactories(): array;

/**
* Returns a PSR-17 HTTP Uploaded File factory, or null if one is not found.
*
* Compatible libraries: https://packagist.org/providers/psr/http-factory-implementation
*
* @return null|\Psr\Http\Message\UploadedFileFactoryInterface A PSR-17 HTTP UploadedFile factory, or null if one cannot be found.
*/
public static function httpUploadedFileFactory(): ?object;

/**
* Returns an array with all PSR-17 HTTP Uri Factory implementations discovered. No implementations are instantiated by the discovery process.
*
* Compatible providers: https://packagist.org/providers/psr/http-factory-implementation
*
* @return CandidateEntity[] An array of CandidateEntity objects representing all implementations discovered.
*/
public static function httpUriFactories(): array;

/**
* Returns a PSR-17 HTTP Uri factory, or null if one is not found.
*
* Compatible libraries: https://packagist.org/providers/psr/http-factory-implementation
*
* @return null|\Psr\Http\Message\UriFactoryInterface A PSR-17 HTTP Uri factory, or null if one cannot be found.
*/
public static function httpUriFactory(): ?object;

/**
* Returns a PSR-3 Logger, or null if one is not found.
*
Expand Down
70 changes: 70 additions & 0 deletions src/Discover.php
Expand Up @@ -50,6 +50,16 @@ final class Discover implements DiscoverContract
*/
private const PSR_HTTP_STREAM_FACTORY = '\Psr\Http\Message\StreamFactoryInterface';

/**
* @var string
*/
private const PSR_HTTP_UPLOADED_FILE_FACTORY = '\Psr\Http\Message\UploadedFileFactoryInterface';

/**
* @var string
*/
private const PSR_HTTP_URI_FACTORY = '\Psr\Http\Message\UriFactoryInterface';

/**
* @var string
*/
Expand Down Expand Up @@ -285,6 +295,66 @@ public static function httpStreamFactory(bool $singleton = false): ?object
return self::discover(self::PSR_HTTP_STREAM_FACTORY);
}

public static function httpUploadedFileFactories(): array
{
$implementationsPackage = '\PsrDiscovery\Implementations\Psr17\UploadedFileFactories';

if (! class_exists($implementationsPackage)) {
throw new SupportPackageNotFoundException('PSR-17 HTTP UploadedFile Factory', 'psr-discovery/http-factory-implementations');
}

self::$extendedCandidates[self::PSR_HTTP_UPLOADED_FILE_FACTORY] ??= $implementationsPackage::allCandidates();

return self::discoveries(self::PSR_HTTP_UPLOADED_FILE_FACTORY);
}

public static function httpUploadedFileFactory(bool $singleton = false): ?object
{
$implementationsPackage = '\PsrDiscovery\Implementations\Psr17\UploadedFileFactories';

if (! class_exists($implementationsPackage)) {
throw new SupportPackageNotFoundException('PSR-17 HTTP UploadedFile Factory', 'psr-discovery/http-factory-implementations');
}

self::$candidates[self::PSR_HTTP_UPLOADED_FILE_FACTORY] ??= $implementationsPackage::candidates();

if ($singleton) {
return self::singleton(self::PSR_HTTP_UPLOADED_FILE_FACTORY);
}

return self::discover(self::PSR_HTTP_UPLOADED_FILE_FACTORY);
}

public static function httpUriFactories(): array
{
$implementationsPackage = '\PsrDiscovery\Implementations\Psr17\UriFactories';

if (! class_exists($implementationsPackage)) {
throw new SupportPackageNotFoundException('PSR-17 HTTP Uri Factory', 'psr-discovery/http-factory-implementations');
}

self::$extendedCandidates[self::PSR_HTTP_URI_FACTORY] ??= $implementationsPackage::allCandidates();

return self::discoveries(self::PSR_HTTP_URI_FACTORY);
}

public static function httpUriFactory(bool $singleton = false): ?object
{
$implementationsPackage = '\PsrDiscovery\Implementations\Psr17\UriFactories';

if (! class_exists($implementationsPackage)) {
throw new SupportPackageNotFoundException('PSR-17 HTTP Uri Factory', 'psr-discovery/http-factory-implementations');
}

self::$candidates[self::PSR_HTTP_URI_FACTORY] ??= $implementationsPackage::candidates();

if ($singleton) {
return self::singleton(self::PSR_HTTP_URI_FACTORY);
}

return self::discover(self::PSR_HTTP_URI_FACTORY);
}

public static function log(bool $singleton = false): ?object
{
$implementationsPackage = '\PsrDiscovery\Implementations\Psr3\Logs';
Expand Down

0 comments on commit f8a2f89

Please sign in to comment.