8000 [DoctrineBridge] Determine attribute or annotation type for directories by cinamo · Pull Request #43754 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[DoctrineBridge] Determine attribute or annotation type for directories #43754

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

Merged
merged 1 commit into from
Nov 2, 2021
Merged
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
Determine attribute or annotation type for directories
  • Loading branch information
cinamo authored and nicolas-grekas committed Nov 2, 2021
commit 98454f8b44f5a2fdd1dc5b73b935435fe636b30e
10000
Original file line number Diff line number Diff line change
Expand Up @@ -89,25 +89,8 @@ protected function loadMappingInformation(array $objectManager, ContainerBuilder
if (!$mappingConfig) {
continue;
}
} elseif (!$mappingConfig['type'] && \PHP_VERSION_ID < 80000) {
$mappingConfig['type'] = 'annotation';
} elseif (!$mappingConfig['type']) {
$mappingConfig['type'] = 'attribute';

$glob = new GlobResource($mappingConfig['dir'], '*', true);
$container->addResource($glob);

foreach ($glob as $file) {
$content = file_get_contents($file);

if (preg_match('/^#\[.*Entity\b/m', $content)) {
break;
}
if (preg_match('/^ \* @.*Entity\b/m', $content)) {
$mappingConfig['type'] = 'annotation';
break;
}
}
$mappingConfig['type'] = $this->detectMappingType($mappingConfig['dir'], $container);
}

$this->assertValidMappingConfiguration($mappingConfig, $objectManager['name']);
Expand Down Expand Up @@ -259,7 +242,7 @@ protected function assertValidMappingConfiguration(array $mappingConfig, string
/**
* Detects what metadata driver to use for the supplied directory.
*
* @return string|null
* @return string|null A metadata driver short name, if one can be detected
*/
protected function detectMetadataDriver(string $dir, ContainerBuilder $container)
{
Expand All @@ -280,13 +263,48 @@ protected function detectMetadataDriver(string $dir, ContainerBuilder $container
}
$container->fileExists($resource, false);

return $container->fileExists($dir.'/'.$this->getMappingObjectDefaultName(), false) ? 'annotation' : null;
if ($container->fileExists($dir.'/'.$this->getMappingObjectDefaultName(), false)) {
return $this->detectMappingType($dir, $container);
}

return null;
}
$container->fileExists($dir.'/'.$configPath, false);

return $driver;
}

/**
* Detects what mapping type to use for the supplied directory.
*
* @return string A mapping type 'attribute' or 'annotation'
*/
private function detectMappingType(string $directory, ContainerBuilder $container): string
{
if (\PHP_VERSION_ID < 80000) {
return 'annotation';
}

$type = 'attribute';

$glob = new GlobResource($directory, '*', true);
$container->addResource($glob);

foreach ($glob as $file) {
$content = file_get_contents($file);

if (preg_match('/^#\[.*Entity\b/m', $content)) {
break;
}
if (preg_match('/^ \* @.*Entity\b/m', $content)) {
$type = 'annotation';
break;
}
}

return $type;
}

/**
* Loads a configured object manager metadata, query or result cache driver.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,23 @@ public function testFixManagersAutoMappings(array $originalEm1, array $originalE
], $expectedEm2));
}

public function testMappingTypeDetection()
{
$container = $this->createContainer();

$reflection = new \ReflectionClass(\get_class($this->extension));
$method = $reflection->getMethod('detectMappingType');
$method->setAccessible(true);

// The ordinary fixtures contain annotation
$mappingType = $method->invoke($this->extension, __DIR__.'/../Fixtures', $container);
$this->assertSame($mappingType, 'annotation');

// In the attribute folder, attributes are used
$mappingType = $method->invoke($this->extension, __DIR__.'/../Fixtures/Attribute', $container);
$this->assertSame($mappingType, \PHP_VERSION_ID < 80000 ? 'annotation' : 'attribute');
}

public function providerBasicDrivers()
{
return [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bridge\Doctrine\Tests\Fixtures\Attribute;

use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\Id;

#[Entity]
class UuidIdEntity
{
#[Id]
#[Column("uuid")]
protected $id;

public function __construct($id)
{
$this->id = $id;
}
}
0