Skip to content

Commit

Permalink
u
Browse files Browse the repository at this point in the history
  • Loading branch information
bohanyang committed Oct 3, 2023
1 parent bbc4975 commit 34575e7
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 145 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Mango\DependencyInjection;

use Mango\HttpKernel\PayloadInitializationListener;
use Mango\HttpKernel\MapRequestPayloadListener;
use ReflectionClass;
use ReflectionException;
use ReflectionNamedType;
Expand Down Expand Up @@ -142,7 +142,7 @@ private function registerDtoInitializers(ContainerBuilder $container): void
}
}

$listener = $container->findDefinition(PayloadInitializationListener::class);
$listener = $container->findDefinition(MapRequestPayloadListener::class);
$listener->setArgument('$initializers', ServiceLocatorTagPass::register($container, $initializers));
}
}
13 changes: 0 additions & 13 deletions packages/mango/Doctrine/Exception/NotFound.php

This file was deleted.

36 changes: 12 additions & 24 deletions packages/mango/Doctrine/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
use Generator;
use InvalidArgumentException;
use LogicException;
use Mango\Doctrine\Exception\NotFound;
use Mango\Doctrine\Exception\UnexpectedRowsAffected;
use RuntimeException;

Expand Down Expand Up @@ -680,28 +679,16 @@ public function fetchColumnGrouped(): array

public function fetchFirstColumn(): array
{
$values = $this->getQueryResult()->fetchFirstColumn();

foreach ($values as $i => $value) {
$values[$i] = $this->convertResultToPHPValue('c0', $value);
}

return $values;
}

public function fetchOne(): mixed
{
if (false === $values = $this->getQueryResult()->fetchNumeric()) {
throw new NotFound();
}

return $this->convertResultToPHPValue('c0', $values[0]);
return array_map(
fn ($value) => $this->convertResultToPHPValue('c0', $value),
$this->getQueryResult()->fetchFirstColumn(),
);
}

public function fetchOneWithDefault(mixed $value): mixed
public function fetchOne(mixed $default = false): mixed
{
if (false === $values = $this->getQueryResult()->fetchNumeric()) {
return $value;
return $default;
}

return $this->convertResultToPHPValue('c0', $values[0]);
Expand Down Expand Up @@ -927,11 +914,7 @@ public function where(mixed ...$expressions): self
}

if (is_string($matchColumn)) {
if (is_array($expression)) {
$expression = $this->in($matchColumn, $expression);
}

$expression = $this->eq($matchColumn, $expression);
$expression = is_array($expression) ? $this->in($matchColumn, $expression) : $this->eq($matchColumn, $expression);
}

$where[] = $expression;
Expand All @@ -944,6 +927,11 @@ public function where(mixed ...$expressions): self
return $this;
}

public function andWhere(mixed ...$expressions): self
{
return $this->where(...$expressions);
}

public function onConflictDoNothing(string|array $conflict, ?int $rowsAffected = null): int
{
$conflict = array_map(fn ($n) => $this->quoteColumn($n), (array) $conflict);
Expand Down
31 changes: 5 additions & 26 deletions packages/mango/Doctrine/SchemaProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@
use InvalidArgumentException;
use Mango\Doctrine\Exception\UnexpectedRowsAffected;
use Mango\Doctrine\Schema\TableBuilder;
use PDOException;
use Symfony\Component\DependencyInjection\Attribute\TaggedIterator;
use Throwable;
use Symfony\Component\Messenger\MessageBusInterface;
use UnexpectedValueException;

use function array_keys;
Expand All @@ -28,7 +27,7 @@

class SchemaProvider implements SchemaProviderInterface
{
private Schema $schema;
private readonly Schema $schema;

private array $tables = [];

Expand All @@ -37,6 +36,7 @@ public function __construct(
private Connection $connection,
#[TaggedIterator('mango.doctrine.table_builder')]
private iterable $tableBuilders,
private MessageBusInterface $messageBus,
) {
$this->createSchema();
}
Expand All @@ -46,30 +46,9 @@ public function getConnection(): Connection
return $this->connection;
}

public function transactional(Closure $func)
public function transactional(Closure $func): mixed
{
$this->connection->beginTransaction();
$commit = true;

try {
$res = $func($this, $commit);
if ($commit) {
$this->connection->commit();
}

return $res;
} catch (Throwable $e) {
$commit = false;

throw $e;
} finally {
if (! $commit) {
try {
$this->connection->rollBack();
} catch (PDOException) {
}
}
}
return $this->connection->transactional($func);
}

public function toSql(): array
Expand Down
17 changes: 0 additions & 17 deletions packages/mango/HttpKernel/AsPayloadInitializer.php

This file was deleted.

24 changes: 0 additions & 24 deletions packages/mango/HttpKernel/MapRequestPayload.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,65 +4,57 @@

namespace Mango\HttpKernel;

use Closure;
use Mango\HttpKernel\MapRequestPayload as MangoMapRequestPayload;
use Psr\Container\ContainerInterface;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
use Symfony\Component\HttpKernel\Attribute\MapRequestPayload;
use Symfony\Component\HttpKernel\Event\ControllerArgumentsEvent;
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;

use function is_string;
use function in_array;

class PayloadInitializationListener
class MapRequestPayloadListener
{
public function __construct(
#[Autowire(service: 'service_container')]
private ContainerInterface $container,
private ContainerInterface $initializers,
private DenormalizerInterface $denormalizer,
) {
}

private function getInitializer(MapRequestPayload $attribute): ?callable
#[AsEventListener(priority: 1)]
public function onKernelControllerArguments(ControllerArgumentsEvent $event): void
{
if ($attribute instanceof MangoMapRequestPayload && $attribute->initializer !== null) {
if (is_string($attribute->initializer)) {
return $this->container->get($attribute->initializer);
}
$request = $event->getRequest();

return Closure::fromCallable([$this->container->get($attribute->initializer[0]), $attribute->initializer[1]]);
if (! $routeParams = $request->attributes->get('_route_params', [])) {
return;
}

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

return null;
}

#[AsEventListener(priority: 1)]
public function onKernelControllerArguments(ControllerArgumentsEvent $event): void
{
$arguments = $event->getArguments();

foreach ($arguments as $i => $argument) {
if (! $argument instanceof MapRequestPayload) {
continue;
}

if (! $initializer = $this->getInitializer($argument)) {
continue;
}
$groups = $argument->serializationContext['groups'] ?? [];

if (! $object = $initializer(...$arguments)) {
if ($groups !== [] && ! in_array('route', $groups, true)) {
continue;
}

$object = $this->denormalizer->denormalize(
$routeParams,
$argument->metadata->getType(),
context: ['groups' => ['route']] + $argument->serializationContext,
);

$attribute = new MapRequestPayload(
$argument->acceptFormat,
$argument->serializationContext + [AbstractObjectNormalizer::OBJECT_TO_POPULATE => $object],
[AbstractObjectNormalizer::OBJECT_TO_POPULATE => $object] + $argument->serializationContext,
$argument->validationGroups,
);

Expand Down
5 changes: 3 additions & 2 deletions packages/mango/Jose/AlgHeaderChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@

final class AlgHeaderChecker implements HeaderChecker
{
public function __construct(private readonly string $algorithm)
{
public function __construct(
private readonly string $algorithm,
) {
}

public function checkHeader(mixed $value): void
Expand Down
19 changes: 8 additions & 11 deletions packages/mango/Query/QueryResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Result;
use LogicException;
use RuntimeException;

use function array_keys;
use function array_map;

class QueryResult
{
Expand Down Expand Up @@ -187,22 +187,19 @@ public function fetchColumnGrouped(): array
public function fetchFirstColumn(): array
{
$this->requireOneColumn();
$values = $this->result->fetchFirstColumn();

foreach ($values as $i => $value) {
$values[$i] = $this->convertResultToPHPValue($this->key0, $value);
}

return $values;
return array_map(
fn ($value) => $this->convertResultToPHPValue($this->key0, $value),
$this->result->fetchFirstColumn(),
);
}

public function fetchOne(): mixed
public function fetchOne(mixed $default = false): mixed
{
$this->requireOneColumn();
$values = $this->result->fetchFirstColumn();

if ($values === []) {
throw new RuntimeException('No result found.');
if (false === $values = $this->result->fetchNumeric()) {
return $default;
}

return $this->convertResultToPHPValue($this->key0, $values[0]);
Expand Down
29 changes: 29 additions & 0 deletions packages/mango/Serializer/HydrateTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace Mango\Serializer;

use Symfony\Component\Serializer\Mapping\ClassDiscriminatorResolverInterface;
use Symfony\Contracts\Service\Attribute\Required;

trait HydrateTrait
{
private readonly ClassDiscriminatorResolverInterface $classDiscriminatorResolver;

#[Required]
public function setClassDiscriminatorResolver(ClassDiscriminatorResolverInterface $classDiscriminatorResolver): void
{
$this->classDiscriminatorResolver = $classDiscriminatorResolver;
}

private function hydrate(string $_class, mixed ...$data): object
{
if ($mapping = $this->classDiscriminatorResolver->getMappingForClass($_class)) {
$class = $mapping->getClassForType($data[$typeProperty = $mapping->getTypeProperty()]);
unset($data[$typeProperty]);
}

return new $class(...$data);
}
}

0 comments on commit 34575e7

Please sign in to comment.