-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[HttpKernel] Allow to inject PSR-7 ServerRequest in controllers and to return a PSR-7 response. #14751
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[HttpKernel] Allow to inject PSR-7 ServerRequest in controllers and to return a PSR-7 response. #14751
Changes from all commits
d950b53
680ec31
24f04ed
8e6c13f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,8 @@ | |
namespace Symfony\Component\HttpKernel\Controller; | ||
|
||
use Psr\Log\LoggerInterface; | ||
use Symfony\Bridge\PsrHttpMessage\Factory\DiactorosFactory; | ||
use Symfony\Bridge\PsrHttpMessage\HttpMessageFactoryInterface; | ||
use Symfony\Component\HttpFoundation\Request; | ||
|
||
/** | ||
|
@@ -22,21 +24,27 @@ | |
* the controller method arguments. | ||
* | ||
* @author Fabien Potencier <fabien@symfony.com> | ||
* @author Kévin Dunglas <dunglas@gmail.com> | ||
* | ||
* @api | ||
*/ | ||
class ControllerResolver implements ControllerResolverInterface | ||
{ | ||
private $logger; | ||
private $httpMessageFactory; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param LoggerInterface $logger A LoggerInterface instance | ||
*/ | ||
public function __construct(LoggerInterface $logger = null) | ||
public function __construct(LoggerInterface $logger = null, HttpMessageFactoryInterface $httpMessageFactory = null) | ||
{ | ||
$this->logger = $logger; | ||
|
||
if (null === $httpMessageFactory && class_exists('Symfony\Bridge\PsrHttpMessage\Factory\DiactorosFactory')) { | ||
$this->httpMessageFactory = new DiactorosFactory(); | ||
} | ||
} | ||
|
||
/** | ||
|
@@ -112,21 +120,43 @@ protected function doGetArguments(Request $request, $controller, array $paramete | |
foreach ($parameters as $param) { | ||
if (array_key_exists($param->name, $attributes)) { | ||
$arguments[] = $attributes[$param->name]; | ||
} elseif ($param->getClass() && $param->getClass()->isInstance($request)) { | ||
$arguments[] = $request; | ||
} elseif ($param->isDefaultValueAvailable()) { | ||
$arguments[] = $param->getDefaultValue(); | ||
} else { | ||
if (is_array($controller)) { | ||
$repr = sprintf('%s::%s()', get_class($controller[0]), $controller[1]); | ||
} elseif (is_object($controller)) { | ||
$repr = get_class($controller); | ||
} else { | ||
$repr = $controller; | ||
|
||
continue; | ||
} | ||
|
||
if ($class = $param->getClass()) { | ||
if ($class->isInstance($request)) { | ||
$arguments[] = $request; | ||
|
||
continue; | ||
} | ||
|
||
throw new \RuntimeException(sprintf('Controller "%s" requires that you provide a value for the "$%s" argument (because there is no default value or because there is a non optional argument after this one).', $repr, $param->name)); | ||
if ($class->implementsInterface('Psr\Http\Message\ServerRequestInterface')) { | ||
if (null === $this->httpMessageFactory) { | ||
throw new \RuntimeException('The PSR-7 Bridge must be installed to inject instances of Psr\Http\Message\ServerRequestInterface in controllers.'); | ||
} | ||
|
||
$arguments[] = $this->httpMessageFactory->createRequest($request); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't understand how this works. The Symfony Request won't be in the RequestStack. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure I understand your concern well. The Symfony request is added to the request stack here: https://github.com/symfony/symfony/blob/2.7/src/Symfony/Component/HttpKernel/HttpKernel.php#L124 So it is already in the stack when it is converted to a PSR request here. |
||
|
||
continue; | ||
} | ||
} | ||
|
||
if ($param->isDefaultValueAvailable()) { | ||
$arguments[] = $param->getDefaultValue(); | ||
|
||
continue; | ||
} | ||
|
||
if (is_array($controller)) { | ||
$repr = sprintf('%s::%s()', get_class($controller[0]), $controller[1]); | ||
} elseif (is_object($controller)) { | ||
$repr = get_class($controller); | ||
} else { | ||
$repr = $controller; | ||
} | ||
|
||
throw new \RuntimeException(sprintf('Controller "%s" requires that you provide a value for the "$%s" argument (because there is no default value or because there is a non optional argument after this one).', $repr, $param->name)); | ||
} | ||
|
||
return $arguments; | ||
|
@@ -157,7 +187,7 @@ protected function createController($controller) | |
} | ||
|
||
/** | ||
* Returns an instantiated controller | ||
* Returns an instantiated controller. | ||
* | ||
* @param string $class A class name | ||
* | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,12 @@ | |
"homepage": "https://symfony.com/contributors" | ||
} | ||
], | ||
"repositories": [ | ||
{ | ||
"type": "vcs", | ||
"url": "https://github.com/dunglas/psr-http-message-bridge" | ||
} | ||
], | ||
"require": { | ||
"php": ">=5.3.9", | ||
"symfony/event-dispatcher": "~2.5.9|~2.6,>=2.6.2|~3.0.0", | ||
|
@@ -38,7 +44,9 @@ | |
"symfony/stopwatch": "~2.3|~3.0.0", | ||
"symfony/templating": "~2.2|~3.0.0", | ||
"symfony/translation": "~2.0,>=2.0.5|~3.0.0", | ||
"symfony/var-dumper": "~2.6|~3.0.0" | ||
"symfony/var-dumper": "~2.6|~3.0.0", | ||
"symfony/psr-http-message-bridge": "dev-wip", | ||
"zendframework/zend-diactoros": "~1.0" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again, those are soft deps (dev ones possibily). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here (in |
||
}, | ||
"conflict": { | ||
"symfony/config": "<2.7" | ||
|
@@ -50,7 +58,9 @@ | |
"symfony/console": "", | ||
"symfony/dependency-injection": "", | ||
"symfony/finder": "", | ||
"symfony/var-dumper": "" | ||
"symfony/var-dumper": "", | ||
"symfony/psr-http-message-bridge": "To enable PSR-7 support.", | ||
"zendframework/zend-diactoros": "To enable PSR-7 support." | ||
}, | ||
"autoload": { | ||
"psr-4": { "Symfony\\Component\\HttpKernel\\": "" } | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Those 2 deps are actually soft ones, not hard ones.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's in the
require-dev
section (just for tests).