@@ -12,10 +12,10 @@ discard block |
||
12 | 12 | */ |
13 | 13 | class Migrations extends \Module { |
14 | 14 | |
15 | - public $ids = []; |
|
16 | - public $migrationObjects = []; |
|
15 | + public $ids = []; |
|
16 | + public $migrationObjects = []; |
|
17 | 17 | |
18 | - public function startMigration($migrationId, $mapId, $filePath) { |
|
18 | + public function startMigration($migrationId, $mapId, $filePath) { |
|
19 | 19 | $log = new \Migrations\Log(); |
20 | 20 | $log->migration_id = $migrationId; |
21 | 21 | $log->migration_map_id = $mapId; |
@@ -24,11 +24,11 @@ discard block |
||
24 | 24 | |
25 | 25 | $reader = new Migrations\Reader\Xml(); |
26 | 26 | if (!$reader->loadData($filePath)) { |
27 | - $event = new Migrations\Log\Event(); |
|
28 | - $event->log_id = $log->id; |
|
29 | - $event->type = 'load_data_error'; |
|
30 | - $event->save(); |
|
31 | - return false; |
|
27 | + $event = new Migrations\Log\Event(); |
|
28 | + $event->log_id = $log->id; |
|
29 | + $event->type = 'load_data_error'; |
|
30 | + $event->save(); |
|
31 | + return false; |
|
32 | 32 | } |
33 | 33 | $walker = new \Migrations\Walker(); |
34 | 34 | $walker->migration = Migrations\Migration::get($migrationId); |
@@ -38,43 +38,43 @@ discard block |
||
38 | 38 | $walker->walk(); |
39 | 39 | $log->result = 'success'; |
40 | 40 | $log->save(); |
41 | - } |
|
41 | + } |
|
42 | 42 | |
43 | - public function loadParseIds($type) { |
|
43 | + public function loadParseIds($type) { |
|
44 | 44 | $this->ids['parseIds'][$type] = \Migrations\Id::getList(['where' => ['type', $type], 'key' => 'parse_id']); |
45 | - } |
|
45 | + } |
|
46 | 46 | |
47 | - public function loadObjectIds($type) { |
|
47 | + public function loadObjectIds($type) { |
|
48 | 48 | $this->ids['objectIds'][$type] = \Migrations\Id::getList(['where' => ['type', $type], 'key' => 'object_id']); |
49 | - } |
|
49 | + } |
|
50 | 50 | |
51 | - public function findObject($parseId, $type) { |
|
51 | + public function findObject($parseId, $type) { |
|
52 | 52 | if (empty($this->ids['parseIds'][$type])) { |
53 | - $this->loadParseIds($type); |
|
54 | - ksort($this->ids['parseIds'][$type]); |
|
53 | + $this->loadParseIds($type); |
|
54 | + ksort($this->ids['parseIds'][$type]); |
|
55 | 55 | } |
56 | 56 | if (!empty($this->ids['parseIds'][$type][$parseId])) { |
57 | - return $this->ids['parseIds'][$type][$parseId]; |
|
57 | + return $this->ids['parseIds'][$type][$parseId]; |
|
58 | + } |
|
58 | 59 | } |
59 | - } |
|
60 | 60 | |
61 | - public function findParse($objectId, $type) { |
|
61 | + public function findParse($objectId, $type) { |
|
62 | 62 | if (empty($this->ids['objectIds'][$type])) { |
63 | - $this->loadObjectIds($type); |
|
64 | - ksort($this->ids['objectIds'][$type]); |
|
63 | + $this->loadObjectIds($type); |
|
64 | + ksort($this->ids['objectIds'][$type]); |
|
65 | 65 | } |
66 | 66 | if (!empty($this->ids['objectIds'][$type][$objectId])) { |
67 | - return $this->ids['objectIds'][$type][$objectId]; |
|
67 | + return $this->ids['objectIds'][$type][$objectId]; |
|
68 | + } |
|
68 | 69 | } |
69 | - } |
|
70 | 70 | |
71 | - public function getMigrationObject($objectId) { |
|
71 | + public function getMigrationObject($objectId) { |
|
72 | 72 | if (empty($this->migrationObjects)) { |
73 | - $this->migrationObjects = \Migrations\Migration\Object::getList(); |
|
73 | + $this->migrationObjects = \Migrations\Migration\Object::getList(); |
|
74 | 74 | } |
75 | 75 | if (!empty($this->migrationObjects[$objectId])) { |
76 | - return $this->migrationObjects[$objectId]; |
|
76 | + return $this->migrationObjects[$objectId]; |
|
77 | + } |
|
77 | 78 | } |
78 | - } |
|
79 | 79 | |
80 | 80 | } |
@@ -13,61 +13,61 @@ discard block |
||
13 | 13 | |
14 | 14 | class Walker { |
15 | 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; |
|
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 | 23 | |
24 | - //walk map pathes on cur path |
|
25 | - public function walk() { |
|
24 | + //walk map pathes on cur path |
|
25 | + public function walk() { |
|
26 | 26 | $walked = []; |
27 | 27 | //walk know pathes |
28 | 28 | foreach ($this->map->paths(['where' => ['path', $this->curPath]]) as $path) { |
29 | - if (isset($this->data[$path->item])) { |
|
29 | + if (isset($this->data[$path->item])) { |
|
30 | 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(); |
|
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 | 40 | } elseif ($path->type == 'object') { //start parse path data |
41 | - $this->startObjectParse($path->object_id, $this->data[$path->item]); |
|
41 | + $this->startObjectParse($path->object_id, $this->data[$path->item]); |
|
42 | 42 | } |
43 | - } |
|
44 | - $walked[$path->item] = true; |
|
43 | + } |
|
44 | + $walked[$path->item] = true; |
|
45 | 45 | } |
46 | 46 | //check unparsed paths |
47 | 47 | foreach ($this->data as $key => &$data) { |
48 | - //skip parsed and attribtes |
|
49 | - if ($key == '@attributes' || !empty($walked[$key])) { |
|
48 | + //skip parsed and attribtes |
|
49 | + if ($key == '@attributes' || !empty($walked[$key])) { |
|
50 | 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 |
|
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 | 58 | $this->startObjectParse($object, $data); |
59 | - } else { //create new map path for configure unknown path |
|
59 | + } else { //create new map path for configure unknown path |
|
60 | 60 | $this->mapPath = new Migration\Map\Path(); |
61 | 61 | $this->mapPath->parent_id = $this->mapPathParent ? $this->mapPathParent->id : 0; |
62 | 62 | $this->mapPath->path = $this->curPath; |
63 | 63 | $this->mapPath->item = $key; |
64 | 64 | $this->mapPath->migration_map_id = $this->map->id; |
65 | 65 | $this->mapPath->save(); |
66 | - } |
|
66 | + } |
|
67 | + } |
|
67 | 68 | } |
68 | - } |
|
69 | 69 | |
70 | - private function startObjectParse($object_id, &$data) { |
|
70 | + private function startObjectParse($object_id, &$data) { |
|
71 | 71 | $objectParser = new Parser\Object(); |
72 | 72 | $objectParser->object = is_object($object_id) ? $object_id : \App::$cur->migrations->getMigrationObject($object_id); |
73 | 73 | $objectParser->data = $data; |
@@ -76,33 +76,33 @@ discard block |
||
76 | 76 | |
77 | 77 | if ($objectParser->object->clear && json_decode($objectParser->object->clear, true)) { |
78 | 78 | |
79 | - $where = json_decode($objectParser->object->clear, true); |
|
80 | - if (!$where) { |
|
79 | + $where = json_decode($objectParser->object->clear, true); |
|
80 | + if (!$where) { |
|
81 | 81 | $where = []; |
82 | - } else { |
|
82 | + } else { |
|
83 | 83 | $where = [[$where]]; |
84 | - } |
|
85 | - if ($ids) { |
|
84 | + } |
|
85 | + if ($ids) { |
|
86 | 86 | $where[] = ['id', implode(',', $ids), 'NOT IN']; |
87 | - } |
|
88 | - if (empty(\App::$cur->migrations->ids['objectIds'])) { |
|
87 | + } |
|
88 | + if (empty(\App::$cur->migrations->ids['objectIds'])) { |
|
89 | 89 | \App::$cur->migrations->loadObjectIds($objectParser->object->model); |
90 | - } |
|
91 | - if (!empty(\App::$cur->migrations->ids['objectIds'][$objectParser->object->model])) { |
|
90 | + } |
|
91 | + if (!empty(\App::$cur->migrations->ids['objectIds'][$objectParser->object->model])) { |
|
92 | 92 | $where[] = ['id', implode(',', array_keys(\App::$cur->migrations->ids['objectIds'][$objectParser->object->model])), 'IN']; |
93 | - } |
|
94 | - $modelName = $objectParser->object->model; |
|
95 | - $objects = $modelName::getList(['where' => $where]); |
|
96 | - foreach ($objects as $object) { |
|
93 | + } |
|
94 | + $modelName = $objectParser->object->model; |
|
95 | + $objects = $modelName::getList(['where' => $where]); |
|
96 | + foreach ($objects as $object) { |
|
97 | 97 | $objectId = \App::$cur->migrations->findParse($object->id, $objectParser->object->model); |
98 | 98 | if ($objectId) { |
99 | - $objectId->delete(); |
|
100 | - unset(\App::$cur->migrations->ids['objectIds'][$objectParser->object->model][$object->id]); |
|
101 | - unset(\App::$cur->migrations->ids['parseIds'][$objectParser->object->model][$objectId->parse_id]); |
|
99 | + $objectId->delete(); |
|
100 | + unset(\App::$cur->migrations->ids['objectIds'][$objectParser->object->model][$object->id]); |
|
101 | + unset(\App::$cur->migrations->ids['parseIds'][$objectParser->object->model][$objectId->parse_id]); |
|
102 | 102 | } |
103 | 103 | $object->delete(); |
104 | - } |
|
104 | + } |
|
105 | + } |
|
105 | 106 | } |
106 | - } |
|
107 | 107 | |
108 | 108 | } |