-
-
Notifications
You must be signed in to change notification settings - Fork 73
Support closure returned by .php files in the PHPDriver
#450
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
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 |
|---|---|---|
|
|
@@ -4,7 +4,11 @@ | |
|
|
||
| namespace Doctrine\Persistence\Mapping\Driver; | ||
|
|
||
| use Closure; | ||
| use CompileError; | ||
| use Doctrine\Deprecations\Deprecation; | ||
| use Doctrine\Persistence\Mapping\ClassMetadata; | ||
| use Error; | ||
|
|
||
| /** | ||
| * The PHPDriver includes php files which just populate ClassMetadataInfo | ||
|
|
@@ -35,6 +39,33 @@ public function loadMetadataForClass(string $className, ClassMetadata $metadata) | |
| */ | ||
| protected function loadMappingFile(string $file): array | ||
| { | ||
| try { | ||
| $callback = Closure::bind(static function (string $file): mixed { | ||
| $metadata = null; | ||
|
|
||
| return include $file; | ||
| }, null, null)($file); | ||
| } catch (CompileError $e) { | ||
| throw $e; | ||
| } catch (Error) { | ||
| // Calling any method on $metadata=null will raise an Error | ||
| // Falling back to legacy behavior of expecting $metadata to be populated | ||
| $callback = null; | ||
| } | ||
|
|
||
| if ($callback instanceof Closure) { | ||
| $callback($this->metadata); | ||
|
|
||
| return [$this->metadata->getName() => $this->metadata]; | ||
| } | ||
|
|
||
| Deprecation::trigger( | ||
| 'doctrine/persistence', | ||
| 'https://github.com/doctrine/persistence/pull/450', | ||
| 'Not returning a Closure from a PHP mapping file is deprecated', | ||
| ); | ||
|
|
||
| unset($callback); | ||
| $metadata = $this->metadata; | ||
|
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. To be transparent, this introduces
I would deprecate this behaviors first. And maybe deprecate not returning a closure. 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. Yes to both. 💪🏻 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. The code way more verbose, but it can be greatly simplified for the next major version: protected function loadMappingFile(string $file): array
{
$callback = Closure::bind(static function ($file): mixed {
return include $file;
}, null, null)($file);
if (! $callback instanceof Closure) {
throw new MappingException('The PHP mapping file did not return a Closure.');
}
$callback($this->metadata);
return [$this->metadata->getName() => $this->metadata];
} |
||
| include $file; | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| use Doctrine\Persistence\Mapping\ClassMetadata; | ||
|
|
||
| assert($metadata instanceof ClassMetadata); | ||
| $metadata->getFieldNames(); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| use Doctrine\Persistence\Mapping\ClassMetadata; | ||
|
|
||
| return static function (ClassMetadata $metadata): void { | ||
GromNaN marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| $metadata->getFieldNames(); | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| use Doctrine\Persistence\Mapping\ClassMetadata; | ||
|
|
||
| return static function (ClassMetadata $metadata): void { | ||
| $metadata->isIdentifier(static::class); | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| return function (): void { | ||
| $this->setGlobalBasename('global-mapping'); | ||
| }; |
Uh oh!
There was an error while loading. Please reload this page.