3434
3535use Bubble \Exception \InvalidDataException ;
3636use Bubble \Exception \UnknownFunctionException ;
37+ use Bubble \Data \DataResolver ;
38+ use Bubble \Renderer \Template ;
3739
3840/**
3941 * Eval sandbox
@@ -53,7 +55,7 @@ class EvalSandBox
5355 *
5456 * @var string
5557 */
56- private static $ _functionContext = "\Bubble\Util \FunctionsContext " ;
58+ private static $ _functionContext = "\Bubble\Data \FunctionsContext " ;
5759
5860 /**
5961 * Changes the current functions context.
@@ -74,24 +76,38 @@ public static function setFunctionsContext(string $context): void
7476 self ::$ _functionContext = $ context ;
7577 }
7678
77- public static function eval (string $ code )
79+ public static function eval (string $ code, DataResolver $ resolver )
7880 {
79- $ code = self ::_parseCode ($ code );
81+ $ parsed = self ::_parseCode ($ code, $ resolver );
8082 $ context = self ::$ _functionContext ;
8183
8284 return eval (
83- "\$context = new {$ context }; return {$ code }; "
85+ "{ $ parsed [ 0 ]} \$context = new {$ context }; return {$ parsed [ 1 ] }; "
8486 );
8587 }
8688
87- private static function _parseCode (string $ code ): string
89+ private static function _parseCode (string $ code, DataResolver $ resolver ): array
8890 {
89- return preg_replace_callback ("#@(\w+) \\(#U " , function ($ m ) {
91+ $ var_allocation = "" ;
92+
93+ do {
94+ $ code = preg_replace_callback (Template::DATA_MODEL_QUERY_REGEX , function ($ m ) use ($ resolver , &$ var_allocation ) {
95+ $ var_name = uniqid ("tempvar_ " );
96+ $ var_value = Utilities::toEvalSandBoxValue ($ resolver ->resolve ($ m [1 ]));
97+ $ var_allocation .= "\${$ var_name } = {$ var_value }; " ;
98+
99+ return "\${$ var_name }" ;
100+ }, $ code );
101+ } while (preg_match (Template::DATA_MODEL_QUERY_REGEX , $ code , $ matches ));
102+
103+ $ code = preg_replace_callback ("/@(\w+) \\(/U " , function ($ m ) {
90104 if (!method_exists (self ::$ _functionContext , $ m [1 ])) {
91105 throw new UnknownFunctionException ($ m [1 ]);
92106 }
93107
94108 return str_replace ("@ {$ m [1 ]}( " , "\$context-> {$ m [1 ]}( " , $ m [0 ]);
95109 }, $ code );
110+
111+ return [$ var_allocation , $ code ];
96112 }
97113}
0 commit comments