Skip to content

Commit 11d9666

Browse files
authored
Register missing PSR interfaces to services (#16696)
### What does it do? Registers the Guzzlehttp implementations of PSR interfaces ResponseFactoryInterface, UploadedFileFactoryInterface, and UriFactoryInterface into the services container. ### Why is it needed? While we offer [a bunch of interfaces as part of the core services already](https://docs.modx.com/3.x/en/extending-modx/services/http), I ran into needing the ResponseFactoryInterface to create response classes on a project. I then double checked what other interfaces we can already provide through guzzlehttp/psr7 and made sure we're adding them all in. ### How to test ``` $factory = $this->modx->services->get(ResponseFactoryInterface::class); $response = $factory->createResponse($statusCode) ->withHeader('Content-Type', 'application/json') ->withHeader('Access-Control-Allow-Origin', '*'); $response->getBody()->write(json_encode($data)); ``` ### Related issue(s)/PR(s) N/a
1 parent 0ad09ef commit 11d9666

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

core/src/Revolution/modX.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,11 @@
2828
use PDOStatement;
2929
use Psr\Http\Client\ClientInterface;
3030
use Psr\Http\Message\RequestFactoryInterface;
31+
use Psr\Http\Message\ResponseFactoryInterface;
3132
use Psr\Http\Message\ServerRequestFactoryInterface;
3233
use Psr\Http\Message\StreamFactoryInterface;
34+
use Psr\Http\Message\UploadedFileFactoryInterface;
35+
use Psr\Http\Message\UriFactoryInterface;
3336
use xPDO\Cache\xPDOFileCache;
3437
use xPDO\xPDO;
3538
use xPDO\xPDOException;
@@ -2742,11 +2745,26 @@ protected function _initHttpClient()
27422745
return new HttpFactory();
27432746
});
27442747
}
2748+
if (!$this->services->has(ResponseFactoryInterface::class)) {
2749+
$this->services->add(ResponseFactoryInterface::class, function() {
2750+
return new HttpFactory();
2751+
});
2752+
}
27452753
if (!$this->services->has(StreamFactoryInterface::class)) {
27462754
$this->services->add(StreamFactoryInterface::class, function() {
27472755
return new HttpFactory();
27482756
});
27492757
}
2758+
if (!$this->services->has(UploadedFileFactoryInterface::class)) {
2759+
$this->services->add(UploadedFileFactoryInterface::class, function() {
2760+
return new HttpFactory();
2761+
});
2762+
}
2763+
if (!$this->services->has(UriFactoryInterface::class)) {
2764+
$this->services->add(UriFactoryInterface::class, function() {
2765+
return new HttpFactory();
2766+
});
2767+
}
27502768
}
27512769

27522770
/**

0 commit comments

Comments
 (0)