forked from mmucklo/DtcGridBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GridSourceGenerator.php
executable file
·175 lines (147 loc) · 6.18 KB
/
GridSourceGenerator.php
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php
namespace Dtc\GridBundle\Generator;
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
use Doctrine\ORM\Mapping\ClassMetadataInfo;
use Dtc\GridBundle\Util\CamelCase;
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
use Symfony\Component\Yaml\Yaml;
/**
* @deprecated
* Class GridSourceGenerator
*/
class GridSourceGenerator extends Generator
{
private $saveCache;
private $skeletonDir;
public function __construct($skeletonDir)
{
$this->skeletonDir = $skeletonDir;
}
protected function generateColumns(BundleInterface $bundle, $entity, $metadata)
{
$parts = explode('\\', $entity);
$entityClass = array_pop($parts);
$entityNamespace = implode('\\', $parts);
$gridColumnsNamespace = $bundle->getNamespace().'\\Grid\\Columns';
$gridColumnsNamespace .= ($entityNamespace) ? '\\'.$entityNamespace : '';
$gridColumnClass = $entityClass.'GridColumn';
$dirPath = $bundle->getPath().'/Grid/Columns';
$gridColumnPath = $dirPath.'/'.str_replace('\\', '/', $entity).'GridColumn.php';
$templatePath = $bundle->getPath().'/Resources/views/'.str_replace('\\', '/', $entity).'/_grid.html.twig';
$fields = [];
foreach ($this->getFieldsFromMetadata($metadata) as $field) {
$fields[$field] = CamelCase::fromCamelCase($field);
}
$params = [
'fields' => $fields,
'namespace' => $gridColumnsNamespace,
'class' => $gridColumnClass,
'template_name' => "{$bundle->getName()}:{$entity}:_grid.html.twig",
];
$this->saveCache[$templatePath] = $this->render($this->skeletonDir, 'grid_template.html.twig', $params);
$this->saveCache[$gridColumnPath] = $this->render($this->skeletonDir, 'GridColumns.php.twig', $params);
return [$gridColumnClass, $gridColumnsNamespace, $gridColumnPath, $templatePath];
}
public function generate(BundleInterface $bundle, $entityDocument, $metadata, $columns = false)
{
if ($metadata instanceof ClassMetadataInfo) {
$manager = '@doctrine.orm.default_entity_manager';
$class = 'Dtc\GridBundle\Grid\Source\EntityGridSource';
} elseif ($metadata instanceof \Doctrine\ODM\MongoDB\Mapping\ClassMetadataInfo) {
$manager = '@doctrine_mongodb.odm.default_document_manager';
$class = 'Dtc\GridBundle\Grid\Source\DocumentGridSource';
} else {
throw new \Exception(__METHOD__.' - Unknown class for metadata: '.get_class($metadata));
}
$entityDocumentClassPath = $metadata->getReflectionClass()->getName();
$parts = explode('\\', $entityDocument);
$entityDocumentClass = array_pop($parts);
$files = [];
if ($columns) {
list($gridColumnClass, $gridColumnsNamespace, $gridColumnPath, $templatePath) =
$this->generateColumns($bundle, $entityDocument, $metadata);
$files = [
'grid_columns' => $gridColumnPath,
'grid_template' => $templatePath,
];
}
// Check to see if the files exists
if ($this->saveCache) {
foreach ($this->saveCache as $file => $output) {
$this->saveFile($file, $output);
}
}
$config = [];
$serviceName = 'grid.source.'.strtolower($entityDocumentClass);
$config[$serviceName] = [
'class' => $class,
'arguments' => [$manager, $entityDocumentClassPath],
'tags' => [['name' => 'dtc_grid.source']],
'calls' => [
['autoDiscoverColumns'],
], ];
if ($columns && isset($gridColumnsNamespace) && isset($gridColumnClass)) {
$config[$serviceName]['calls'] = [
['setColumns', [
'@'.$serviceName.'.columns',
],
],
];
$config[$serviceName.'.columns'] = [
'class' => $gridColumnsNamespace.'\\'.$gridColumnClass,
'arguments' => ['@twig'],
];
}
$configFile = $bundle->getPath().'/Resources/config/grid.yml';
$services = [];
if (file_exists($configFile)) {
$services = Yaml::parse($contents = file_get_contents($configFile));
if (isset($services['services'])) {
$services['services'] = array_merge($services['services'], $config);
} else {
$services['services'] = $config;
}
} else {
$services['services'] = $config;
}
$this->saveFile($configFile, Yaml::dump($services, 3));
$params = [
'gridsource_id' => $serviceName,
'files' => $files,
];
$output = $this->render($this->skeletonDir, 'controller.php.twig', $params);
echo $output;
exit();
}
private function getFieldsFromMetadata($metadata)
{
if ($metadata instanceof ClassMetadataInfo) {
$fields = $metadata->getFieldNames();
// Remove the primary key field if it's not managed manually
if (!$metadata->isIdentifierNatural()) {
$fields = array_diff($fields, $metadata->getIdentifier());
}
foreach ($metadata->associationMappings as $fieldName => $relation) {
if (ClassMetadataInfo::ONE_TO_MANY !== $relation['type']) {
$fields[] = $fieldName;
}
}
return $fields;
} elseif ($metadata instanceof ClassMetadata) {
$fields = $metadata->getFieldNames();
$retFields = [];
$identifier = $metadata->getIdentifier();
$identifier = isset($identifier[0]) ? $identifier[0] : null;
foreach ($fields as $field) {
if ($identifier === $field) {
$mapping = $metadata->getFieldMapping($field);
if (isset($mapping['strategy']) && 'auto' == $mapping['strategy']) {
continue;
}
}
$retFields[] = $field;
}
return $retFields;
}
}
}