1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sludio\HelperBundle\Script\Repository; |
4
|
|
|
|
5
|
|
|
use AppCache; |
|
|
|
|
6
|
|
|
use Doctrine\Common\Persistence\Mapping\ClassMetadata; |
7
|
|
|
use Sludio\HelperBundle\Script\Utils\Helper; |
8
|
|
|
|
9
|
|
|
abstract class QuickInsertFunctions |
10
|
|
|
{ |
11
|
|
|
public static $entityManager; |
12
|
|
|
public static $connection; |
13
|
|
|
protected static $mock = []; |
14
|
|
|
protected static $metadata = []; |
15
|
|
|
protected static $tableName; |
16
|
|
|
protected static $identifier; |
17
|
|
|
|
18
|
|
|
protected static function buildExtra($extra) |
19
|
|
|
{ |
20
|
|
|
$methods = [ |
21
|
|
|
'GROUP BY', |
22
|
|
|
'HAVING', |
23
|
|
|
'ORDER BY', |
24
|
|
|
]; |
25
|
|
|
$sql = ''; |
26
|
|
|
|
27
|
|
|
foreach ($methods as $method) { |
28
|
|
|
if (isset($extra[$method])) { |
29
|
|
|
$sql .= ' '.$method.' '; |
30
|
|
|
if (\is_array($extra[$method])) { |
31
|
|
|
$sql .= implode(' ', $extra[$method]).' '; |
32
|
|
|
} else { |
33
|
|
|
$sql .= $extra[$method].' '; |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
if (isset($extra['LIMIT']) && \is_array($extra['LIMIT'])) { |
39
|
|
|
if (isset($extra['LIMIT'][1])) { |
40
|
|
|
list($offset, $limit) = $extra['LIMIT']; |
41
|
|
|
} else { |
42
|
|
|
$offset = 0; |
43
|
|
|
$limit = $extra['LIMIT'][0]; |
44
|
|
|
} |
45
|
|
|
$sql = sprintf('%sLIMIT %s, %s', $sql, $offset, $limit); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
return Helper::oneSpace($sql); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
protected static function buildWhere($tableName, array $where) |
52
|
|
|
{ |
53
|
|
|
$whereSql = ''; |
54
|
|
|
if (!empty($where)) { |
55
|
|
|
reset($where); |
56
|
|
|
$first = key($where); |
57
|
|
|
$path = ' WHERE '; |
58
|
|
|
foreach ($where as $key => $value) { |
59
|
|
|
if (!\is_array($value) && isset(self::$mock[$tableName][$key])) { |
60
|
|
|
$whereSql .= $path.self::$mock[$tableName][$key].' = '.self::slashes($tableName, $key, $value); |
61
|
|
|
} elseif (\is_array($value)) { |
62
|
|
|
$whereSql .= $path.$value[0]; |
63
|
|
|
} else { |
64
|
|
|
$whereSql .= $path.$key.' = '.self::slashes($tableName, $key, $value); |
65
|
|
|
} |
66
|
|
|
if ($key === $first) { |
67
|
|
|
$path = ' AND '; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $whereSql; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
protected static function slashes($tableName, $key, $value) |
76
|
|
|
{ |
77
|
|
|
if ($value instanceof \DateTime) { |
78
|
|
|
$result = "'".addslashes(trim($value->format('Y-m-d H:i:s')))."'"; |
79
|
|
|
} else { |
80
|
|
|
$result = self::numeric($tableName, $key, $value) ? $value : "'".addslashes(trim($value))."'"; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$trim = trim($result); |
84
|
|
|
if ($trim === '' || $trim === "''") { |
85
|
|
|
$result = null; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return $result; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
protected static function numeric($tableName, $key, $value) |
92
|
|
|
{ |
93
|
|
|
$intTypes = [ |
94
|
|
|
'boolean', |
95
|
|
|
'integer', |
96
|
|
|
'longint', |
97
|
|
|
]; |
98
|
|
|
$flip = []; |
99
|
|
|
if (isset(self::$mock[$tableName])) { |
100
|
|
|
$flip = array_flip(self::$mock[$tableName]); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
if (isset(self::$metadata[$tableName], $flip[$key])) { |
104
|
|
|
if (\in_array(self::$metadata[$tableName]->getFieldMapping($flip[$key])['type'], $intTypes, false)) { |
105
|
|
|
return true; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return false; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return is_numeric($value); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
protected static function getTable(&$object, &$tableName, &$columns, &$type, $manager = null, array $extraFields = []) |
115
|
|
|
{ |
116
|
|
|
self::init($manager); |
117
|
|
|
if (\is_object($object)) { |
118
|
|
|
self::extract($object); |
119
|
|
|
$tableName = self::$tableName; |
120
|
|
|
$columns = self::$mock[$tableName] ?: []; |
121
|
|
|
$type = 'object'; |
122
|
|
|
} else { |
123
|
|
|
$tableName = $object['table_name']; |
124
|
|
|
unset($object['table_name']); |
125
|
|
|
$type = 'table'; |
126
|
|
|
$columns = array_keys($object) ?: []; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
if (isset($extraFields[$tableName])) { |
130
|
|
|
$columns = array_merge($columns, $extraFields[$tableName]); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public static function init($manager = null) |
135
|
|
|
{ |
136
|
|
|
if (self::$connection) { |
137
|
|
|
return; |
138
|
|
|
} |
139
|
|
|
global $kernel; |
140
|
|
|
|
141
|
|
|
if (AppCache::class === \get_class($kernel)) { |
142
|
|
|
$kernel = $kernel->getKernel(); |
143
|
|
|
} |
144
|
|
|
$container = $kernel->getContainer(); |
145
|
|
|
|
146
|
|
|
$manager = $manager ?: $container->getParameter('sludio_helper.entity.manager'); |
147
|
|
|
if (\is_object($manager)) { |
148
|
|
|
self::$entityManager = $manager; |
149
|
|
|
} else { |
150
|
|
|
self::$entityManager = $container->get('doctrine')->getManager($manager); |
151
|
|
|
} |
152
|
|
|
self::$connection = self::$entityManager->getConnection(); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
protected static function extract($object) |
156
|
|
|
{ |
157
|
|
|
self::init(false); |
158
|
|
|
$data = self::extractExt(self::$entityManager->getMetadataFactory()->getMetadataFor(\get_class($object))); |
159
|
|
|
|
160
|
|
|
self::$mock = $data['mock']; |
161
|
|
|
self::$tableName = $data['table']; |
162
|
|
|
self::$metadata[$data['table']] = $data['meta']; |
163
|
|
|
self::$identifier = $data['identifier']; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
public static function extractExt(ClassMetadata $metadata) |
167
|
|
|
{ |
168
|
|
|
$fields = $metadata->getFieldNames(); |
169
|
|
|
$columns = $metadata->getColumnNames(); |
170
|
|
|
$table = $metadata->getTableName(); |
171
|
|
|
$identifier = null; |
172
|
|
|
|
173
|
|
|
$result = []; |
174
|
|
|
foreach ($fields as $key => $field) { |
175
|
|
|
/** @var $columns array */ |
176
|
|
|
foreach ($columns as $key2 => $column) { |
177
|
|
|
if ($key === $key2) { |
178
|
|
|
$result[$table][$field] = $column; |
179
|
|
|
if ($field === $metadata->getIdentifier()[0]) { |
180
|
|
|
$identifier = $column; |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
$data = [ |
187
|
|
|
'mock' => $result, |
188
|
|
|
'table' => $table, |
189
|
|
|
'meta' => $metadata, |
190
|
|
|
'identifier' => $identifier, |
191
|
|
|
]; |
192
|
|
|
|
193
|
|
|
return $data; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
protected static function parseUpdateResult($object, $type, $id, $tableName, array $result = null) |
197
|
|
|
{ |
198
|
|
|
$data = []; |
199
|
|
|
if (!empty($result)) { |
200
|
|
|
foreach ($result as $key => $value) { |
201
|
|
|
$content = self::value($object, $key, $type, $tableName, false); |
202
|
|
|
if ($id && !\in_array($content, [ |
203
|
|
|
null, |
204
|
|
|
$value, |
205
|
|
|
], true)) { |
206
|
|
|
$data[$key] = $content; |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
return $data; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
protected static function value($object, $variable, $type, $tableName, $check = true) |
215
|
|
|
{ |
216
|
|
|
$value = null; |
217
|
|
|
if ($type === 'object') { |
218
|
|
|
$value = $object->{'get'.ucfirst(Helper::toCamelCase($variable))}(); |
219
|
|
|
} else { |
220
|
|
|
if (isset($object[$variable])) { |
221
|
|
|
$value = $object[$variable]; |
222
|
|
|
} |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
if ($check) { |
226
|
|
|
self::slashes($tableName, $variable, $value); |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
return $value; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
protected static function makeValues($tableName, $data) |
233
|
|
|
{ |
234
|
|
|
$values = ''; |
235
|
|
|
foreach ($data as $key => $value) { |
236
|
|
|
$values .= self::slashes($tableName, $key, $value).','; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
return \substr($values, 0, -1); |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths