/
vshmidt
/
php
Обзор
Документация
Войти
/
vshmidt
/
php
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
ext/dom/tests/DOMXPath_callables.phpt
88 строк
2 KB
Niels Dossche
[RFC] Improve callbacks in ext/dom and ext/xsl (#12627)
13 янв 2024, 02:00
Не верифицирован
13 янв 2024, 02:00
90785dd
Код
Авторство
О чём код?
--TEST-- registerPHPFunctions() with callables - legit cases --EXTENSIONS-- dom --FILE-- <?php class MyClass { public static function dump(string $var) { var_dump($var); } } class MyDOMXPath extends DOMXPath { public function registerCycle() { $this->registerPhpFunctions(["cycle" => array($this, "dummy")]); } public function dummy(string $var) { echo "dummy: $var\n"; } } $doc = new DOMDocument(); $doc->loadHTML('<a href="https://php.net">hello</a>'); echo "--- Legit cases: none ---\n"; $xpath = new DOMXPath($doc); $xpath->registerNamespace("php", "http://php.net/xpath"); try { $xpath->evaluate("//a[php:function('var_dump', string(@href))]"); } catch (Error $e) { echo $e->getMessage(), "\n"; } echo "--- Legit cases: all ---\n"; $xpath->registerPHPFunctions(null); $xpath->evaluate("//a[php:function('var_dump', string(@href))]"); $xpath->evaluate("//a[php:function('MyClass::dump', string(@href))]"); echo "--- Legit cases: set ---\n"; $xpath = new DOMXPath($doc); $xpath->registerNamespace("php", "http://php.net/xpath"); $xpath->registerPhpFunctions([]); $xpath->registerPHPFunctions(["xyz" => MyClass::dump(...), "mydump" => function (string $x) { var_dump($x); }]); $xpath->registerPhpFunctions(str_repeat("var_dump", mt_rand(1, 1) /* defeat SCCP */)); $xpath->evaluate("//a[php:function('mydump', string(@href))]"); $xpath->evaluate("//a[php:function('xyz', string(@href))]"); $xpath->evaluate("//a[php:function('var_dump', string(@href))]"); try { $xpath->evaluate("//a[php:function('notinset', string(@href))]"); } catch (Throwable $e) { echo $e->getMessage(), "\n"; } echo "--- Legit cases: set with cycle ---\n"; $xpath = new MyDOMXPath($doc); $xpath->registerNamespace("php", "http://php.net/xpath"); $xpath->registerCycle(); $xpath->evaluate("//a[php:function('cycle', string(@href))]"); echo "--- Legit cases: reset to null ---\n"; $xpath->registerPhpFunctions(null); $xpath->evaluate("//a[php:function('var_dump', string(@href))]"); ?> --EXPECT-- --- Legit cases: none --- No callbacks were registered --- Legit cases: all --- string(15) "https://php.net" string(15) "https://php.net" --- Legit cases: set --- string(15) "https://php.net" string(15) "https://php.net" string(15) "https://php.net" No callback handler "notinset" registered --- Legit cases: set with cycle --- dummy: https://php.net --- Legit cases: reset to null --- string(15) "https://php.net"