-
-
Notifications
You must be signed in to change notification settings - Fork 315
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[LiveComponent] use ValueResolver instead of onRequest to parse LiveArgs
- Loading branch information
Showing
6 changed files
with
204 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,9 @@ | |
|
||
namespace Symfony\UX\LiveComponent\Attribute; | ||
|
||
use Symfony\Component\HttpKernel\Attribute\ValueResolver; | ||
use Symfony\UX\LiveComponent\ValueResolver\LiveArgValueResolver; | ||
|
||
/** | ||
* An attribute to configure a LiveArg (custom argument passed to a LiveAction). | ||
* | ||
|
@@ -19,63 +22,16 @@ | |
* @author Tomas Norkūnas <[email protected]> | ||
*/ | ||
#[\Attribute(\Attribute::TARGET_PARAMETER)] | ||
final class LiveArg | ||
final class LiveArg extends ValueResolver | ||
{ | ||
public function __construct( | ||
/** | ||
* @param string|null $name The name of the argument received by the LiveAction | ||
*/ | ||
public ?string $name = null, | ||
bool $disabled = false, | ||
string $resolver = LiveArgValueResolver::class, | ||
) { | ||
} | ||
|
||
/** | ||
* @internal | ||
* | ||
* @return array<string, mixed> | ||
*/ | ||
public static function liveArgs(object $component, string $action, array $arguments = []): array | ||
{ | ||
$method = new \ReflectionMethod($component, $action); | ||
$liveArgs = []; | ||
|
||
foreach ($method->getParameters() as $parameter) { | ||
foreach ($parameter->getAttributes(self::class) as $liveArg) { | ||
/** @var LiveArg $attr */ | ||
$attr = $liveArg->newInstance(); | ||
$parameterName = $attr->name ?? $parameter->getName(); | ||
$type = $parameter->getType(); | ||
|
||
if (!isset($arguments[$parameterName])) { | ||
continue; | ||
} | ||
|
||
$value = $arguments[$parameterName]; | ||
if (null !== $type && !self::allowsString($type) && '' === $value) { | ||
$value = null; | ||
} | ||
|
||
$liveArgs[$parameter->getName()] = $value; | ||
} | ||
} | ||
|
||
return $liveArgs; | ||
} | ||
|
||
private static function allowsString(\ReflectionType $type): bool | ||
{ | ||
if ($type instanceof \ReflectionNamedType) { | ||
return 'string' === $type->getName(); | ||
} | ||
|
||
if ($type instanceof \ReflectionUnionType) { | ||
foreach ($type->getTypes() as $subType) { | ||
if ('string' === $subType->getName()) { | ||
return true; | ||
} | ||
} | ||
} | ||
|
||
return false; | ||
parent::__construct($resolver, $disabled); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?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\UX\LiveComponent\Util; | ||
|
||
use Symfony\Component\HttpFoundation\Exception\JsonException; | ||
use Symfony\Component\HttpFoundation\Request; | ||
|
||
/** | ||
* @author Kevin Bond <[email protected]> | ||
* @author Ryan Weaver <[email protected]> | ||
* | ||
* @internal | ||
*/ | ||
final class LiveRequestDataParser | ||
{ | ||
/** | ||
* @return array{ | ||
* data: array, | ||
* args: array, | ||
* actions: array | ||
* // has "fingerprint" and "tag" string key, keyed by component id | ||
* children: array | ||
* propsFromParent: array | ||
* } | ||
*/ | ||
public static function parseDataFor(Request $request): array | ||
{ | ||
if (!$request->attributes->has('_live_request_data')) { | ||
if ($request->query->has('props')) { | ||
$liveRequestData = [ | ||
'props' => self::parseJsonFromQuery($request, 'props'), | ||
'updated' => self::parseJsonFromQuery($request, 'updated'), | ||
'args' => [], | ||
'actions' => [], | ||
'children' => self::parseJsonFromQuery($request, 'children'), | ||
'propsFromParent' => self::parseJsonFromQuery($request, 'propsFromParent'), | ||
]; | ||
} else { | ||
try { | ||
$requestData = json_decode($request->request->get('data'), true, 512, \JSON_BIGINT_AS_STRING | \JSON_THROW_ON_ERROR); | ||
} catch (\JsonException $e) { | ||
throw new JsonException('Could not decode request body.', $e->getCode(), $e); | ||
} | ||
|
||
$liveRequestData = [ | ||
'props' => $requestData['props'] ?? [], | ||
'updated' => $requestData['updated'] ?? [], | ||
'args' => $requestData['args'] ?? [], | ||
'actions' => $requestData['actions'] ?? [], | ||
'children' => $requestData['children'] ?? [], | ||
'propsFromParent' => $requestData['propsFromParent'] ?? [], | ||
]; | ||
} | ||
|
||
$request->attributes->set('_live_request_data', $liveRequestData); | ||
} | ||
|
||
return $request->attributes->get('_live_request_data'); | ||
} | ||
|
||
private static function parseJsonFromQuery(Request $request, string $key): array | ||
{ | ||
if (!$request->query->has($key)) { | ||
return []; | ||
} | ||
|
||
try { | ||
return json_decode($request->query->get($key), true, 512, \JSON_THROW_ON_ERROR); | ||
} catch (\JsonException $exception) { | ||
throw new JsonException(sprintf('Invalid JSON on query string %s.', $key), 0, $exception); | ||
} | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
src/LiveComponent/src/ValueResolver/LiveArgValueResolver.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?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\UX\LiveComponent\ValueResolver; | ||
|
||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpKernel\Controller\ArgumentValueResolverInterface; | ||
use Symfony\Component\HttpKernel\Controller\ValueResolverInterface; | ||
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata; | ||
|
||
/** | ||
* @author Jannes Drijkoningen <[email protected]> | ||
* | ||
* @internal | ||
*/ | ||
if (\interface_exists(ValueResolverInterface::class)) { | ||
class LiveArgValueResolver implements ValueResolverInterface | ||
{ | ||
use LiveArgValueResolverTrait { | ||
resolve as resolveArgument; | ||
} | ||
|
||
public function resolve(Request $request, ArgumentMetadata $argument): iterable | ||
{ | ||
if (!$this->supports($argument)) { | ||
return []; | ||
} | ||
|
||
return $this->resolveArgument($request, $argument); | ||
} | ||
} | ||
} else { | ||
class LiveArgValueResolver implements ArgumentValueResolverInterface | ||
{ | ||
use LiveArgValueResolverTrait { | ||
supports as supportsArgument; | ||
} | ||
|
||
public function supports(Request $request, ArgumentMetadata $argument): bool | ||
{ | ||
return $this->supportsArgument($argument); | ||
} | ||
} | ||
} |
Oops, something went wrong.