42 lines
1.2 KiB
PHP
42 lines
1.2 KiB
PHP
<?php
|
|
|
|
namespace Picea\Extension;
|
|
|
|
use Picea\Compiler\Context;
|
|
|
|
class PrintExtension implements Extension {
|
|
|
|
public array $token = [ "echo", "print", "printf" ];
|
|
|
|
public int $flag = \ENT_QUOTES;
|
|
|
|
public string $encoding;
|
|
|
|
public bool $doubleEncode = true;
|
|
|
|
public function __construct() {
|
|
$this->encoding = ini_get("default_charset");
|
|
}
|
|
|
|
public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string
|
|
{
|
|
switch($token) {
|
|
case 'echo':
|
|
case 'print':
|
|
if (in_array('raw', $options)) {
|
|
return "<?php echo $arguments ?>";
|
|
}
|
|
|
|
return "<?php echo htmlspecialchars((string) $arguments, {$this->flag}, '{$this->encoding}', " . ($this->doubleEncode ? "true" : "false") . ") ?>";
|
|
|
|
case 'printf':
|
|
if (in_array('raw', $options)) {
|
|
return "<?php printf((string) $arguments) ?>";
|
|
}
|
|
|
|
return "<?php echo htmlspecialchars(sprintf((string) $arguments), {$this->flag}, '{$this->encoding}', " . ($this->doubleEncode ? "true" : "false") . ") ?>";
|
|
}
|
|
}
|
|
|
|
}
|