forked from EasyCorp/EasyAdminBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOdmConfigPass.php
More file actions
111 lines (96 loc) · 3.23 KB
/
OdmConfigPass.php
File metadata and controls
111 lines (96 loc) · 3.23 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
namespace JavierEguiluz\Bundle\EasyAdminBundle\Configuration;
/**
* Providing support the the 'documents' config entry.
* The entities are an abstract construct, hence all logic is applied to ODM
* documents as they were entities. Thanks to Javier for the tip!
*
* @author Thomas Parys <[email protected]>
*
* By default the entity name is used as its label (showed in buttons, the
* main menu, etc.). That's why the config format #3 can optionally define
* a custom entity label
*
* easy_admin:
* entities:
* User:
* class: AppBundle\Entity\User
* label: 'Clients'
*
* For documents:
*
* # Config format #1: no custom document name
* easy_admin:
* documents:
* - AppBundle\Document\User
*
* # Config format #2: simple config with custom document name
* easy_admin:
* documents:
* User: AppBundle\Document\User
*
*/
class OdmConfigPass implements ConfigPassInterface
{
/**
* Mapping element types to Doctrine service bundle class names.
* @var array
*/
protected $dataServices = [
'entities' => 'dataservice_orm',
'documents' => 'dataservice_mongo',
];
/**
* For an easy and unified processing, documents should not differ
* from their entities counterparts.
* From now on merged as one.
*
* @param $backendConfig
* @return mixed
*/
private function processMergeDocumentsIntoEntities($backendConfig)
{
if (isset($backendConfig['entities'])) {
foreach ($backendConfig['entities'] as $entityName => $entityConfig) {
// normalize config formats #1 and #2 to use the 'class' option as config format #3
if (!is_array($entityConfig)) {
$entityConfig = array('class' => $entityConfig);
}
$entityConfig['data_manager_service']
= $this->dataServices['entities'];
$backendConfig['entities'][$entityName] = $entityConfig;
}
}
if (isset($backendConfig['documents'])) {
foreach ($backendConfig['documents'] as $entityName => $entityConfig) {
// normalize config formats #1 and #2 to use the 'class' option as config format #3
if (!is_array($entityConfig)) {
$entityConfig = array('class' => $entityConfig);
}
$entityConfig['data_manager_service']
= $this->dataServices['documents'];
$backendConfig['documents'][$entityName] = $entityConfig;
}
}
if (!isset($backendConfig['documents'])) {
return $backendConfig;
}
$backendConfig['entities'] =
$backendConfig['entities'] +
$backendConfig['documents'];
unset($backendConfig['documents']);
// rekey
$backendConfig['entities'] = array_values($backendConfig['entities']);
return $backendConfig;
}
/**
* @param array $backendConfig
*
* @return array
*/
public function process(array $backendConfig)
{
$backendConfig = $this->processMergeDocumentsIntoEntities($backendConfig);
return $backendConfig;
}
}