Skip to content

Support returning a function when using the PhpDriver #200

@arjanfrans

Description

@arjanfrans

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions