Path: blob/master/src/infrastructure/markup/markuprule/PhutilRemarkupEvalRule.php
12241 views
<?php12final class PhutilRemarkupEvalRule extends PhutilRemarkupRule {34const KEY_EVAL = 'eval';56public function getPriority() {7return 50;8}910public function apply($text) {11return preg_replace_callback(12'/\${{{(.+?)}}}/',13array($this, 'newExpressionToken'),14$text);15}1617public function newExpressionToken(array $matches) {18$expression = $matches[1];1920if (!$this->isFlatText($expression)) {21return $matches[0];22}2324$engine = $this->getEngine();25$token = $engine->storeText($expression);2627$list_key = self::KEY_EVAL;28$expression_list = $engine->getTextMetadata($list_key, array());2930$expression_list[] = array(31'token' => $token,32'expression' => $expression,33'original' => $matches[0],34);3536$engine->setTextMetadata($list_key, $expression_list);3738return $token;39}4041public function didMarkupText() {42$engine = $this->getEngine();4344$list_key = self::KEY_EVAL;45$expression_list = $engine->getTextMetadata($list_key, array());4647foreach ($expression_list as $expression_item) {48$token = $expression_item['token'];49$expression = $expression_item['expression'];5051$result = $this->evaluateExpression($expression);5253if ($result === null) {54$result = $expression_item['original'];55}5657$engine->overwriteStoredText($token, $result);58}59}6061private function evaluateExpression($expression) {62static $string_map;6364if ($string_map === null) {65$string_map = array(66'strings' => array(67'platform' => array(68'server' => array(69'name' => PlatformSymbols::getPlatformServerName(),70'path' => pht('phabricator/'),71),72'client' => array(73'name' => PlatformSymbols::getPlatformClientName(),74'path' => pht('arcanist/'),75),76),77),78);79}8081$parts = explode('.', $expression);8283$cursor = $string_map;84foreach ($parts as $part) {85if (isset($cursor[$part])) {86$cursor = $cursor[$part];87} else {88break;89}90}9192if (is_string($cursor)) {93return $cursor;94}9596return null;97}9899}100101102