-
Notifications
You must be signed in to change notification settings - Fork 7.9k
[RFC] Improve callbacks in ext/dom and ext/xsl #12627
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 6 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
84d143f
Improve callbacks in ext/dom and ext/xsl
nielsdos b009284
Refactor code to allow multiple namespaces
nielsdos e8b9b5e
Implement namespaced functions for DOM
nielsdos 70817b9
Implement namespaced functions for XSL
nielsdos ce5ab06
Update to the new registerPhpFunctionNS proposal
nielsdos 48bcd44
Add additional test
nielsdos 28a3c40
Split error cases off of tests
nielsdos 552dde0
Address code review comments
nielsdos 32a6270
Split off more tests
nielsdos eb1e449
[ci skip] NEWS
nielsdos 657679e
[ci skip] UPGRADING
nielsdos 9561820
[ci skip] UPGRADING
nielsdos File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
--TEST-- | ||
registerPHPFunctions() with callables | ||
--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))]"); | ||
|
||
echo "--- Error cases ---\n"; | ||
|
||
$xpath = new DOMXPath($doc); | ||
try { | ||
$xpath->registerPhpFunctions("nonexistent"); | ||
} catch (TypeError $e) { | ||
echo $e->getMessage(), "\n"; | ||
} | ||
|
||
try { | ||
$xpath->registerPhpFunctions(function () {}); | ||
} catch (TypeError $e) { | ||
echo $e->getMessage(), "\n"; | ||
} | ||
|
||
try { | ||
$xpath->registerPhpFunctions([function () {}]); | ||
} catch (Throwable $e) { | ||
echo $e->getMessage(), "\n"; | ||
} | ||
|
||
try { | ||
$xpath->registerPhpFunctions([var_dump(...)]); | ||
} catch (Throwable $e) { | ||
echo $e->getMessage(), "\n"; | ||
} | ||
|
||
try { | ||
$xpath->registerPhpFunctions(["nonexistent"]); | ||
} catch (Throwable $e) { | ||
echo $e->getMessage(), "\n"; | ||
} | ||
|
||
try { | ||
$xpath->registerPhpFunctions(["" => var_dump(...)]); | ||
} catch (Throwable $e) { | ||
echo $e->getMessage(), "\n"; | ||
} | ||
|
||
try { | ||
$xpath->registerPhpFunctions(["\0" => var_dump(...)]); | ||
} catch (Throwable $e) { | ||
echo $e->getMessage(), "\n"; | ||
} | ||
|
||
try { | ||
$xpath->registerPhpFunctions(""); | ||
} catch (Throwable $e) { | ||
echo $e->getMessage(), "\n"; | ||
} | ||
|
||
?> | ||
--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" | ||
--- Error cases --- | ||
DOMXPath::registerPhpFunctions(): Argument #1 ($restrict) must be a callable, function "nonexistent" not found or invalid function name | ||
DOMXPath::registerPhpFunctions(): Argument #1 ($restrict) must be of type array|string|null, Closure given | ||
Object of class Closure could not be converted to string | ||
Object of class Closure could not be converted to string | ||
DOMXPath::registerPhpFunctions(): Argument #1 ($restrict) must be an array with valid callbacks as values, function "nonexistent" not found or invalid function name | ||
DOMXPath::registerPhpFunctions(): Argument #1 ($restrict) must be an array containing valid callback names | ||
DOMXPath::registerPhpFunctions(): Argument #1 ($restrict) must be an array containing valid callback names | ||
DOMXPath::registerPhpFunctions(): Argument #1 ($restrict) must be a valid callback name |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We usually split error cases into a different test file