| 1 | <?php |
||
| 5 | class template |
||
| 6 | { |
||
| 7 | /** |
||
| 8 | * This method replaces {$key} entries in a string with the value of that key in an arguments list |
||
| 9 | * If the key isn't in the arguments array, it will remain in the returned string as-is. |
||
| 10 | * The arguments list may be an object or an array and the values may be basic types or callable. |
||
| 11 | * In the latter case, the key will be substituted with the return value of the callable. The callable |
||
| 12 | * is called with the key matched. |
||
| 13 | * <code> |
||
| 14 | * $parsedTemplate = \arc\template::substitute( 'Hello {$world} {$foo}', [ 'world' => 'World!' ] ); |
||
| 15 | * // => 'Hello World! {$foo}' |
||
| 16 | *</code> |
||
| 17 | * @param string $template |
||
| 18 | * @param array $arguments |
||
| 19 | * @return string |
||
| 20 | */ |
||
| 21 | public static function substitute($template, $arguments) |
||
| 22 | { |
||
| 23 | if (is_object($arguments) && !($arguments instanceof \ArrayObject )) { |
||
| 24 | $arguments = get_object_vars( $arguments ); |
||
| 25 | } |
||
| 26 | $regex = '\{\$(' . join( array_keys( (array) $arguments ), '|' ) . ')\}'; |
||
| 27 | |||
| 28 | return preg_replace_callback( '/'.$regex.'/', function ($matches) use ($arguments) { |
||
| 29 | $argument = $arguments[ $matches[1] ]; |
||
| 30 | if (is_callable( $argument )) { |
||
| 31 | $argument = call_user_func( $argument, $matches[1] ); |
||
| 32 | } |
||
| 33 | |||
| 34 | return $argument; |
||
| 35 | }, $template ); |
||
| 36 | } |
||
| 37 | |||
| 38 | /** |
||
| 39 | * This method is identical to \arc\template::substitute but it removes any keys left over. |
||
| 40 | * <code> |
||
| 41 | * $parsedTemplate = \arc\template::substituteAll( 'Hello {$world} {$foo}', [ 'world' => 'World!' ] ); |
||
| 42 | * // => 'Hello World! ' |
||
| 43 | *</code> |
||
| 44 | * @param string $template |
||
| 45 | * @param array $arguments |
||
| 46 | * @return string |
||
| 47 | */ |
||
| 48 | public static function substituteAll($template, $arguments) |
||
| 49 | { |
||
| 50 | return preg_replace('/\{\$.*\}/', '', self::substitute( $template, $arguments ) ); |
||
| 51 | } |
||
| 52 | |||
| 53 | public static function compile($template) |
||
| 54 | { |
||
| 55 | $prefix = <<<'EOFUNC' |
||
| 56 | extract( $arguments, EXTR_REFS ); |
||
| 57 | ob_start(); |
||
| 58 | EOFUNC; |
||
| 59 | $postfix = <<<'EOFUNC' |
||
| 60 | $result = ob_get_contents(); |
||
| 61 | ob_end_clean(); |
||
| 62 | |||
| 63 | return $result; |
||
| 64 | EOFUNC; |
||
| 65 | |||
| 66 | return create_function( '$arguments', $prefix .' ?'.'>'. $template .'<?'.'php ' . $postfix ); |
||
|
1 ignored issue
–
show
|
|||
| 67 | } |
||
| 68 | |||
| 69 | public static function compileSubstitute($template) |
||
| 70 | { |
||
| 71 | $template = preg_replace('/EOTEMPLATE;.*$/', '', $template); |
||
| 72 | |||
| 73 | return self::compile( "<"."?php\n echo <<<EOTEMPLATE\n".$template."\nEOTEMPLATE;\n ?".">"); |
||
| 74 | } |
||
| 75 | |||
| 76 | public static function run($template, $arguments) |
||
| 84 | } |
||
| 85 |
create_functioncan pose a great security vulnerability as it is similar toeval, and could be used for arbitrary code execution. We highly recommend to use a closure instead.