-
Notifications
You must be signed in to change notification settings - Fork 19
Feature: Doctrine Provider #253
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,4 +8,5 @@ tools/phpstan/cache/ | |
cache/ | ||
site/ | ||
.build/ | ||
.castor* | ||
.castor* | ||
tests/Doctrine/db.sqlite |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,13 +12,15 @@ | |
use AutoMapper\Loader\FileLoader; | ||
use AutoMapper\Metadata\MetadataFactory; | ||
use AutoMapper\Metadata\MetadataRegistry; | ||
use AutoMapper\Provider\Doctrine\DoctrineProvider; | ||
use AutoMapper\Provider\ProviderInterface; | ||
use AutoMapper\Provider\ProviderRegistry; | ||
use AutoMapper\Symfony\ExpressionLanguageProvider; | ||
use AutoMapper\Transformer\PropertyTransformer\PropertyTransformerInterface; | ||
use AutoMapper\Transformer\PropertyTransformer\PropertyTransformerRegistry; | ||
use AutoMapper\Transformer\TransformerFactoryInterface; | ||
use Doctrine\Common\Annotations\AnnotationReader; | ||
use Doctrine\ORM\EntityManagerInterface; | ||
use Symfony\Component\EventDispatcher\EventDispatcher; | ||
use Symfony\Component\EventDispatcher\EventDispatcherInterface; | ||
use Symfony\Component\ExpressionLanguage\ExpressionLanguage; | ||
|
@@ -148,6 +150,7 @@ public static function create( | |
EventDispatcherInterface $eventDispatcher = new EventDispatcher(), | ||
iterable $providers = [], | ||
bool $removeDefaultProperties = false, | ||
?EntityManagerInterface $entityManager = null, | ||
): AutoMapperInterface { | ||
if (\count($transformerFactories) > 0) { | ||
trigger_deprecation('jolicode/automapper', '9.0', 'The "$transformerFactories" property will be removed in version 10.0, AST transformer factories must be included within AutoMapper.', __METHOD__); | ||
|
@@ -176,6 +179,12 @@ public static function create( | |
$classDiscriminatorFromClassMetadata = new ClassDiscriminatorFromClassMetadata($classMetadataFactory); | ||
} | ||
|
||
$providers = iterator_to_array($providers); | ||
nikophil marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if (null !== $entityManager) { | ||
$providers[] = new DoctrineProvider($entityManager); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. so we can't surcharge this provider and its static ? is there any way we could provide it with configuration so we can surcharge it ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the DX method to create the automapper, you will be able to surcharge it in symfony like always, or just don't pass the entity manager and pass your own provider |
||
} | ||
|
||
$customTransformerRegistry = new PropertyTransformerRegistry($propertyTransformers); | ||
$metadataRegistry = new MetadataRegistry($configuration); | ||
$providerRegistry = new ProviderRegistry($providers); | ||
|
@@ -192,6 +201,7 @@ public static function create( | |
$expressionLanguage, | ||
$eventDispatcher, | ||
$removeDefaultProperties, | ||
$entityManager, | ||
); | ||
|
||
$mapperGenerator = new MapperGenerator( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace AutoMapper\EventListener\Doctrine; | ||
|
||
use AutoMapper\Event\PropertyMetadataEvent; | ||
use Doctrine\ORM\EntityManagerInterface; | ||
|
||
final readonly class DoctrineIdentifierListener | ||
{ | ||
public function __construct( | ||
private EntityManagerInterface $entityManager | ||
) { | ||
} | ||
|
||
public function __invoke(PropertyMetadataEvent $event): void | ||
{ | ||
if ($event->mapperMetadata->target === 'array' || !$this->entityManager->getMetadataFactory()->hasMetadataFor($event->mapperMetadata->target)) { | ||
return; | ||
} | ||
|
||
$metadata = $this->entityManager->getClassMetadata($event->mapperMetadata->target); | ||
|
||
if ($metadata->isIdentifier($event->target->property)) { | ||
$event->identifier = true; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace AutoMapper\EventListener\Doctrine; | ||
|
||
use AutoMapper\Event\GenerateMapperEvent; | ||
use AutoMapper\Provider\Doctrine\DoctrineProvider; | ||
use Doctrine\ORM\EntityManagerInterface; | ||
|
||
final readonly class DoctrineProviderListener | ||
{ | ||
public function __construct( | ||
private EntityManagerInterface $entityManager | ||
) { | ||
} | ||
|
||
public function __invoke(GenerateMapperEvent $event): void | ||
{ | ||
if ($event->mapperMetadata->target === 'array' || !$this->entityManager->getMetadataFactory()->hasMetadataFor($event->mapperMetadata->target)) { | ||
return; | ||
} | ||
|
||
$event->provider = DoctrineProvider::class; | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so this feature won't be tested with orm 2 (there are still a lot of them in the wild) ?