Verzeichnisstruktur phpBB-3.3.15
- Veröffentlicht
- 28.08.2024
So funktioniert es
|
Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück |
Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis. |
|
(Beispiel Datei-Icons)
|
Auf das Icon klicken um den Quellcode anzuzeigen |
ProgrammableCallback.php
001 <?php
002
003 /**
004 * @package s9e\TextFormatter
005 * @copyright Copyright (c) 2010-2022 The s9e authors
006 * @license http://www.opensource.org/licenses/mit-license.php The MIT License
007 */
008 namespace s9e\TextFormatter\Configurator\Items;
009
010 use InvalidArgumentException;
011 use s9e\TextFormatter\Configurator\ConfigProvider;
012 use s9e\TextFormatter\Configurator\Helpers\ConfigHelper;
013 use s9e\TextFormatter\Configurator\JavaScript\Code;
014 use s9e\TextFormatter\Configurator\JavaScript\FunctionProvider;
015
016 class ProgrammableCallback implements ConfigProvider
017 {
018 /**
019 * @var callable Callback
020 */
021 protected $callback;
022
023 /**
024 * @var string JavaScript source code for this callback
025 */
026 protected $js = 'returnFalse';
027
028 /**
029 * @var array List of params to be passed to the callback
030 */
031 protected $params = [];
032
033 /**
034 * @var array Variables associated with this instance
035 */
036 protected $vars = [];
037
038 /**
039 * @param callable $callback
040 */
041 public function __construct($callback)
042 {
043 if (!is_callable($callback))
044 {
045 throw new InvalidArgumentException(__METHOD__ . '() expects a callback');
046 }
047
048 $this->callback = $this->normalizeCallback($callback);
049 $this->autoloadJS();
050 }
051
052 /**
053 * Add a parameter by value
054 *
055 * @param mixed $paramValue
056 * @return self
057 */
058 public function addParameterByValue($paramValue)
059 {
060 $this->params[] = $paramValue;
061
062 return $this;
063 }
064
065 /**
066 * Add a parameter by name
067 *
068 * The value will be dynamically generated by the caller
069 *
070 * @param string $paramName
071 * @return self
072 */
073 public function addParameterByName($paramName)
074 {
075 if (array_key_exists($paramName, $this->params))
076 {
077 throw new InvalidArgumentException("Parameter '" . $paramName . "' already exists");
078 }
079
080 $this->params[$paramName] = null;
081
082 return $this;
083 }
084
085 /**
086 * Get this object's callback
087 *
088 * @return callable
089 */
090 public function getCallback()
091 {
092 return $this->callback;
093 }
094
095 /**
096 * Get this callback's JavaScript
097 *
098 * @return string
099 */
100 public function getJS()
101 {
102 return $this->js;
103 }
104
105 /**
106 * Get this object's variables
107 *
108 * @return array
109 */
110 public function getVars()
111 {
112 return $this->vars;
113 }
114
115 /**
116 * Remove all the parameters
117 *
118 * @return self
119 */
120 public function resetParameters()
121 {
122 $this->params = [];
123
124 return $this;
125 }
126
127 /**
128 * Set this callback's JavaScript
129 *
130 * @param string $js JavaScript source code for this callback
131 * @return self
132 */
133 public function setJS($js)
134 {
135 $this->js = $js;
136
137 return $this;
138 }
139
140 /**
141 * Set or overwrite one of this callback's variable
142 *
143 * @param string $name Variable name
144 * @param string $value Variable value
145 * @return self
146 */
147 public function setVar($name, $value)
148 {
149 $this->vars[$name] = $value;
150
151 return $this;
152 }
153
154 /**
155 * Set all of this callback's variables at once
156 *
157 * @param array $vars Associative array of values
158 * @return self
159 */
160 public function setVars(array $vars)
161 {
162 $this->vars = $vars;
163
164 return $this;
165 }
166
167 /**
168 * {@inheritdoc}
169 */
170 public function asConfig()
171 {
172 $config = ['callback' => $this->callback];
173
174 foreach ($this->params as $k => $v)
175 {
176 if (is_numeric($k))
177 {
178 // By value
179 $config['params'][] = $v;
180 }
181 elseif (isset($this->vars[$k]))
182 {
183 // By name, but the value is readily available in $this->vars
184 $config['params'][] = $this->vars[$k];
185 }
186 else
187 {
188 // By name
189 $config['params'][$k] = null;
190 }
191 }
192
193 if (isset($config['params']))
194 {
195 $config['params'] = ConfigHelper::toArray($config['params'], true, true);
196 }
197
198 // Add the callback's JavaScript representation
199 $config['js'] = new Code($this->js);
200
201 return $config;
202 }
203
204 /**
205 * Try to load the JavaScript source for this callback
206 *
207 * @return void
208 */
209 protected function autoloadJS()
210 {
211 if (!is_string($this->callback))
212 {
213 return;
214 }
215
216 try
217 {
218 $this->js = FunctionProvider::get($this->callback);
219 }
220 catch (InvalidArgumentException $e)
221 {
222 // Do nothing
223 }
224 }
225
226 /**
227 * Normalize a callback's representation
228 *
229 * @param callable $callback
230 * @return callable
231 */
232 protected function normalizeCallback($callback)
233 {
234 // Normalize ['foo', 'bar'] to 'foo::bar'
235 if (is_array($callback) && is_string($callback[0]))
236 {
237 $callback = $callback[0] . '::' . $callback[1];
238 }
239
240 // Normalize '\\foo' to 'foo' and '\\foo::bar' to 'foo::bar'
241 if (is_string($callback))
242 {
243 $callback = ltrim($callback, '\\');
244 }
245
246 return $callback;
247 }
248 }