forked from EasyCorp/EasyAdminBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMetadataConfigPass.php
More file actions
95 lines (77 loc) · 3.53 KB
/
MetadataConfigPass.php
File metadata and controls
95 lines (77 loc) · 3.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
/*
* This file is part of the EasyAdminBundle.
*
* (c) Javier Eguiluz <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace JavierEguiluz\Bundle\EasyAdminBundle\Configuration;
use Doctrine\Common\Persistence\ManagerRegistry;
use Doctrine\ORM\Mapping\ClassMetadata;
use Symfony\Component\Config\Definition\Exception\InvalidTypeException;
/**
* Introspects the metadata of the Doctrine entities to complete the
* configuration of the properties.
*
* @author Javier Eguiluz <[email protected]>
*/
class MetadataConfigPass implements ConfigPassInterface
{
/** @var ManagerRegistry */
private $doctrine;
public function __construct(ManagerRegistry $doctrine)
{
$this->doctrine = $doctrine;
}
public function process(array $backendConfig)
{
foreach ($backendConfig['entities'] as $entityName => $entityConfig) {
try {
$em = $this->doctrine->getManagerForClass($entityConfig['class']);
} catch (\ReflectionException $e) {
throw new InvalidTypeException(sprintf('The configured class "%s" for the path "easy_admin.entities.%s" does not exist. Did you forget to create the entity class or to define its namespace?', $entityConfig['class'], $entityName));
}
if (null === $em) {
throw new InvalidTypeException(sprintf('The configured class "%s" for the path "easy_admin.entities.%s" is no mapped entity.', $entityConfig['class'], $entityName));
}
$entityMetadata = $em->getMetadataFactory()->getMetadataFor($entityConfig['class']);
$entityConfig['primary_key_field_name'] = $entityMetadata->getSingleIdentifierFieldName();
$entityConfig['properties'] = $this->processEntityPropertiesMetadata($entityMetadata);
$backendConfig['entities'][$entityName] = $entityConfig;
}
return $backendConfig;
}
/**
* Takes the entity metadata introspected via Doctrine and completes its
* contents to simplify data processing for the rest of the application.
*
* @param ClassMetadata $entityMetadata The entity metadata introspected via Doctrine
*
* @return array The entity properties metadata provided by Doctrine
*/
private function processEntityPropertiesMetadata(ClassMetadata $entityMetadata)
{
$entityPropertiesMetadata = array();
if ($entityMetadata->isIdentifierComposite) {
throw new \RuntimeException(sprintf("The '%s' entity isn't valid because it contains a composite primary key.", $entityMetadata->name));
}
// introspect regular entity fields
foreach ($entityMetadata->fieldMappings as $fieldName => $fieldMetadata) {
$entityPropertiesMetadata[$fieldName] = $fieldMetadata;
}
// introspect fields for entity associations
foreach ($entityMetadata->associationMappings as $fieldName => $associationMetadata) {
$entityPropertiesMetadata[$fieldName] = array_merge($associationMetadata, array(
'type' => 'association',
'associationType' => $associationMetadata['type'],
));
// associations different from *-to-one cannot be sorted
if ($associationMetadata['type'] & ClassMetadata::TO_MANY) {
$entityPropertiesMetadata[$fieldName]['sortable'] = false;
}
}
return $entityPropertiesMetadata;
}
}