[ Index ] |
PHP Cross Reference of phpBB-3.3.12-deutsch |
[Summary view] [Print] [Text view]
1 <?php 2 3 /** 4 * @package s9e\TextFormatter 5 * @copyright Copyright (c) 2010-2022 The s9e authors 6 * @license http://www.opensource.org/licenses/mit-license.php The MIT License 7 */ 8 namespace s9e\TextFormatter\Configurator\JavaScript; 9 10 class CallbackGenerator 11 { 12 /** 13 * @var array Path to callbacks in keys, callback signature in values 14 */ 15 public $callbacks = [ 16 'tags.*.attributes.*.filterChain.*' => [ 17 'attrValue' => '*', 18 'attrName' => 'string' 19 ], 20 'tags.*.filterChain.*' => [ 21 'tag' => '!Tag', 22 'tagConfig' => '!Object' 23 ] 24 ]; 25 26 /** 27 * @var Encoder 28 */ 29 protected $encoder; 30 31 /** 32 * Constructor 33 */ 34 public function __construct() 35 { 36 $this->encoder = new Encoder; 37 } 38 39 /** 40 * Replace all callbacks in given config 41 * 42 * @param array $config Original config 43 * @return array Modified config 44 */ 45 public function replaceCallbacks(array $config) 46 { 47 foreach ($this->callbacks as $path => $params) 48 { 49 $config = $this->mapArray($config, explode('.', $path), $params); 50 } 51 52 return $config; 53 } 54 55 /** 56 * Build the list of arguments used in a callback invocation 57 * 58 * @param array $params Callback parameters 59 * @param array $localVars Known vars from the calling scope 60 * @return string JavaScript code 61 */ 62 protected function buildCallbackArguments(array $params, array $localVars) 63 { 64 // Remove 'parser' as a parameter, since there's no such thing in JavaScript 65 unset($params['parser']); 66 67 // Rebuild the local vars map to include global vars and computed values 68 $available = array_combine(array_keys($localVars), array_keys($localVars)); 69 $available += [ 70 'innerText' => '(tag.getEndTag() ? text.substr(tag.getPos() + tag.getLen(), tag.getEndTag().getPos() - tag.getPos() - tag.getLen()) : "")', 71 'logger' => 'logger', 72 'openTags' => 'openTags', 73 'outerText' => 'text.substr(tag.getPos(), (tag.getEndTag() ? tag.getEndTag().getPos() + tag.getEndTag().getLen() - tag.getPos() : tag.getLen()))', 74 'registeredVars' => 'registeredVars', 75 'tagText' => 'text.substr(tag.getPos(), tag.getLen())', 76 'text' => 'text' 77 ]; 78 79 $args = []; 80 foreach ($params as $k => $v) 81 { 82 if (isset($v)) 83 { 84 // Param by value 85 $args[] = $this->encoder->encode($v); 86 } 87 elseif (isset($available[$k])) 88 { 89 // Param by name that matches a local expression 90 $args[] = $available[$k]; 91 } 92 else 93 { 94 $args[] = 'registeredVars[' . json_encode($k) . ']'; 95 } 96 } 97 98 return implode(',', $args); 99 } 100 101 /** 102 * Generate a function from a callback config 103 * 104 * @param array $config Callback config 105 * @param array $params Param names as keys, param types as values 106 * @return Code 107 */ 108 protected function generateFunction(array $config, array $params) 109 { 110 $js = (string) $config['js']; 111 112 // returnFalse() and returnTrue() can be used as-is 113 if ($js === 'returnFalse' || $js === 'returnTrue') 114 { 115 return new Code($js); 116 } 117 118 // Add an empty list of params if none is set 119 $config += ['params' => []]; 120 121 $src = $this->getHeader($params); 122 $src .= 'function(' . implode(',', array_keys($params)) . '){'; 123 $src .= 'return ' . $this->parenthesizeCallback($js); 124 $src .= '(' . $this->buildCallbackArguments($config['params'], $params) . ');}'; 125 126 return new Code($src); 127 } 128 129 /** 130 * Generate a function header for given signature 131 * 132 * @param array $params Param names as keys, param types as values 133 * @return string 134 */ 135 protected function getHeader(array $params) 136 { 137 // Prepare the function's header 138 $header = "/**\n"; 139 foreach ($params as $paramName => $paramType) 140 { 141 $header .= '* @param {' . $paramType . '} ' . $paramName . "\n"; 142 } 143 $header .= "* @return {*}\n"; 144 $header .= "*/\n"; 145 146 return $header; 147 } 148 149 /** 150 * Replace callbacks in given config array 151 * 152 * @param array $array Original config 153 * @param string[] $path Path to callbacks 154 * @param array $params Default params 155 * @return array Modified config 156 */ 157 protected function mapArray(array $array, array $path, array $params) 158 { 159 $key = array_shift($path); 160 $keys = ($key === '*') ? array_keys($array) : [$key]; 161 foreach ($keys as $key) 162 { 163 if (!isset($array[$key])) 164 { 165 continue; 166 } 167 $array[$key] = (empty($path)) ? $this->generateFunction($array[$key], $params) : $this->mapArray($array[$key], $path, $params); 168 } 169 170 return $array; 171 } 172 173 /** 174 * Add parentheses to a function literal, if necessary 175 * 176 * Will return single vars as-is, and will put anything else between parentheses 177 * 178 * @param string $callback Original callback 179 * @return string Modified callback 180 */ 181 protected function parenthesizeCallback($callback) 182 { 183 return (preg_match('(^[.\\w]+$)D', $callback)) ? $callback : '(' . $callback . ')'; 184 } 185 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Sun Jun 23 12:25:44 2024 | Cross-referenced by PHPXref 0.7.1 |