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 returning a function when using the PhpDriver #200

Open
arjanfrans opened this issue Jul 20, 2021 · 0 comments
Open

Support returning a function when using the PhpDriver #200

arjanfrans opened this issue Jul 20, 2021 · 0 comments

Comments

@arjanfrans
Copy link

When defining ORM mapping using the php driver there is $metadata variable floating around in the mapping file. IDE's complain about type hinting.

It would be nice to be able to return a static function that gets the $metadata variable as an argument, so then you can write the mapping in php like this:

// config/orm/mapping/App.Entity.User.php

<?php

use Doctrine\ORM\Mapping\Builder\ClassMetadataBuilder;
use Doctrine\ORM\Mapping\ClassMetadata;

return static function (ClassMetadata $metadata): void
{
    $builder = new ClassMetadataBuilder($metadata);
    
    // ...
};

I modified the PHP driver to be able to do this (lib/Doctrine/Persistence/Mapping/Driver/PHPDriver.php):

// ...
    protected function loadMappingFile($file)
    {
        $metadata = $this->metadata;
        $mapping = include $file;
    
        if (is_callable($mapping)) {
            $mapping($metadata);
        }

        return [$metadata->getName() => $metadata];
    }
// ...

Is there any argument against doing this? The big advantage here is that you at least have a type hint.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant