|
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::numeric($tableName, $key, $value) ? $value : "'".addslashes(trim($value))."'"); |
|
61
|
|
|
} elseif (\is_array($value)) { |
|
62
|
|
|
$whereSql .= $path.$value[0]; |
|
63
|
|
|
} else { |
|
64
|
|
|
$whereSql .= $path.$key.' = '.(self::numeric($tableName, $key, $value) ? $value : "'".addslashes(trim($value))."'"); |
|
65
|
|
|
} |
|
66
|
|
|
if ($key === $first) { |
|
67
|
|
|
$path = ' AND '; |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
return $whereSql; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
protected static function numeric($tableName, $key, $value) |
|
76
|
|
|
{ |
|
77
|
|
|
$intTypes = [ |
|
78
|
|
|
'boolean', |
|
79
|
|
|
'integer', |
|
80
|
|
|
'longint', |
|
81
|
|
|
]; |
|
82
|
|
|
$flip = []; |
|
83
|
|
|
if (isset(self::$mock[$tableName])) { |
|
84
|
|
|
$flip = array_flip(self::$mock[$tableName]); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
if (isset(self::$metadata[$tableName], $flip[$key])) { |
|
88
|
|
|
if (\in_array(self::$metadata[$tableName]->getFieldMapping($flip[$key])['type'], $intTypes, false)) { |
|
89
|
|
|
return true; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
return false; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
return is_numeric($value); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
protected static function getTable(&$object, &$tableName, &$columns, &$type, $manager = null, array $extraFields = []) |
|
99
|
|
|
{ |
|
100
|
|
|
self::init($manager); |
|
101
|
|
|
if (\is_object($object)) { |
|
102
|
|
|
self::extract($object); |
|
103
|
|
|
$tableName = self::$tableName; |
|
104
|
|
|
$columns = self::$mock[$tableName] ?: []; |
|
105
|
|
|
$type = 'object'; |
|
106
|
|
|
} else { |
|
107
|
|
|
$tableName = $object['table_name']; |
|
108
|
|
|
unset($object['table_name']); |
|
109
|
|
|
$type = 'table'; |
|
110
|
|
|
$columns = array_keys($object) ?: []; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
if (isset($extraFields[$tableName])) { |
|
114
|
|
|
$columns = array_merge($columns, $extraFields[$tableName]); |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
public static function init($manager = null) |
|
119
|
|
|
{ |
|
120
|
|
|
if (self::$connection) { |
|
121
|
|
|
return; |
|
122
|
|
|
} |
|
123
|
|
|
global $kernel; |
|
124
|
|
|
|
|
125
|
|
|
if (AppCache::class === \get_class($kernel)) { |
|
126
|
|
|
$kernel = $kernel->getKernel(); |
|
127
|
|
|
} |
|
128
|
|
|
$container = $kernel->getContainer(); |
|
129
|
|
|
|
|
130
|
|
|
$manager = $manager ?: $container->getParameter('sludio_helper.entity.manager'); |
|
131
|
|
|
if (\is_object($manager)) { |
|
132
|
|
|
self::$entityManager = $manager; |
|
133
|
|
|
} else { |
|
134
|
|
|
self::$entityManager = $container->get('doctrine')->getManager($manager); |
|
135
|
|
|
} |
|
136
|
|
|
self::$connection = self::$entityManager->getConnection(); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
protected static function extract($object) |
|
140
|
|
|
{ |
|
141
|
|
|
self::init(false); |
|
142
|
|
|
$data = self::extractExt(self::$entityManager->getMetadataFactory()->getMetadataFor(\get_class($object))); |
|
143
|
|
|
|
|
144
|
|
|
self::$mock = $data['mock']; |
|
145
|
|
|
self::$tableName = $data['table']; |
|
146
|
|
|
self::$metadata[$data['table']] = $data['meta']; |
|
147
|
|
|
self::$identifier = $data['identifier']; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
public static function extractExt(ClassMetadata $metadata) |
|
151
|
|
|
{ |
|
152
|
|
|
$fields = $metadata->getFieldNames(); |
|
153
|
|
|
$columns = $metadata->getColumnNames(); |
|
154
|
|
|
$table = $metadata->getTableName(); |
|
155
|
|
|
$identifier = null; |
|
156
|
|
|
|
|
157
|
|
|
$result = []; |
|
158
|
|
|
foreach ($fields as $key => $field) { |
|
159
|
|
|
/** @var $columns array */ |
|
160
|
|
|
foreach ($columns as $key2 => $column) { |
|
161
|
|
|
if ($key === $key2) { |
|
162
|
|
|
$result[$table][$field] = $column; |
|
163
|
|
|
if ($field === $metadata->getIdentifier()[0]) { |
|
164
|
|
|
$identifier = $column; |
|
165
|
|
|
} |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
$data = [ |
|
171
|
|
|
'mock' => $result, |
|
172
|
|
|
'table' => $table, |
|
173
|
|
|
'meta' => $metadata, |
|
174
|
|
|
'identifier' => $identifier, |
|
175
|
|
|
]; |
|
176
|
|
|
|
|
177
|
|
|
return $data; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
protected static function value($object, $variable, $type, $check = true) |
|
181
|
|
|
{ |
|
182
|
|
|
$value = null; |
|
183
|
|
|
if ($type === 'object') { |
|
184
|
|
|
$value = $object->{'get'.ucfirst(Helper::toCamelCase($variable))}(); |
|
185
|
|
|
} else { |
|
186
|
|
|
if (isset($object[$variable])) { |
|
187
|
|
|
$value = $object[$variable]; |
|
188
|
|
|
} |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
if ($check) { |
|
192
|
|
|
Helper::variable($value); |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
return $value; |
|
196
|
|
|
} |
|
197
|
|
|
} |
|
198
|
|
|
|
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