@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.)
hq.recaptime.dev/wiki/Phorge
phorge
phabricator
1<?php
2
3final class PhutilRemarkupEvalRule extends PhutilRemarkupRule {
4
5 const KEY_EVAL = 'eval';
6
7 public function getPriority() {
8 return 50;
9 }
10
11 public function apply($text) {
12 return preg_replace_callback(
13 '/\${{{(.+?)}}}/',
14 array($this, 'newExpressionToken'),
15 $text);
16 }
17
18 public function newExpressionToken(array $matches) {
19 $expression = $matches[1];
20
21 if (!$this->isFlatText($expression)) {
22 return $matches[0];
23 }
24
25 $engine = $this->getEngine();
26 $token = $engine->storeText($expression);
27
28 $list_key = self::KEY_EVAL;
29 $expression_list = $engine->getTextMetadata($list_key, array());
30
31 $expression_list[] = array(
32 'token' => $token,
33 'expression' => $expression,
34 'original' => $matches[0],
35 );
36
37 $engine->setTextMetadata($list_key, $expression_list);
38
39 return $token;
40 }
41
42 public function didMarkupText() {
43 $engine = $this->getEngine();
44
45 $list_key = self::KEY_EVAL;
46 $expression_list = $engine->getTextMetadata($list_key, array());
47
48 foreach ($expression_list as $expression_item) {
49 $token = $expression_item['token'];
50 $expression = $expression_item['expression'];
51
52 $result = $this->evaluateExpression($expression);
53
54 if ($result === null) {
55 $result = $expression_item['original'];
56 }
57
58 $engine->overwriteStoredText($token, $result);
59 }
60 }
61
62 private function evaluateExpression($expression) {
63 static $string_map;
64
65 if ($string_map === null) {
66 $string_map = array(
67 'strings' => array(
68 'platform' => array(
69 'server' => array(
70 'name' => PlatformSymbols::getPlatformServerName(),
71 'path' => PlatformSymbols::getPlatformServerPath(),
72 ),
73 'client' => array(
74 'name' => PlatformSymbols::getPlatformClientName(),
75 'path' => PlatformSymbols::getPlatformClientPath(),
76 ),
77 ),
78 ),
79 );
80 }
81
82 $parts = explode('.', $expression);
83
84 $cursor = $string_map;
85 foreach ($parts as $part) {
86 if (isset($cursor[$part])) {
87 $cursor = $cursor[$part];
88 } else {
89 break;
90 }
91 }
92
93 if (is_string($cursor)) {
94 return $cursor;
95 }
96
97 return null;
98 }
99
100}