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

Allow defining PHP mapping with a static function #201

Open
wants to merge 2 commits into
base: 2.2.x
Choose a base branch
from
Open
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
3 changes: 1 addition & 2 deletions lib/Doctrine/Persistence/Mapping/Driver/AnnotationDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use function array_merge;
use function array_unique;
use function assert;
use function get_class;
use function get_declared_classes;
use function in_array;
use function is_dir;
Expand Down Expand Up @@ -179,7 +178,7 @@ public function isTransient($className)
$classAnnotations = $this->reader->getClassAnnotations(new ReflectionClass($className));

foreach ($classAnnotations as $annot) {
if (isset($this->entityAnnotationClasses[get_class($annot)])) {
if (isset($this->entityAnnotationClasses[$annot::class])) {
return false;
}
}
Expand Down
7 changes: 6 additions & 1 deletion lib/Doctrine/Persistence/Mapping/Driver/PHPDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@ public function loadMetadataForClass($className, ClassMetadata $metadata)
protected function loadMappingFile($file)
{
$metadata = $this->metadata;
include $file;

$mapping = include $file;

if (is_callable($mapping)) {
$mapping($metadata);
}

return [$metadata->getName() => $metadata];
}
Expand Down
13 changes: 13 additions & 0 deletions tests/Doctrine/Tests/Persistence/Mapping/PHPDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,21 @@ public function testLoadMetadata(): void
$driver = new PHPDriver([__DIR__ . '/_files']);
$driver->loadMetadataForClass(PHPTestEntity::class, $metadata);
}

public function testLoadMetadataWithFunction(): void
{
$metadata = $this->createMock(ClassMetadata::class);
$metadata->expects($this->once())->method('getFieldNames');

$driver = new PHPDriver([__DIR__ . '/_files']);
$driver->loadMetadataForClass(PHPStaticTestEntity::class, $metadata);
}
}

class PHPTestEntity
{
}

class PHPStaticTestEntity
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

use Doctrine\Persistence\Mapping\ClassMetadata;

return static function (ClassMetadata $metadata): void
{
$metadata->getFieldNames();
};
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,7 @@ public function __setInitialized($initialized)
$this->initialized = (bool) $initialized;
}

/**
* @return mixed
*/
public function __get(string $name)
public function __get(string $name): mixed
{
if ($this->initializer) {
$cb = $this->initializer;
Expand All @@ -180,10 +177,7 @@ public function __get(string $name)
return $this->checkedProperty;
}

/**
* @param mixed $value
*/
public function __set(string $name, $value): void
public function __set(string $name, mixed $value): void
{
if ($this->initializer) {
$cb = $this->initializer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,7 @@ class TypedFoo
{
private int $id;

/**
* @param mixed $id
*/
public function setId($id): void
public function setId(mixed $id): void
{
$this->id = $id;
}
Expand All @@ -77,18 +74,12 @@ class TypedNullableFoo
{
private ?string $value;

/**
* @param mixed $value
*/
public function setValue($value): void
public function setValue(mixed $value): void
{
$this->value = $value;
}

/**
* @return mixed
*/
public function getValue()
public function getValue(): mixed
{
return $this->value;
}
Expand Down