Differential D19302 Diff 46190 src/applications/files/document/render/PhabricatorDocumentRenderingEngine.php
Changeset View
Changeset View
Standalone View
Standalone View
src/applications/files/document/render/PhabricatorDocumentRenderingEngine.php
<?php | <?php | ||||
abstract class PhabricatorDocumentRenderingEngine | abstract class PhabricatorDocumentRenderingEngine | ||||
extends Phobject { | extends Phobject { | ||||
private $request; | private $request; | ||||
private $controller; | private $controller; | ||||
private $activeEngine; | private $activeEngine; | ||||
private $ref; | |||||
final public function setRequest(AphrontRequest $request) { | final public function setRequest(AphrontRequest $request) { | ||||
$this->request = $request; | $this->request = $request; | ||||
return $this; | return $this; | ||||
} | } | ||||
final public function getRequest() { | final public function getRequest() { | ||||
if (!$this->request) { | if (!$this->request) { | ||||
Show All 12 Lines | final public function getController() { | ||||
if (!$this->controller) { | if (!$this->controller) { | ||||
throw new PhutilInvalidStateException('setController'); | throw new PhutilInvalidStateException('setController'); | ||||
} | } | ||||
return $this->controller; | return $this->controller; | ||||
} | } | ||||
final protected function getActiveEngine() { | final protected function getActiveEngine() { | ||||
if (!$this->activeEngine) { | return $this->activeEngine; | ||||
throw new PhutilInvalidStateException('setActiveEngine'); | |||||
} | } | ||||
return $this->activeEngine; | final protected function getRef() { | ||||
return $this->ref; | |||||
} | } | ||||
final public function newDocumentView(PhabricatorDocumentRef $ref) { | final public function newDocumentView(PhabricatorDocumentRef $ref) { | ||||
$request = $this->getRequest(); | $request = $this->getRequest(); | ||||
$viewer = $request->getViewer(); | $viewer = $request->getViewer(); | ||||
$engines = PhabricatorDocumentEngine::getEnginesForRef($viewer, $ref); | $engines = PhabricatorDocumentEngine::getEnginesForRef($viewer, $ref); | ||||
▲ Show 20 Lines • Show All 118 Lines • ▼ Show 20 Lines | protected function getSelectedDocumentEngineKey() { | ||||
return $this->getRequest()->getURIData('engineKey'); | return $this->getRequest()->getURIData('engineKey'); | ||||
} | } | ||||
final public function newRenderResponse(PhabricatorDocumentRef $ref) { | final public function newRenderResponse(PhabricatorDocumentRef $ref) { | ||||
$request = $this->getRequest(); | $request = $this->getRequest(); | ||||
$viewer = $request->getViewer(); | $viewer = $request->getViewer(); | ||||
$engines = PhabricatorDocumentEngine::getEnginesForRef($viewer, $ref); | $engines = PhabricatorDocumentEngine::getEnginesForRef($viewer, $ref); | ||||
$engine_key = $request->getURIData('engineKey'); | $engine_key = $this->getSelectedDocumentEngineKey(); | ||||
if (!isset($engines[$engine_key])) { | if (!isset($engines[$engine_key])) { | ||||
return $this->newErrorResponse( | return $this->newErrorResponse( | ||||
$ref, | |||||
pht( | pht( | ||||
'The engine ("%s") is unknown, or unable to render this document.', | 'The engine ("%s") is unknown, or unable to render this document.', | ||||
$engine_key)); | $engine_key)); | ||||
} | } | ||||
$engine = $engines[$engine_key]; | $engine = $engines[$engine_key]; | ||||
$this->activeEngine = $engine; | $this->activeEngine = $engine; | ||||
$encode_setting = $request->getStr('encode'); | $encode_setting = $request->getStr('encode'); | ||||
if (strlen($encode_setting)) { | if (strlen($encode_setting)) { | ||||
$engine->setEncodingConfiguration($encode_setting); | $engine->setEncodingConfiguration($encode_setting); | ||||
} | } | ||||
$highlight_setting = $request->getStr('highlight'); | $highlight_setting = $request->getStr('highlight'); | ||||
if (strlen($highlight_setting)) { | if (strlen($highlight_setting)) { | ||||
$engine->setHighlightingConfiguration($highlight_setting); | $engine->setHighlightingConfiguration($highlight_setting); | ||||
} | } | ||||
try { | try { | ||||
$content = $engine->newDocument($ref); | $content = $engine->newDocument($ref); | ||||
} catch (Exception $ex) { | } catch (Exception $ex) { | ||||
return $this->newErrorResponse($ref, $ex->getMessage()); | return $this->newErrorResponse($ex->getMessage()); | ||||
} | } | ||||
return $this->newContentResponse($ref, $content); | return $this->newContentResponse($content); | ||||
} | } | ||||
private function newErrorResponse( | public function newErrorResponse($message) { | ||||
PhabricatorDocumentRef $ref, | |||||
$message) { | |||||
$container = phutil_tag( | $container = phutil_tag( | ||||
'div', | 'div', | ||||
array( | array( | ||||
'class' => 'document-engine-error', | 'class' => 'document-engine-error', | ||||
), | ), | ||||
array( | array( | ||||
id(new PHUIIconView()) | id(new PHUIIconView()) | ||||
->setIcon('fa-exclamation-triangle red'), | ->setIcon('fa-exclamation-triangle red'), | ||||
' ', | ' ', | ||||
$message, | $message, | ||||
)); | )); | ||||
return $this->newContentResponse($ref, $container); | return $this->newContentResponse($container); | ||||
} | } | ||||
private function newContentResponse( | private function newContentResponse($content) { | ||||
PhabricatorDocumentRef $ref, | |||||
$content) { | |||||
$request = $this->getRequest(); | $request = $this->getRequest(); | ||||
$viewer = $request->getViewer(); | $viewer = $request->getViewer(); | ||||
$controller = $this->getController(); | $controller = $this->getController(); | ||||
if ($request->isAjax()) { | if ($request->isAjax()) { | ||||
return id(new AphrontAjaxResponse()) | return id(new AphrontAjaxResponse()) | ||||
->setContent( | ->setContent( | ||||
array( | array( | ||||
'markup' => hsprintf('%s', $content), | 'markup' => hsprintf('%s', $content), | ||||
)); | )); | ||||
} | } | ||||
$crumbs = $this->newCrumbs($ref); | $crumbs = $this->newCrumbs(); | ||||
if ($crumbs) { | |||||
$crumbs->setBorder(true); | $crumbs->setBorder(true); | ||||
} | |||||
$content_frame = id(new PHUIObjectBoxView()) | $content_frame = id(new PHUIObjectBoxView()) | ||||
->setBackground(PHUIObjectBoxView::BLUE_PROPERTY) | ->setBackground(PHUIObjectBoxView::BLUE_PROPERTY) | ||||
->appendChild($content); | ->appendChild($content); | ||||
$page_frame = id(new PHUITwoColumnView()) | $page_frame = id(new PHUITwoColumnView()) | ||||
->setFooter($content_frame); | ->setFooter($content_frame); | ||||
return $controller->newPage() | $title = array(); | ||||
->setCrumbs($crumbs) | $ref = $this->getRef(); | ||||
->setTitle( | if ($ref) { | ||||
array( | $title = array( | ||||
$ref->getName(), | $ref->getName(), | ||||
pht('Standalone'), | pht('Standalone'), | ||||
)) | ); | ||||
} else { | |||||
$title = pht('Document'); | |||||
} | |||||
return $controller->newPage() | |||||
->setCrumbs($crumbs) | |||||
->setTitle($title) | |||||
->appendChild($page_frame); | ->appendChild($page_frame); | ||||
} | } | ||||
protected function newCrumbs(PhabricatorDocumentRef $ref) { | protected function newCrumbs() { | ||||
$engine = $this->getActiveEngine(); | |||||
$controller = $this->getController(); | $controller = $this->getController(); | ||||
$crumbs = $controller->buildApplicationCrumbsForEditEngine(); | $crumbs = $controller->buildApplicationCrumbsForEditEngine(); | ||||
$this->addApplicationCrumbs($ref, $crumbs); | $ref = $this->getRef(); | ||||
$engine = $this->getActiveEngine(); | $this->addApplicationCrumbs($crumbs, $ref); | ||||
if ($ref) { | |||||
$label = $engine->getViewAsLabel($ref); | $label = $engine->getViewAsLabel($ref); | ||||
if ($label) { | if ($label) { | ||||
$crumbs->addTextCrumb($label); | $crumbs->addTextCrumb($label); | ||||
} | } | ||||
} | |||||
return $crumbs; | return $crumbs; | ||||
} | } | ||||
protected function addApplicationCrumbs( | protected function addApplicationCrumbs( | ||||
PhabricatorDocumentRef $ref, | PHUICrumbsView $crumbs, | ||||
PHUICrumbsView $crumbs) { | PhabricatorDocumentRef $ref = null) { | ||||
return; | return; | ||||
} | } | ||||
} | } |