From 64b7bd46123991cef72ecaa439bd4765c99dcad3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Tamarelle?= Date: Tue, 28 Oct 2025 12:53:04 +0100 Subject: [PATCH] Static PHP config --- .../DoctrineMongoDBExtension.php | 6 ++++- src/Mapping/StaticClassMetadata.php | 12 +++++++++ .../PhpBundle/Document/TestDocument.php | 9 +++++++ .../Fixtures/Bundles/PhpBundle/PhpBundle.php | 11 ++++++++ ...ures.Bundles.AttributesBundle.Document.php | 20 ++++++++++++++ .../StaticPhpBundle/Document/TestDocument.php | 27 +++++++++++++++++++ .../StaticPhpBundle/StaticPhpBundle.php | 11 ++++++++ 7 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 src/Mapping/StaticClassMetadata.php create mode 100644 tests/DependencyInjection/Fixtures/Bundles/PhpBundle/Document/TestDocument.php create mode 100644 tests/DependencyInjection/Fixtures/Bundles/PhpBundle/PhpBundle.php create mode 100644 tests/DependencyInjection/Fixtures/Bundles/PhpBundle/config/Doctrine.Bundle.MongoDBBundle.Tests.DependencyInjection.Fixtures.Bundles.AttributesBundle.Document.php create mode 100644 tests/DependencyInjection/Fixtures/Bundles/StaticPhpBundle/Document/TestDocument.php create mode 100644 tests/DependencyInjection/Fixtures/Bundles/StaticPhpBundle/StaticPhpBundle.php diff --git a/src/DependencyInjection/DoctrineMongoDBExtension.php b/src/DependencyInjection/DoctrineMongoDBExtension.php index b4b23847..44c5c4a9 100644 --- a/src/DependencyInjection/DoctrineMongoDBExtension.php +++ b/src/DependencyInjection/DoctrineMongoDBExtension.php @@ -25,6 +25,8 @@ use Doctrine\ODM\MongoDB\Mapping\Annotations\View; use Doctrine\ODM\MongoDB\Mapping\Driver\AttributeDriver; use Doctrine\Persistence\Mapping\Driver\MappingDriverChain; +use Doctrine\Persistence\Mapping\Driver\PHPDriver; +use Doctrine\Persistence\Mapping\Driver\StaticPHPDriver; use Doctrine\Persistence\Proxy; use InvalidArgumentException; use LogicException; @@ -326,7 +328,7 @@ protected function assertValidMappingConfiguration( } if (! in_array($mappingConfig['type'], ['xml', 'php', 'staticphp', 'attribute'])) { - throw new InvalidArgumentException(sprintf('Can only configure "xml", "yml", "php", "staticphp" or "attribute" through the DoctrineBundle. Use your own bundle to configure other metadata drivers. You can register them by adding a new driver to the "%s" service definition.', $this->getObjectManagerElementName($objectManagerName . '_metadata_driver'))); + throw new InvalidArgumentException(sprintf('Can only configure "xml", "php", "staticphp" or "attribute" through the DoctrineMongoDBBundle. Use your own bundle to configure other metadata drivers. You can register them by adding a new driver to the "%s" service definition.', $this->getObjectManagerElementName($objectManagerName . '_metadata_driver'))); } } @@ -1024,6 +1026,8 @@ protected function getMetadataDriverClass(string $driverType): string return match ($driverType) { 'driver_chain' => MappingDriverChain::class, 'attribute' => AttributeDriver::class, + 'staticphp' => StaticPHPDriver::class, + 'php' => PHPDriver::class, 'xml' => XmlDriver::class, default => throw new InvalidArgumentException(sprintf('Metadata driver not supported: "%s"', $driverType)) }; diff --git a/src/Mapping/StaticClassMetadata.php b/src/Mapping/StaticClassMetadata.php new file mode 100644 index 00000000..f5429ae2 --- /dev/null +++ b/src/Mapping/StaticClassMetadata.php @@ -0,0 +1,12 @@ +mapField([ + 'name' => '_id', + 'fieldName' => 'id', + 'type' => Type::ID, + 'id' => true, + ]); + $metadata->mapField([ + 'name' => 'name', + 'fieldName' => 'name', + 'type' => Type::STRING, + ]); +}; diff --git a/tests/DependencyInjection/Fixtures/Bundles/StaticPhpBundle/Document/TestDocument.php b/tests/DependencyInjection/Fixtures/Bundles/StaticPhpBundle/Document/TestDocument.php new file mode 100644 index 00000000..f214cf83 --- /dev/null +++ b/tests/DependencyInjection/Fixtures/Bundles/StaticPhpBundle/Document/TestDocument.php @@ -0,0 +1,27 @@ +mapField([ + 'name' => '_id', + 'fieldName' => 'id', + 'type' => Type::ID, + 'id' => true, + ]); + $metadata->mapField([ + 'name' => 'name', + 'fieldName' => 'name', + 'type' => Type::STRING, + ]); + } +} diff --git a/tests/DependencyInjection/Fixtures/Bundles/StaticPhpBundle/StaticPhpBundle.php b/tests/DependencyInjection/Fixtures/Bundles/StaticPhpBundle/StaticPhpBundle.php new file mode 100644 index 00000000..81e3de21 --- /dev/null +++ b/tests/DependencyInjection/Fixtures/Bundles/StaticPhpBundle/StaticPhpBundle.php @@ -0,0 +1,11 @@ +