-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[HttpKernel] Add MapSessionParameter
to map session parameters to controllers
#54458
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
base: 7.4
Are you sure you want to change the base?
Changes from all commits
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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\HttpKernel\Attribute; | ||
|
||
use Symfony\Component\HttpKernel\Controller\ArgumentResolver\SessionParameterValueResolver; | ||
use Symfony\Component\HttpKernel\Controller\ValueResolverInterface; | ||
|
||
/** | ||
* Can be used to pass a session parameter to a controller argument. | ||
*/ | ||
#[\Attribute(\Attribute::TARGET_PARAMETER)] | ||
class MapSessionParameter extends ValueResolver | ||
{ | ||
/** | ||
* @param string|null $name The name of the session parameter; if null, the name of the argument in the controller will be used | ||
* @param class-string<ValueResolverInterface>|string $resolver The name of the resolver to use | ||
*/ | ||
public function __construct( | ||
public ?string $name = null, | ||
string $resolver = SessionParameterValueResolver::class, | ||
) { | ||
parent::__construct($resolver); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\HttpKernel\Controller\ArgumentResolver; | ||
|
||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpKernel\Attribute\MapSessionParameter; | ||
use Symfony\Component\HttpKernel\Controller\ValueResolverInterface; | ||
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata; | ||
|
||
final class SessionParameterValueResolver implements ValueResolverInterface | ||
{ | ||
public function resolve(Request $request, ArgumentMetadata $argument): array | ||
{ | ||
if (!$attribute = $argument->getAttributesOfType(MapSessionParameter::class, ArgumentMetadata::IS_INSTANCEOF)[0] ?? null) { | ||
return []; | ||
} | ||
|
||
if (!$request->hasSession()) { | ||
return []; | ||
} | ||
|
||
if ((!$type = $argument->getType()) || (!class_exists($type) && !interface_exists($type, false))) { | ||
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.
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. Allowing null allow to use an interface or to create the object only if needed. I didn't checked for the union type, i'll update the PR 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. compound types aren't compatible with value resolvers IMHO, this one or any other 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. For the record, I tested, an it fails indeed. It tells the class |
||
if ($type && (str_contains($type, '|') || str_contains($type, '&'))) { | ||
if (!$argument->hasDefaultValue() && !$argument->isNullable()) { | ||
throw new \LogicException(\sprintf('#[MapSessionParameter] cannot be used on controller argument "$%s": "%s" is an union or intersection type, you need to make the parameter nullable or provide a default value..', $argument->getName(), $type)); | ||
} | ||
} else { | ||
throw new \LogicException(\sprintf('#[MapSessionParameter] cannot be used on controller argument "$%s": "%s" is not a class or interface name.', $argument->getName(), $type)); | ||
} | ||
} | ||
|
||
if (interface_exists($type, false) && !$argument->hasDefaultValue() && !$argument->isNullable()) { | ||
throw new \LogicException(\sprintf('#[MapSessionParameter] cannot be used on controller argument "$%s": "%s" is an interface, you need to make the parameter nullable or provide a default value.', $argument->getName(), $type)); | ||
} | ||
|
||
$name = $attribute->name ?? $argument->getName(); | ||
if ($request->getSession()->has($name)) { | ||
$value = $request->getSession()->get($name); | ||
if (!$value instanceof $type && !str_contains($type, '|') && !str_contains($type, '&')) { | ||
throw new \LogicException(\sprintf('#[MapSessionParameter] cannot be used to map controller argument "$%s": the session contains a value of type "%s" which is not an instance of "%s".', $argument->getName(), get_debug_type($value), $type)); | ||
} | ||
|
||
return [$value]; | ||
} | ||
|
||
if ($argument->hasDefaultValue()) { | ||
$value = $argument->getDefaultValue(); | ||
} else { | ||
// handle the type SessionInterface|null $param, which doesn't have a default value and can't be instantiated. | ||
$value = class_exists($type, false) ? new $type() : null; | ||
} | ||
|
||
if (\is_object($value)) { | ||
$request->getSession()->set($name, $value); | ||
} | ||
|
||
return [$value]; | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.