1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* compiler pass for building a listing of fields for compiler |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace Graviton\DocumentBundle\DependencyInjection\Compiler; |
7
|
|
|
|
8
|
|
|
use Graviton\DocumentBundle\DependencyInjection\Compiler\Utils\Document; |
9
|
|
|
use Graviton\DocumentBundle\DependencyInjection\Compiler\Utils\DocumentMap; |
10
|
|
|
use Graviton\DocumentBundle\DependencyInjection\Compiler\Utils\ArrayField; |
11
|
|
|
use Graviton\DocumentBundle\DependencyInjection\Compiler\Utils\EmbedMany; |
12
|
|
|
use Graviton\DocumentBundle\DependencyInjection\Compiler\Utils\EmbedOne; |
13
|
|
|
use Graviton\DocumentBundle\DependencyInjection\Compiler\Utils\Field; |
14
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
15
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @author List of contributors <https://github.com/libgraviton/graviton/graphs/contributors> |
19
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GNU Public License |
20
|
|
|
* @link http://swisscom.ch |
21
|
|
|
*/ |
22
|
|
|
class DocumentFormFieldsCompilerPass implements CompilerPassInterface |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var DocumentMap |
26
|
|
|
*/ |
27
|
|
|
private $documentMap; |
28
|
|
|
/** |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
private $typeMap = [ |
32
|
|
|
'string' => 'text', |
33
|
|
|
'extref' => 'extref', |
34
|
|
|
'int' => 'integer', |
35
|
|
|
'float' => 'number', |
36
|
|
|
'boolean' => 'strictboolean', |
37
|
|
|
'date' => 'datetime', |
38
|
|
|
]; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Constructor |
42
|
|
|
* |
43
|
|
|
* @param DocumentMap $documentMap Document map |
44
|
|
|
*/ |
45
|
4 |
|
public function __construct(DocumentMap $documentMap) |
46
|
|
|
{ |
47
|
4 |
|
$this->documentMap = $documentMap; |
48
|
4 |
|
} |
49
|
|
|
|
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* load services |
53
|
|
|
* |
54
|
|
|
* @param ContainerBuilder $container container builder |
55
|
|
|
* |
56
|
|
|
* @return void |
57
|
|
|
*/ |
58
|
4 |
View Code Duplication |
public function process(ContainerBuilder $container) |
|
|
|
|
59
|
|
|
{ |
60
|
4 |
|
$map = ['stdclass' => []]; |
61
|
4 |
|
foreach ($this->documentMap->getDocuments() as $document) { |
62
|
4 |
|
$map[$document->getClass()] = $this->getFormFields($document); |
63
|
2 |
|
} |
64
|
4 |
|
$container->setParameter('graviton.document.form.type.document.field_map', $map); |
65
|
4 |
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Get document fields |
69
|
|
|
* |
70
|
|
|
* @param Document $document Document |
71
|
|
|
* @return array |
72
|
|
|
*/ |
73
|
4 |
|
private function getFormFields(Document $document) |
74
|
|
|
{ |
75
|
4 |
|
$reflection = new \ReflectionClass($document->getClass()); |
76
|
4 |
View Code Duplication |
if ($reflection->implementsInterface('Graviton\I18nBundle\Document\TranslatableDocumentInterface')) { |
|
|
|
|
77
|
4 |
|
$instance = $reflection->newInstanceWithoutConstructor(); |
78
|
4 |
|
$translatableFields = $instance->getTranslatableFields(); |
79
|
2 |
|
} else { |
80
|
2 |
|
$translatableFields = []; |
81
|
|
|
} |
82
|
|
|
|
83
|
4 |
|
$result = []; |
84
|
4 |
|
foreach ($document->getFields() as $field) { |
85
|
4 |
|
if ($field instanceof Field) { |
86
|
4 |
|
list($type, $options) = $this->resolveFieldParams( |
87
|
2 |
|
$translatableFields, |
88
|
4 |
|
$field->getFieldName(), |
89
|
4 |
|
$field->getType(), |
90
|
|
|
$field |
91
|
2 |
|
); |
92
|
|
|
|
93
|
4 |
|
$result[] = [ |
94
|
4 |
|
$field->getFormName(), |
95
|
4 |
|
$type, |
96
|
4 |
|
array_replace( |
97
|
4 |
|
['property_path' => $field->getFieldName()], |
98
|
|
|
$options |
99
|
2 |
|
), |
100
|
|
|
]; |
101
|
3 |
|
} elseif ($field instanceof ArrayField) { |
102
|
2 |
|
list($type, $options) = $this->resolveFieldParams( |
103
|
1 |
|
$translatableFields, |
104
|
2 |
|
$field->getFieldName(), |
105
|
2 |
|
$field->getItemType() |
106
|
1 |
|
); |
107
|
|
|
|
108
|
2 |
|
$result[] = [ |
109
|
2 |
|
$field->getFormName(), |
110
|
2 |
|
'collection', |
111
|
|
|
[ |
112
|
2 |
|
'property_path' => $field->getFieldName(), |
113
|
2 |
|
'type' => $type, |
114
|
2 |
|
'options' => $options, |
115
|
1 |
|
], |
116
|
|
|
]; |
117
|
2 |
|
} elseif ($field instanceof EmbedOne) { |
118
|
4 |
|
$result[] = [ |
119
|
4 |
|
$field->getFormName(), |
120
|
4 |
|
'form', |
121
|
|
|
[ |
122
|
4 |
|
'property_path' => $field->getFieldName(), |
123
|
4 |
|
'data_class' => $field->getDocument()->getClass(), |
124
|
4 |
|
'required' => $field->isRequired(), |
125
|
2 |
|
], |
126
|
|
|
]; |
127
|
2 |
|
} elseif ($field instanceof EmbedMany) { |
128
|
4 |
|
$result[] = [ |
129
|
4 |
|
$field->getFormName(), |
130
|
4 |
|
'collection', |
131
|
|
|
[ |
132
|
4 |
|
'property_path' => $field->getFieldName(), |
133
|
4 |
|
'type' => 'form', |
134
|
4 |
|
'options' => ['data_class' => $field->getDocument()->getClass()], |
135
|
2 |
|
], |
136
|
|
|
]; |
137
|
2 |
|
} |
138
|
2 |
|
} |
139
|
4 |
|
return $result; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Resolve simple field type |
144
|
|
|
* |
145
|
|
|
* @param array $translatable Translatable fields |
146
|
|
|
* @param string $fieldName Field name |
147
|
|
|
* @param string $fieldType Field type |
148
|
|
|
* @param mixed $field optional field to pass |
149
|
|
|
* |
150
|
|
|
* @return array Form type and options |
151
|
|
|
*/ |
152
|
4 |
|
private function resolveFieldParams(array $translatable, $fieldName, $fieldType, $field = null) |
153
|
|
|
{ |
154
|
4 |
|
$options = []; |
155
|
|
|
|
156
|
4 |
|
if (in_array($fieldName, $translatable, true) || in_array($fieldName.'[]', $translatable, true)) { |
157
|
4 |
|
$type = 'translatable'; |
158
|
4 |
|
if ($field instanceof Field) { |
159
|
4 |
|
$options['required'] = $field->isRequired(); |
160
|
2 |
|
} |
161
|
4 |
|
} elseif ($fieldType === 'hash') { |
162
|
4 |
|
$type = 'freeform'; |
163
|
4 |
|
} elseif ($fieldType === 'hasharray') { |
164
|
2 |
|
$type = 'collection'; |
165
|
2 |
|
$options['type'] = 'freeform'; |
166
|
4 |
|
} elseif ($fieldType === 'datearray') { |
167
|
2 |
|
$type = 'datearray'; |
168
|
4 |
|
} elseif (isset($this->typeMap[$fieldType])) { |
169
|
4 |
|
$type = $this->typeMap[$fieldType]; |
170
|
2 |
|
} else { |
171
|
2 |
|
$type = 'text'; |
172
|
|
|
} |
173
|
|
|
|
174
|
4 |
|
return [$type, $options]; |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.