Skip to content

Commit

Permalink
u
Browse files Browse the repository at this point in the history
  • Loading branch information
bohanyang committed Sep 25, 2023
1 parent 95e4ff4 commit ef8f6e0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 23 deletions.
1 change: 0 additions & 1 deletion packages/mango/HttpKernel/MapRequestPayload.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ public function __construct(
string|GroupSequence|array|null $validationGroups = null,
string $resolver = RequestPayloadValueResolver::class,
public readonly array|string|null $initializer = null,
public readonly bool $initializerResolve = true,
) {
parent::__construct($acceptFormat, $serializationContext, $validationGroups, $resolver);
}
Expand Down
34 changes: 12 additions & 22 deletions packages/mango/HttpKernel/PayloadInitializationListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@
use Closure;
use Mango\HttpKernel\MapRequestPayload as MangoMapRequestPayload;
use Psr\Container\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
use Symfony\Component\HttpKernel\Attribute\MapRequestPayload;
use Symfony\Component\HttpKernel\Controller\ArgumentResolverInterface;
use Symfony\Component\HttpKernel\Event\ControllerArgumentsEvent;
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;

Expand All @@ -23,28 +21,25 @@ public function __construct(
#[Autowire(service: 'service_container')]
private ContainerInterface $container,
private ContainerInterface $initializers,
#[Autowire(service: 'argument_resolver')]
private ArgumentResolverInterface $argumentResolver,
) {
}

private function getInitializer(MapRequestPayload $attribute): ?callable
{
try {
if ($attribute instanceof MangoMapRequestPayload && $attribute->initializer !== null) {
if (is_string($attribute->initializer)) {
return $this->container->get($attribute->initializer);
}

return Closure::fromCallable([$this->container->get($attribute->initializer[0]), $attribute->initializer[1]]);
if ($attribute instanceof MangoMapRequestPayload && $attribute->initializer !== null) {
if (is_string($attribute->initializer)) {
return $this->container->get($attribute->initializer);
}

if ($type = $attribute->metadata->getType()) {
return $this->initializers->get($type);
}
} catch (NotFoundExceptionInterface) {
return null;
return Closure::fromCallable([$this->container->get($attribute->initializer[0]), $attribute->initializer[1]]);
}

$type = $attribute->metadata->getType();
if ($type && $this->initializers->has($type)) {
return $this->initializers->get($type);
}

return null;
}

#[AsEventListener(priority: 1)]
Expand All @@ -61,12 +56,7 @@ public function onKernelControllerArguments(ControllerArgumentsEvent $event): vo
continue;
}

$initializerArgs = $arguments;
if ($argument instanceof MangoMapRequestPayload && $argument->initializerResolve) {
$initializerArgs = $argument->$this->argumentResolver->getArguments($event->getRequest(), $initializer);
}

if (! $object = $initializer(...$initializerArgs)) {
if (! $object = $initializer(...$arguments)) {
continue;
}

Expand Down

0 comments on commit ef8f6e0

Please sign in to comment.