Changeset View
Changeset View
Standalone View
Standalone View
src/applications/differential/storage/DifferentialModernHunk.php
Show All 9 Lines | final class DifferentialModernHunk extends DifferentialHunk { | ||||
protected $dataType; | protected $dataType; | ||||
protected $dataEncoding; | protected $dataEncoding; | ||||
protected $dataFormat; | protected $dataFormat; | ||||
protected $data; | protected $data; | ||||
private $rawData; | private $rawData; | ||||
private $forcedEncoding; | private $forcedEncoding; | ||||
private $fileData; | |||||
public function getTableName() { | public function getTableName() { | ||||
return 'differential_hunk_modern'; | return 'differential_hunk_modern'; | ||||
} | } | ||||
protected function getConfiguration() { | protected function getConfiguration() { | ||||
return array( | return array( | ||||
self::CONFIG_BINARY => array( | self::CONFIG_BINARY => array( | ||||
▲ Show 20 Lines • Show All 56 Lines • ▼ Show 20 Lines | if ($type == self::DATATYPE_TEXT) { | ||||
$this->dataFormat = self::DATAFORMAT_DEFLATED; | $this->dataFormat = self::DATAFORMAT_DEFLATED; | ||||
} | } | ||||
} | } | ||||
} | } | ||||
return parent::save(); | return parent::save(); | ||||
} | } | ||||
public function saveAsText() { | |||||
$old_type = $this->getDataType(); | |||||
$old_data = $this->getData(); | |||||
if ($old_type == self::DATATYPE_TEXT) { | |||||
return $this; | |||||
} | |||||
$raw_data = $this->getRawData(); | |||||
$this->setDataType(self::DATATYPE_TEXT); | |||||
$this->setData($raw_data); | |||||
$result = $this->save(); | |||||
$this->destroyData($old_type, $old_data); | |||||
return $result; | |||||
} | |||||
public function saveAsFile() { | |||||
$old_type = $this->getDataType(); | |||||
$old_data = $this->getData(); | |||||
if ($old_type == self::DATATYPE_FILE) { | |||||
return $this; | |||||
} | |||||
$raw_data = $this->getRawData(); | |||||
$file = PhabricatorFile::newFromFileData( | |||||
$raw_data, | |||||
array( | |||||
'name' => 'differential-hunk', | |||||
'mime-type' => 'application/octet-stream', | |||||
'viewPolicy' => PhabricatorPolicies::POLICY_NOONE, | |||||
)); | |||||
$this->setDataType(self::DATATYPE_FILE); | |||||
$this->setData($file->getPHID()); | |||||
// NOTE: Because hunks don't have a PHID and we just load hunk data with | |||||
// the ominipotent viewer, we do not need to attach the file to anything. | |||||
$result = $this->save(); | |||||
$this->destroyData($old_type, $old_data); | |||||
return $result; | |||||
} | |||||
private function getRawData() { | private function getRawData() { | ||||
if ($this->rawData === null) { | if ($this->rawData === null) { | ||||
$type = $this->getDataType(); | $type = $this->getDataType(); | ||||
$data = $this->getData(); | $data = $this->getData(); | ||||
switch ($type) { | switch ($type) { | ||||
case self::DATATYPE_TEXT: | case self::DATATYPE_TEXT: | ||||
// In this storage type, the changes are stored on the object. | // In this storage type, the changes are stored on the object. | ||||
$data = $data; | $data = $data; | ||||
break; | break; | ||||
case self::DATATYPE_FILE: | case self::DATATYPE_FILE: | ||||
$data = $this->loadFileData(); | |||||
break; | |||||
default: | default: | ||||
throw new Exception( | throw new Exception( | ||||
pht('Hunk has unsupported data type "%s"!', $type)); | pht('Hunk has unsupported data type "%s"!', $type)); | ||||
} | } | ||||
$format = $this->getDataFormat(); | $format = $this->getDataFormat(); | ||||
switch ($format) { | switch ($format) { | ||||
case self::DATAFORMAT_RAW: | case self::DATAFORMAT_RAW: | ||||
Show All 9 Lines | if ($this->rawData === null) { | ||||
} | } | ||||
$this->rawData = $data; | $this->rawData = $data; | ||||
} | } | ||||
return $this->rawData; | return $this->rawData; | ||||
} | } | ||||
private function loadFileData() { | |||||
if ($this->fileData === null) { | |||||
$type = $this->getDataType(); | |||||
if ($type !== self::DATATYPE_FILE) { | |||||
throw new Exception( | |||||
pht( | |||||
'Unable to load file data for hunk with wrong data type ("%s").', | |||||
$type)); | |||||
} | |||||
$file_phid = $this->getData(); | |||||
$file = $this->loadRawFile($file_phid); | |||||
$data = $file->loadFileData(); | |||||
$this->fileData = $data; | |||||
} | |||||
return $this->fileData; | |||||
} | |||||
private function loadRawFile($file_phid) { | |||||
$viewer = PhabricatorUser::getOmnipotentUser(); | |||||
$files = id(new PhabricatorFileQuery()) | |||||
->setViewer($viewer) | |||||
->withPHIDs(array($file_phid)) | |||||
->execute(); | |||||
if (!$files) { | |||||
throw new Exception( | |||||
pht( | |||||
'Failed to load file ("%s") with hunk data.', | |||||
$file_phid)); | |||||
} | |||||
$file = head($files); | |||||
return $file; | |||||
} | |||||
public function destroyObjectPermanently( | |||||
PhabricatorDestructionEngine $engine) { | |||||
$type = $this->getDataType(); | |||||
$data = $this->getData(); | |||||
$this->destroyData($type, $data, $engine); | |||||
return parent::destroyObjectPermanently($engine); | |||||
} | |||||
private function destroyData( | |||||
$type, | |||||
$data, | |||||
PhabricatorDestructionEngine $engine = null) { | |||||
if (!$engine) { | |||||
$engine = new PhabricatorDestructionEngine(); | |||||
} | |||||
switch ($type) { | |||||
case self::DATATYPE_FILE: | |||||
$file = $this->loadRawFile($data); | |||||
$engine->destroyObject($file); | |||||
break; | |||||
} | |||||
} | |||||
} | } |