forked from dev-labs-bg/laravel-csv-importer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CSVImporter.php
389 lines (342 loc) · 12.5 KB
/
CSVImporter.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
<?php namespace YavorIvanov\CsvImporter;
use MJS\TopSort\Implementations\ArraySort;
use Illuminate\Support\Facades\Event;
use Illuminate\Database\Eloquent\Model as Eloquent;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Config;
use \DateTime;
include_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'include_dir.php');
function listify($val)
{
if (! is_array($val))
return [$val];
return $val;
}
abstract class CSVImporter
{
protected $column_mappings = [];
private $processors = [];
public $file = '';
protected $model = '';
protected $cache_key = [];
protected static $deps = [];
protected $context = [];
protected $cache = Null;
public static $modes = ['default'=>'append', 'overwrite', 'update', 'validate'];
private $mode = '';
// Primary key - used for intra-csv file relations.
// The cache key is used if no primary key is defined.
protected $primary_key = '';
abstract protected function import_row($row);
// PHP doesn't allow lambdas in the main class body.
protected function get_processors()
{
return [
'integer' => function ($v) { return intval($v); },
'to_datetime' => function ($v, $fmt='d/m/y H:i')
{
$created_at = DateTime::createFromFormat($fmt, $v);
return $created_at->format('Y-m-d H:i:s');
},
'string_to_null' => function ($v)
{
if (strtolower($v) == 'null')
return Null;
return $v;
},
];
}
protected function get_validators()
{
return [
'unique' => function ($col, $row)
{
$val = $row[$col];
$current_obj = $this->get_from_cache($row);
$model_col = array_get($this->column_mappings, "$col.name", $col);
$occurences = $this->cache->reduce(function($carry, $o) use ($current_obj, $model_col, $val) {
if ($o != $current_obj && strtolower($o->$model_col) == strtolower($val))
return $carry + 1;
return $carry;
}, 0);
if ($occurences > 0)
{
Log::error("A $this->name with $col = $val already exists in $this->file.");
die;
}
},
];
}
public static function get_importers()
{
// TODO <Yavor>: There's lots of duplicated code with the exporter here.
// Move this somewhere where both can reuse it.
$path = Config::get('csv-importer::import.register_path');
$match_file = (Config::get('csv-importer::import.file_match'));
include_dir($path . $match_file);
$importer_classes = array_filter(get_declared_classes(), function ($cls)
{
// Match all importers and exclude self.
$match = preg_match(Config::get('csv-importer::import.class_match_pattern'), $cls);
return $match && $cls != __CLASS__;
});
foreach ($importer_classes as $key=>$cls)
{
$name = $cls::importer_name($cls);
$importer_classes[$name] = $cls;
unset($importer_classes[$key]);
}
return $importer_classes;
}
public static function get_importer($name)
{
$importers = self::get_importers();
if (array_key_exists($name, $importers))
return $importers[$name];
return Null;
}
public static function sort_importers($importers)
{
foreach ($importers as $name=>$cls)
$importers[$name] = $cls::get_dependencies();
$sorter = new ArraySort;
$sorter->set($importers);
$importers = $sorter->sort();
return $importers;
}
protected function pivot_row($row)
{
return [$row];
}
protected function add_additional($model_object, $to_add)
{
return [$to_add];
}
protected function get_from_cache($val)
{
if (! $this->cache_key)
return Null;
if (is_array($val))
$val = $val[key($this->cache_key)];
$model_key = current($this->cache_key);
return $this->cache->from_index($val);
}
protected function get_from_context($ctx, $key)
{
$key = strtolower($key);
try
{
return $this->context[$ctx][$key];
}
catch (ErrorException $e)
{
echo "\n";
Log::error(
ucfirst($this->name) . ' importer failed to find a ' . $ctx . ' row with key: ' .
$key . '. Please make sure that this record exists.'
);
if ($this->mode == 'validate')
return reset($this->context[$ctx]);
die;
}
}
public static function importer_name($cls)
{
preg_match(Config::get('csv-importer::import.class_match_pattern'), $cls, $groups);
return strtolower($groups[1]);
}
protected function delete_records()
{
call_user_func_array([$this->model, 'whereNotNull'], ['id'])->delete();
}
public static function get_dependencies()
{
$cls = get_called_class();
return $cls::$deps;
}
protected function process($processor, $params)
{
return call_user_func_array($this->processors[$processor], listify($params));
}
protected function validate($processor, $params)
{
return call_user_func_array($this->validators[$processor], listify($params));
}
function __construct()
{
// Derived classes inherit the base class validators/processors.
$this->validators = array_merge(
self::get_validators(),
$this->get_validators()
);
$this->processors = array_merge(
self::get_processors(),
$this->get_processors()
);
$csv_path = Config::get('csv-importer::import.default_csv_path');
$this->file = app_path() . $csv_path . $this->file;
$this->name = self::importer_name(get_class($this));
if (! $this->primary_key && $this->cache_key)
$this->primary_key = $this->cache_key;
$this->context[$this->name] = [];
}
public function parse($file=Null, $offset=0, $limit=Null)
{
Event::fire('import.parse.start');
$file = $file ?: $this->file;
$CSV = new \mnshankar\CSV\CSV();
$rows = $CSV->fromFile($file)->toArray();
$limit = $limit ?: count($rows);
$rows = array_slice($rows, $offset, $limit);
$res = [];
foreach ($rows as $idx=>$row)
{
foreach ($this->pivot_row($row) as $row)
{
$row = $this->parse_row($row);
$this->validate_row($row);
array_push($res, $row);
}
}
return $res;
Event::fire('import.parse.end');
}
public function import($file=Null, $mode='append', $offset=0, $limit=Null)
{
DB::beginTransaction();
$this->mode = $mode;
if ($mode == 'update' && !method_exists($this, 'update'))
throw new \BadMethodCallException("Importer $this->name (" .
get_called_class() .
") called in update mode, but doesn't define an update function.");
// Resolve dependencies.
$dep2class = $this::get_importers();
foreach ($this::get_dependencies() as $name)
{
$dep = new $dep2class[$name];
$dep_import_mode = $mode;
// Don't cascade overwrite.
//
// TODO <Yavor>: Add an option to override this.
// Maybe have override-single and override-cascade modes?
// Cascading override could just show the list of dependencies that would be overriden,
// along with a strong warning about what that means about database cascade deletion, etc.
if ($mode == 'overwrite')
$dep_import_mode = 'append';
$dep->import(Null, $dep_import_mode);
$this->context = array_merge($this->context, $dep->context);
}
if ($mode == 'overwrite')
$this->delete_records();
if ($this->model && $this->cache_key)
{
$this->cache = IndexedCollection::makeFromCollection(call_user_func([$this->model, 'all']))
->case_insensitive_index(current($this->cache_key));
if ($this->primary_key)
{
foreach ($this->cache as $o)
{
$primary_key = $this->primary_key;
if (is_array($primary_key))
$primary_key = reset($primary_key);
$this->context[$this->name][strtolower($o->$primary_key)] = $o;
}
}
}
$rows = $this->parse($file, $offset, $limit);
Event::fire('import.start', array(count($rows), $this->name));
foreach ($rows as $row)
{
$o = Null;
$o = $this->get_from_cache($row);
if ($mode == 'update' && $o)
$this->update($row, $o);
if (! $o)
{
Eloquent::unguard();
$o = $this->import_row($row);
Eloquent::reguard();
}
$this->add_additional($o, $row);
if ($this->cache != Null)
$this->cache->index($o, current($this->cache_key));
if ($this->primary_key)
$this->context[$this->name][strtolower($row[key($this->primary_key)])] = $o;
Event::fire('import.update.progress');
}
if ($mode != 'validate')
DB::commit();
else
DB::rollback();
Event::fire('import.complete');
}
protected function parse_row($row)
{
// NOTE <Yavor>: CSV cells with no values for a column get removed from the
// $row array, instead of defaulting to a NULL value, as NULL may be
// a legitimate value.
//
// FIXME <Yavor>: As of the moment, there is no way to default missing cells to any
// value via processors, as they are eliminated before the preprocessing step.
// For now, you'll need to manually check if the column exists in the
// $row array in the import/update.
$cols_values = array_filter($row);
$column_processors = array_where($this->column_mappings, function($k, $v)
{
return array_key_exists('processors', $v);
});
foreach ($column_processors as $k=>$v)
$column_processors[$k] = $v['processors'];
foreach ($column_processors as $name=>$processors)
{
$value = trim($row[$name]);
if (strlen($value) == 0)
$value = Null;
$processors = listify($processors);
// Format the processors in (name => params) format,
// as not all processors have extra parameters defined.
foreach ($processors as $proc_name => $params)
{
if (! array_key_exists($proc_name, $this->processors))
{
$processors[$params] = [];
unset($processors[$proc_name]);
}
else
{
$processors[$proc_name] = listify($processors[$proc_name]);
}
}
foreach ($processors as $proc_name => $params)
{
array_unshift($params, $value);
$value = $this->process($proc_name, $params);
}
$cols_values[$name] = $value;
}
return $cols_values;
}
protected function validate_row($row)
{
// NOTE <Yavor>: CSV cells with no values for a column get removed from the
// $row array, instead of defaulting to a NULL value, as NULL may be
// a legitimate value.
$cols_values = array_filter($row);
$column_validators = array_where($this->column_mappings, function($k, $v)
{
return array_key_exists('validators', $v);
});
foreach ($column_validators as $k=>$v)
$column_validators[$k] = $v['validators'];
foreach ($column_validators as $name=>$validators)
{
$value = $row[$name];
$validators = listify($validators);
foreach ($validators as $validator_name)
{
$this->validate($validator_name, [$name, $row]);
}
}
}
}
?>