| 1: | <?php |
| 2: | |
| 3: | declare(strict_types=1); |
| 4: | |
| 5: | namespace Atk4\Data\Util; |
| 6: | |
| 7: | use Atk4\Core\DebugTrait; |
| 8: | use Atk4\Data\Model; |
| 9: | use Atk4\Data\Reference\HasMany; |
| 10: | use Atk4\Data\Reference\HasOne; |
| 11: | |
| 12: | |
| 13: | |
| 14: | |
| 15: | |
| 16: | |
| 17: | |
| 18: | |
| 19: | |
| 20: | |
| 21: | class DeepCopy |
| 22: | { |
| 23: | use DebugTrait; |
| 24: | |
| 25: | public const HOOK_AFTER_COPY = self::class . '@afterCopy'; |
| 26: | |
| 27: | |
| 28: | protected Model $source; |
| 29: | |
| 30: | |
| 31: | protected Model $destination; |
| 32: | |
| 33: | |
| 34: | |
| 35: | |
| 36: | |
| 37: | |
| 38: | |
| 39: | protected $references = []; |
| 40: | |
| 41: | |
| 42: | |
| 43: | |
| 44: | |
| 45: | |
| 46: | |
| 47: | protected $exclusions = []; |
| 48: | |
| 49: | |
| 50: | |
| 51: | |
| 52: | |
| 53: | |
| 54: | |
| 55: | |
| 56: | |
| 57: | |
| 58: | protected $transforms = []; |
| 59: | |
| 60: | |
| 61: | |
| 62: | |
| 63: | |
| 64: | |
| 65: | public $mapping = []; |
| 66: | |
| 67: | |
| 68: | |
| 69: | |
| 70: | |
| 71: | |
| 72: | public function from(Model $source) |
| 73: | { |
| 74: | $this->source = $source; |
| 75: | |
| 76: | return $this; |
| 77: | } |
| 78: | |
| 79: | |
| 80: | |
| 81: | |
| 82: | |
| 83: | |
| 84: | public function to(Model $destination) |
| 85: | { |
| 86: | $this->destination = $destination; |
| 87: | |
| 88: | if (!$this->destination->issetPersistence()) { |
| 89: | $this->destination->setPersistence($this->source->getModel()->getPersistence()); |
| 90: | } |
| 91: | |
| 92: | return $this; |
| 93: | } |
| 94: | |
| 95: | |
| 96: | |
| 97: | |
| 98: | |
| 99: | |
| 100: | |
| 101: | |
| 102: | public function with(array $references) |
| 103: | { |
| 104: | $this->references = $references; |
| 105: | |
| 106: | return $this; |
| 107: | } |
| 108: | |
| 109: | |
| 110: | |
| 111: | |
| 112: | |
| 113: | |
| 114: | |
| 115: | |
| 116: | |
| 117: | |
| 118: | public function excluding(array $exclusions) |
| 119: | { |
| 120: | $this->exclusions = $exclusions; |
| 121: | |
| 122: | return $this; |
| 123: | } |
| 124: | |
| 125: | |
| 126: | |
| 127: | |
| 128: | |
| 129: | |
| 130: | |
| 131: | |
| 132: | |
| 133: | |
| 134: | |
| 135: | |
| 136: | |
| 137: | |
| 138: | |
| 139: | |
| 140: | |
| 141: | |
| 142: | |
| 143: | |
| 144: | |
| 145: | |
| 146: | public function transformData(array $transforms) |
| 147: | { |
| 148: | $this->transforms = $transforms; |
| 149: | |
| 150: | return $this; |
| 151: | } |
| 152: | |
| 153: | |
| 154: | |
| 155: | |
| 156: | |
| 157: | |
| 158: | |
| 159: | |
| 160: | protected function extractKeys(array $array): array |
| 161: | { |
| 162: | $result = []; |
| 163: | foreach ($array as $key => $val) { |
| 164: | if (is_int($key)) { |
| 165: | $result[$val] = []; |
| 166: | } else { |
| 167: | $result[$key] = $val; |
| 168: | } |
| 169: | } |
| 170: | |
| 171: | return $result; |
| 172: | } |
| 173: | |
| 174: | |
| 175: | |
| 176: | |
| 177: | public function copy(): Model |
| 178: | { |
| 179: | return $this->destination->atomic(function () { |
| 180: | return $this->_copy( |
| 181: | $this->source, |
| 182: | $this->destination, |
| 183: | $this->references, |
| 184: | $this->exclusions, |
| 185: | $this->transforms |
| 186: | )->reload(); |
| 187: | }); |
| 188: | } |
| 189: | |
| 190: | |
| 191: | |
| 192: | |
| 193: | |
| 194: | |
| 195: | |
| 196: | |
| 197: | |
| 198: | |
| 199: | protected function _copy(Model $source, Model $destination, array $references, array $exclusions, array $transforms): Model |
| 200: | { |
| 201: | try { |
| 202: | |
| 203: | if (isset($this->mapping[$source->table]) && isset($this->mapping[$source->table][$source->getId()])) { |
| 204: | $this->debug('Skipping ' . get_class($source)); |
| 205: | |
| 206: | $destination = $destination->load($this->mapping[$source->table][$source->getId()]); |
| 207: | } else { |
| 208: | $this->debug('Copying ' . get_class($source)); |
| 209: | |
| 210: | $data = $source->get(); |
| 211: | |
| 212: | |
| 213: | |
| 214: | foreach ($this->extractKeys($exclusions) as $key => $val) { |
| 215: | unset($data[$key]); |
| 216: | } |
| 217: | |
| 218: | |
| 219: | |
| 220: | if (isset($transforms[0])) { |
| 221: | $data = $transforms[0]($data); |
| 222: | } |
| 223: | |
| 224: | |
| 225: | |
| 226: | |
| 227: | |
| 228: | unset($data[$source->idField]); |
| 229: | |
| 230: | |
| 231: | $destination = $destination->createEntity(); |
| 232: | foreach ($data as $key => $val) { |
| 233: | if ($destination->hasField($key) && $destination->getField($key)->isEditable()) { |
| 234: | $destination->set($key, $val); |
| 235: | } |
| 236: | } |
| 237: | } |
| 238: | $destination->hook(self::HOOK_AFTER_COPY, [$source]); |
| 239: | |
| 240: | |
| 241: | foreach ($this->extractKeys($references) as $refKey => $refVal) { |
| 242: | $this->debug('Considering ' . $refKey); |
| 243: | |
| 244: | if ($source->hasReference($refKey) && $source->getModel(true)->getReference($refKey) instanceof HasOne) { |
| 245: | $this->debug('Proceeding with ' . $refKey); |
| 246: | |
| 247: | |
| 248: | $sourceTable = $source->refModel($refKey)->table; |
| 249: | |
| 250: | if (isset($this->mapping[$sourceTable]) |
| 251: | && array_key_exists($source->get($refKey), $this->mapping[$sourceTable]) |
| 252: | ) { |
| 253: | |
| 254: | $destination->set($refKey, $this->mapping[$sourceTable][$source->get($refKey)]); |
| 255: | $this->debug(' already copied ' . $source->get($refKey) . ' as ' . $destination->get($refKey)); |
| 256: | } else { |
| 257: | |
| 258: | $this->debug('Value is ' . $source->get($refKey)); |
| 259: | if (!$source->get($refKey)) { |
| 260: | $destination->set($refKey, $source->get($refKey)); |
| 261: | |
| 262: | continue; |
| 263: | } |
| 264: | |
| 265: | |
| 266: | try { |
| 267: | $destination->set( |
| 268: | $refKey, |
| 269: | $this->_copy( |
| 270: | $source->ref($refKey), |
| 271: | $destination->refModel($refKey), |
| 272: | $refVal, |
| 273: | $exclusions[$refKey] ?? [], |
| 274: | $transforms[$refKey] ?? [] |
| 275: | )->getId() |
| 276: | ); |
| 277: | $this->debug(' ... mapped into ' . $destination->get($refKey)); |
| 278: | } catch (DeepCopyException $e) { |
| 279: | $this->debug('escalating a problem from ' . $refKey); |
| 280: | |
| 281: | throw $e->addDepth($refKey); |
| 282: | } |
| 283: | } |
| 284: | } |
| 285: | } |
| 286: | |
| 287: | |
| 288: | $destination->save(); |
| 289: | |
| 290: | |
| 291: | $this->mapping[$source->table][$source->getId()] = $destination->getId(); |
| 292: | $this->debug(' .. copied ' . get_class($source) . ' ' . $source->getId() . ' ' . $destination->getId()); |
| 293: | |
| 294: | |
| 295: | |
| 296: | foreach ($this->extractKeys($references) as $refKey => $refVal) { |
| 297: | if ($source->hasReference($refKey) && $source->getModel(true)->getReference($refKey) instanceof HasMany) { |
| 298: | |
| 299: | foreach ($source->ref($refKey) as $refModel) { |
| 300: | $this->_copy( |
| 301: | $refModel, |
| 302: | $destination->ref($refKey), |
| 303: | $refVal, |
| 304: | $exclusions[$refKey] ?? [], |
| 305: | $transforms[$refKey] ?? [] |
| 306: | ); |
| 307: | } |
| 308: | } |
| 309: | } |
| 310: | |
| 311: | return $destination; |
| 312: | } catch (DeepCopyException $e) { |
| 313: | throw $e; |
| 314: | } catch (\Exception $e) { |
| 315: | $this->debug('model copy failed'); |
| 316: | |
| 317: | throw (new DeepCopyException('Model copy failed', 0, $e)) |
| 318: | ->addMoreInfo('source', $source) |
| 319: | ->addMoreInfo('source_info', $source->__debugInfo()) |
| 320: | ->addMoreInfo('source_data', $source->get()) |
| 321: | ->addMoreInfo('destination', $destination) |
| 322: | ->addMoreInfo('destination_info', $destination->__debugInfo()); |
| 323: | } |
| 324: | } |
| 325: | } |
| 326: | |