Skip to content
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

Support object reference for mongo db #224

Merged
merged 2 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions packages/core/Reflection/ReflectionExtractor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Draw\Component\Core\Reflection;

class ReflectionExtractor
{
public static function getClasses(null|\ReflectionType $reflectionType): array
{
if (!$reflectionType) {
return [];
}

if ($reflectionType instanceof \ReflectionIntersectionType) {
throw new \InvalidArgumentException('Intersection type is not supported');
}

if ($reflectionType instanceof \ReflectionNamedType) {
return [$reflectionType->getName()];
}

if ($reflectionType instanceof \ReflectionUnionType) {
$classes = [];
foreach ($reflectionType->getTypes() as $type) {
$classes = array_merge($classes, static::getClasses($type));
}

return array_values(array_unique($classes));
}

throw new \InvalidArgumentException('Unknown type '.$reflectionType::class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Draw\Bundle\FrameworkExtraBundle\DependencyInjection\Compiler;

use Draw\Component\Core\Reflection\ReflectionAccessor;
use Draw\Component\Core\Reflection\ReflectionExtractor;
use Draw\Component\Mailer\EmailComposer;
use Draw\Component\Mailer\EmailWriter\EmailWriterInterface;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
Expand Down Expand Up @@ -32,7 +33,11 @@ public function process(ContainerBuilder $container): void
$priority = 0;
}

foreach ($this->getClasses((new \ReflectionMethod($class, $methodName))->getParameters()[0]) as $emailType) {
$emailTypes = ReflectionExtractor::getClasses(
(new \ReflectionMethod($class, $methodName))->getParameters()[0]->getType()
);

foreach ($emailTypes as $emailType) {
$emailWriterListenerDefinition
->addMethodCall('addWriter', [$emailType, $id, $methodName, $priority]);
}
Expand All @@ -45,27 +50,4 @@ public function process(ContainerBuilder $container): void
ServiceLocatorTagPass::register($container, $writers)
);
}

/**
* Extract classes base on union and name type.
*
* @return array<class-string>
*/
private function getClasses(\ReflectionParameter $reflectionParameter): array
{
$type = $reflectionParameter->getType();

if ($type instanceof \ReflectionNamedType) {
return [$type->getName()];
}

if ($type instanceof \ReflectionUnionType) {
return array_map(
fn (\ReflectionNamedType $type) => $type->getName(),
$type->getTypes()
);
}

throw new \InvalidArgumentException('Unable to extract classes from parameter. Only named type and union type are supported.');
}
}
10 changes: 8 additions & 2 deletions packages/mailer/EmailComposer.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Draw\Component\Mailer;

use Draw\Component\Core\Reflection\ReflectionAccessor;
use Draw\Component\Core\Reflection\ReflectionExtractor;
use Draw\Component\Mailer\Email\LocalizeEmailInterface;
use Draw\Component\Mailer\EmailWriter\EmailWriterInterface;
use Psr\Container\ContainerInterface;
Expand Down Expand Up @@ -63,8 +64,13 @@ public function registerEmailWriter(EmailWriterInterface $emailWriter): void
$priority = 0;
}

$emailType = (new \ReflectionMethod($class, $methodName))->getParameters()[0]->getClass()->name;
$this->addWriter($emailType, $emailWriter, $methodName, $priority);
$emailTypes = ReflectionExtractor::getClasses(
(new \ReflectionMethod($class, $methodName))->getParameters()[0]->getType()
);

foreach ($emailTypes as $emailType) {
$this->addWriter($emailType, $emailWriter, $methodName, $priority);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,13 @@ public function extract($source, $target, ExtractionContextInterface $extraction
continue;
}

$controller = explode('::', $route->getDefault('_controller'));
$controller = $route->getDefault('_controller');

if (!\is_string($controller)) {
continue;
}

$controller = explode('::', $controller);

if (2 != \count($controller)) {
continue;
Expand Down
21 changes: 14 additions & 7 deletions packages/open-api/Serializer/Handler/ObjectReferenceHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Draw\Component\OpenApi\Serializer\Handler;

use Doctrine\Persistence\ManagerRegistry;
use Doctrine\Persistence\ObjectManager;
use JMS\Serializer\Context;
use JMS\Serializer\DeserializationContext;
use JMS\Serializer\GraphNavigatorInterface;
Expand Down Expand Up @@ -30,8 +31,10 @@ public static function getSubscribingMethods(): array
];
}

public function __construct(private ManagerRegistry $managerRegistry)
{
public function __construct(
private ?ManagerRegistry $ormManagerRegistry,
private ?ManagerRegistry $odmManagerRegistry
) {
}

public function serializeObjectReference(
Expand All @@ -45,8 +48,7 @@ public function serializeObjectReference(
}

$class = $type['params'][0]['name'];
$identifiers = $this->managerRegistry
->getManagerForClass($class)
$identifiers = $this->getManagerForClass($class)
->getMetadataFactory()
->getMetadataFor($class)
->getIdentifierValues($value);
Expand All @@ -64,9 +66,14 @@ public function deserializeObjectReference(
return null;
}

$repository = $this->managerRegistry
->getRepository($type['params'][0]['name']);
return $this->getManagerForClass($type['params'][0]['name'])
->find($type['params'][0]['name'], $value);
}

return $repository->find($value);
private function getManagerForClass(string $class): ObjectManager
{
return $this->ormManagerRegistry?->getManagerForClass($class)
?? $this->odmManagerRegistry?->getManagerForClass($class)
?? throw new \RuntimeException('No object manager found for class '.$class);
}
}
2 changes: 1 addition & 1 deletion packages/tester/Http/Cookie/Cookie.php
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ public function matchesDomain($domain)
{
// Remove the leading '.' as per spec in RFC 6265.
// http://tools.ietf.org/html/rfc6265#section-5.2.3
$cookieDomain = ltrim($this->getDomain(), '.');
$cookieDomain = ltrim((string) $this->getDomain(), '.');
// Domain not set or exact match.
if (!$cookieDomain || !strcasecmp($domain, $cookieDomain)) {
return true;
Expand Down
1 change: 1 addition & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
colors="true"
bootstrap="tests/bootstrap.php"
cacheDirectory=".phpunit.cache"
displayDetailsOnTestsThatTriggerDeprecations="true"
>
<php>
<ini name="memory_limit" value="4096M"/>
Expand Down
Loading