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

Add doctrine/orm v3 compatibility #329

Merged
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
},
"require-dev": {
"doctrine/annotations": "^2.0",
"doctrine/orm": "^2.9",
"doctrine/orm": "^2.9|^3.0",
"matthiasnoback/symfony-dependency-injection-test": "^4.3 || ^5.0",
"nyholm/psr7": "^1.5.1",
"php-cs-fixer/shim": "^3.14",
Expand Down
2 changes: 2 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ parameters:
paths:
- src
- tests
ignoreErrors:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add this to baseline instead :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant separate baseline file.. but nevermind

- '#Call to static method getClass\(\) on an unknown class Doctrine\\Common\\Util\\ClassUtils#'
22 changes: 20 additions & 2 deletions src/Services/MeilisearchService.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Meilisearch\Bundle\Services;

use Doctrine\Common\Util\ClassUtils;
use Doctrine\ORM\Proxy\DefaultProxyClassNameResolver;
use Doctrine\Persistence\ObjectManager;
use Meilisearch\Bundle\Collection;
use Meilisearch\Bundle\Engine;
Expand Down Expand Up @@ -236,7 +237,7 @@ private function getBaseClassName($objectOrClass): string
}

if (is_object($objectOrClass)) {
return ClassUtils::getClass($objectOrClass);
return self::resolveClass($objectOrClass);
}

return $objectOrClass;
Expand Down Expand Up @@ -306,7 +307,7 @@ private function getAggregatorsFromEntities(ObjectManager $objectManager, array
$aggregators = [];

foreach ($entities as $entity) {
$entityClassName = ClassUtils::getClass($entity);
$entityClassName = self::resolveClass($entity);
if (array_key_exists($entityClassName, $this->entitiesAggregators)) {
foreach ($this->entitiesAggregators[$entityClassName] as $aggregator) {
$aggregators[] = new $aggregator(
Expand Down Expand Up @@ -367,4 +368,21 @@ private function assertIsSearchable(string $className): void
throw new Exception('Class '.$className.' is not searchable.');
}
}

private static function resolveClass(object $object): string
{
static $resolver;

$resolver ??= (function () {
// Doctrine ORM v3+ compatibility
if (\class_exists(DefaultProxyClassNameResolver::class)) {
return fn (object $object) => DefaultProxyClassNameResolver::getClass($object);
}

// Legacy Doctrine ORM compatibility
return fn (object $object) => ClassUtils::getClass($object); // @codeCoverageIgnore
})();

return $resolver($object);
}
}
8 changes: 6 additions & 2 deletions tests/Dbal/Type/DummyObjectIdType.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ public function convertToPHPValue($value, AbstractPlatform $platform): ?DummyObj
}

if (!\is_string($value) && !is_int($value)) {
throw ConversionException::conversionFailedInvalidType($value, $this->getName(), ['null', 'string', 'int', DummyObjectId::class]);
$actualType = \get_debug_type($value);
$possibleTypes = ['null', 'string', 'int', self::class];
throw new ConversionException(\sprintf("Could not convert PHP value '%s' of type '%s' to type '%s'. Expected one of the following types: %s", $value, $actualType, $this->getName(), \implode(', ', $possibleTypes)));
}

return new DummyObjectId((int) $value);
Expand All @@ -48,7 +50,9 @@ public function convertToDatabaseValue($value, AbstractPlatform $platform): ?int
}

if (!\is_string($value) && !is_int($value)) {
throw ConversionException::conversionFailedInvalidType($value, $this->getName(), ['null', 'string', 'int', DummyObjectId::class]);
$actualType = \get_debug_type($value);
$possibleTypes = ['null', 'string', 'int', self::class];
throw new ConversionException(\sprintf("Could not convert PHP value '%s' of type '%s' to type '%s'. Expected one of the following types: %s", $value, $actualType, $this->getName(), \implode(', ', $possibleTypes)));
}

return (int) $value;
Expand Down
Loading