|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Data tree walker |
|
5
|
|
|
* |
|
6
|
|
|
* @author Alexey Krupskiy <[email protected]> |
|
7
|
|
|
* @link http://inji.ru/ |
|
8
|
|
|
* @copyright 2015 Alexey Krupskiy |
|
9
|
|
|
* @license https://github.com/injitools/cms-Inji/blob/master/LICENSE |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Migrations; |
|
13
|
|
|
|
|
14
|
|
|
class Walker { |
|
15
|
|
|
|
|
16
|
|
|
public $migration = null; |
|
17
|
|
|
public $map = null; |
|
18
|
|
|
public $data = null; |
|
19
|
|
|
public $mapPath = null; |
|
20
|
|
|
public $mapPathParent = null; |
|
21
|
|
|
public $curPath = '/'; |
|
22
|
|
|
public $migtarionLog = null; |
|
23
|
|
|
|
|
24
|
|
|
//walk map pathes on cur path |
|
25
|
|
|
public function walk() { |
|
26
|
|
|
$walked = []; |
|
27
|
|
|
//walk know pathes |
|
28
|
|
|
foreach ($this->map->paths(['where' => ['path', $this->curPath]]) as $path) { |
|
29
|
|
|
if (isset($this->data[$path->item])) { |
|
30
|
|
|
if ($path->type == 'container') { //create walker for container |
|
31
|
|
|
$walker = new Walker(); |
|
32
|
|
|
$walker->migration = $this->migration; |
|
33
|
|
|
$walker->map = $this->map; |
|
34
|
|
|
$walker->data = &$this->data[$path->item]; |
|
35
|
|
|
$walker->curPath = $this->curPath . $path->item . '/'; |
|
36
|
|
|
$walker->mapPath = $path; |
|
37
|
|
|
$walker->mapPathParent = $this->mapPath; |
|
38
|
|
|
$walker->migtarionLog = $this->migtarionLog; |
|
39
|
|
|
$walker->walk(); |
|
40
|
|
|
} elseif ($path->type == 'object') { //start parse path data |
|
41
|
|
|
$this->startObjectParse($path->object_id, $this->data[$path->item]); |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
$walked[$path->item] = true; |
|
45
|
|
|
} |
|
46
|
|
|
//check unparsed paths |
|
47
|
|
|
foreach ($this->data as $key => &$data) { |
|
48
|
|
|
//skip parsed and attribtes |
|
49
|
|
|
if ($key == '@attributes' || !empty($walked[$key])) { |
|
50
|
|
|
continue; |
|
51
|
|
|
} |
|
52
|
|
|
//search object for parse |
|
53
|
|
|
$object = Migration\Object::get([ |
|
54
|
|
|
['code', $key], |
|
55
|
|
|
['migration_id', $this->migration->id] |
|
56
|
|
|
]); |
|
57
|
|
|
if ($object) { //parse as object |
|
58
|
|
|
$this->startObjectParse($object, $data); |
|
59
|
|
|
} else { //create new map path for configure unknown path |
|
60
|
|
|
$this->mapPath = new Migration\Map\Path(); |
|
61
|
|
|
$this->mapPath->parent_id = $this->mapPathParent ? $this->mapPathParent->id : 0; |
|
62
|
|
|
$this->mapPath->path = $this->curPath; |
|
63
|
|
|
$this->mapPath->item = $key; |
|
64
|
|
|
$this->mapPath->migration_map_id = $this->map->id; |
|
65
|
|
|
$this->mapPath->save(); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
private function startObjectParse($object_id, &$data) { |
|
71
|
|
|
$objectParser = new Parser\Object(); |
|
72
|
|
|
$objectParser->object = is_object($object_id) ? $object_id : \App::$cur->migrations->getMigrationObject($object_id); |
|
73
|
|
|
$objectParser->data = $data; |
|
74
|
|
|
$objectParser->walker = $this; |
|
75
|
|
|
$ids = $objectParser->parse(); |
|
76
|
|
|
|
|
77
|
|
|
if ($objectParser->object->clear && json_decode($objectParser->object->clear, true)) { |
|
78
|
|
|
|
|
79
|
|
|
$where = json_decode($objectParser->object->clear, true); |
|
80
|
|
|
if (!$where) { |
|
81
|
|
|
$where = []; |
|
82
|
|
|
} else { |
|
83
|
|
|
$where = [[$where]]; |
|
84
|
|
|
} |
|
85
|
|
|
if ($ids) { |
|
86
|
|
|
$where[] = ['id', implode(',', $ids), 'NOT IN']; |
|
87
|
|
|
if (!empty(\App::$cur->migrations->ids['objectIds'][$objectParser->object->model])) { |
|
88
|
|
|
$where[] = ['id', implode(',', array_keys(\App::$cur->migrations->ids['objectIds'][$objectParser->object->model])), 'IN']; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
$modelName = $objectParser->object->model; |
|
92
|
|
|
$objects = $modelName::getList(['where' => $where]); |
|
93
|
|
|
foreach ($objects as $object) { |
|
94
|
|
|
$objectId = \App::$cur->migrations->findParse($object->id, $objectParser->object->model); |
|
95
|
|
|
if ($objectId) { |
|
96
|
|
|
$objectId->delete(); |
|
97
|
|
|
unset(\App::$cur->migrations->ids['objectIds'][$objectParser->object->model][$object->id]); |
|
98
|
|
|
unset(\App::$cur->migrations->ids['parseIds'][$objectParser->object->model][$objectId->parse_id]); |
|
99
|
|
|
} |
|
100
|
|
|
$object->delete(); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
} |
|
106
|
|
|
|