Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:121716 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 47718 invoked from network); 19 Nov 2023 18:26:21 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 19 Nov 2023 18:26:21 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 8FAB0180034 for ; Sun, 19 Nov 2023 10:26:21 -0800 (PST) X-Spam-Checker-Version: SpamAssassin 4.0.0 (2022-12-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-3.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from mail-lf1-f51.google.com (mail-lf1-f51.google.com [209.85.167.51]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Sun, 19 Nov 2023 10:26:21 -0800 (PST) Received: by mail-lf1-f51.google.com with SMTP id 2adb3069b0e04-507adc3381cso4691852e87.3 for ; Sun, 19 Nov 2023 10:26:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1700418377; x=1701023177; darn=lists.php.net; h=content-transfer-encoding:in-reply-to:from:content-language :references:to:subject:user-agent:mime-version:date:message-id:from :to:cc:subject:date:message-id:reply-to; bh=cz/N400iLOTsrdpAc9mk/HDICO8c9lw1dlUMv/o53e0=; b=d65R2gOFlhZAK7amlYmVgSojXyYijaGKdd0sGQ8XNSQ8UQie7UmMAbdM2xUOGk4/JI 5llAcpOflLtZdIVXL4hreuFO5NWj487JUTpJ5zrQ9F0HH+qvwlqgDECEi+iX0XTWncEm 8K7oZ00wyXN7IRZCvhLxFz5RNGy1WvAmjyW1cvd8lNwONWSDN7q58gFm+orjYWcsP635 Hu7q2MCg0/SYgdfCS10FfFXHHPEV/labafJAv3uiQJUYBGMl0Od9Nm3VxwxFi6gIwKkL hpLAa2enodh+zfkzlChM5Y/R5LPg3KiFXboPPBhKr3UP+p3NBn2GZH6JuYC0Ur33En3c rEFA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700418377; x=1701023177; h=content-transfer-encoding:in-reply-to:from:content-language :references:to:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=cz/N400iLOTsrdpAc9mk/HDICO8c9lw1dlUMv/o53e0=; b=h3RPVHJePlVtXVGyIGjqDChMiKjOsh4PjDaIC1wZYfLzKujQNXrIYGsZsyj8iNPCrT 0iKhqLi66gFnDbL6vMtI662WzEKFcbKomNwBAMO5/8TKjFDrC2YApYk8QR9DZ+5LbjqO vjQ0g4j1yCCZszIYnQoMeKi2ttxrWxp6qZhYAzH7c4UMMeASNLmWVfImzqS/4zAHB9wj 3hJ0WedtmrMEO8UJLstfD2yYiNqohVuPnZXtzvBYcZKFIBGLiwMW6ZDyxq4fsOdlPI45 dGPwJJI/j3ZEkE6IEwelenB+Z5mgM25DrYPLgKBfbniQ8Te3Qbfd8KI9yD0RLWJwP6eV CUZQ== X-Gm-Message-State: AOJu0YwmSC/Ueyd+o/8m9+xKZRtax8xO2CsS7MnCffO8IUd/Fxl7XU7y p0WdDiSAPty64Qu2Zp3MWFAmi31ii/s= X-Google-Smtp-Source: AGHT+IFmwkoPfqIW+WSl6mp9btp4RzFWw79qjkjEhC9bVYzEMaZyOKiE/6wCmI3z1h1jPlCefvpkGA== X-Received: by 2002:a19:f60d:0:b0:503:3cc:cd39 with SMTP id x13-20020a19f60d000000b0050303cccd39mr3987114lfe.8.1700418376864; Sun, 19 Nov 2023 10:26:16 -0800 (PST) Received: from ?IPV6:2a02:1811:cc83:ee50:280e:1e36:3a00:824? (ptr-dtfv08akcem5xburtic.18120a2.ip6.access.telenet.be. [2a02:1811:cc83:ee50:280e:1e36:3a00:824]) by smtp.gmail.com with ESMTPSA id r15-20020a1709067fcf00b00985ed2f1584sm3085836ejs.187.2023.11.19.10.26.16 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 19 Nov 2023 10:26:16 -0800 (PST) Message-ID: Date: Sun, 19 Nov 2023 19:26:15 +0100 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird To: PHP internals References: Content-Language: en-US In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Subject: Re: DOMXPath / XSLTProcessor function callbacks From: dossche.niels@gmail.com (Niels Dossche) Hi Frederik On 16/11/2023 20:34, Frederik Bosch wrote: > Hi Niels, > > On 16-10-2023 23:19, Niels Dossche wrote: >> Sorry for the resend... I accidentally replied to you only without including the list the first time. >> >> On 15/10/2023 21:37, Frederik Bosch wrote: >>> Dear Niels, >>> >>> First of all, thanks for all your hard work already on the DOM and SimpleXML extensions. I have been following your work in PHP-SRC, great! I am the author of this XSL 2.0 Transpiler in PHP package (https://github.com/genkgo/xsl). It is indeed possible to use workarounds for closures or object methods. I am relying on them in my package. >>> >>> My suggestion for the future would be to add the following method. >>> >>> public XSLTProcessor::registerFunctionsNS(string $namespace, array|ArrayAccess $functions): void >>> >>> Then a user can register functions like this. >>> >>> $xsltProcessorOrDomXpath->registerFunctionsNS('urn:my.namespace', array('upper-case', 'strtoupper', 'pow' => fn ($a, $b) => $a[0]->textContent ** $b[0]->textContent, 'other' => [$obj, 'method']); >>> >> Interesting suggestion. So you want to be able to use something like `my.namespace:function(...)` as I understand it. > > Sorry I missed these messages completely. Unfortunately internals ends up in my spam. Sorry that I did not reply. What I want to use is this. > > $processor->registerFunctionNS( >     'urn:my.ns', >     [ >         'round-up' => fn ($arg1) => ceil($arg1), >         'round-down' => fn ($arg1) => floor($arg1) >     ] > ); > > >     >     > > > While it follows the logic of XSL setParameter which also allows a namespace and DOM setAttributeNS, createElementNS et al methods, it also allows you to keep registerPhpFunctions method as is. Because basically, it can then become shorthand for registerFunctionNS (simplified): > > $processor->registerFunctionNS( >     'http://php.net/xsl', >     [ >         'functionString' => fn ($func, ...$args) => call_user_func_array($func, $args), >         'function' => fn ($func, ...$args) => call_user_func_array($func, $args) >     ] > ); > Ah okay I get now what you mean. This would be a further nice-to-have as well. And as I'm updating the API anyway I think it's reasonable to add this to the RFC. I'm currently busy with something else but I'll be able to work again on this towards the end of the month probably. >> I'm not sure that adding this is that much more beneficial though (complexity vs benefit trade-off). >> I assume this is motivated by the fact that you can then use third party libraries while having to worry less about name clashes? > It allows me to use namespaces as intended. >> Let's say we add non-namespace `registerFunction(string $name, callable $callback): void`, you can then still use a convention of using a prefix, thus _kinda_ achieving the same. > I don't understand this one. Can I use a prefix without namespace? No, this was just me misunderstanding :) >> >> In any case, this is going to be hard to support in combination with the underlying library (libxslt). >> That's because the function namespace registration is process-wide, so this cannot be changed at runtime and certainly not for ZTS SAPIs. > So namespaces http://php.net/xsl and http://php.net/xpath are already process-wide, right? I do not see why more custom namespaces then would be a problem, but I have very little knowledge of libxslt library. When I wrote this I only found APIs for process-wide registrations. This would be bad in threaded webserver contexts as request data leaks between requests. I looked again and I now found the APIs that we can use for request-wide registrations instead of process-wide, which doesn't have that problem. >> >>> The registered functions should use the same methodology as php:function(). Hence, string casting of arguments is something the library user should do. I would leave registerPHPFunctions as is, and maybe discourage it in favor of the method above. What if both are called? I think it would be most clear if the registerFunctionsNS method throws InvalidArgumentException when http://php.net/xsl or http://php.net/xpath is passed as namespace >>> >>> Cheers, >>> Frederik Kind regards Niels >> Cheers >> Niels >> >>> >>> On 13-10-2023 00:39, Niels Dossche wrote: >>>> I'm looking to extend the functionality of calling PHP functions from within the DOMXPath or XSLTProcessor classes. >>>> >>>> In case you're unfamiliar here's a quick rundown. >>>> The DOMXPath class allows you to execute XPath queries on a DOM tree to lookup certain nodes satisfying a filter. >>>> PHP allows the user to execute function callbacks within these. For example (from the manual): >>>>     $xpath->query('//book[php:functionString("substr", title, 0, 3) = "PHP"]'); >>>> This will read the title element's text content, call substr on it, and then compare the output against "PHP". >>>> You can not only call builtin functions, but also user functions. >>>> >>>> To be able to call PHP functions, you need to use DOMXPath::registerPhpFunctions() (https://www.php.net/manual/en/domxpath.registerphpfunctions.php). >>>> You either pass in NULL to allow all functions, or pass in which function names are allowed to be called. >>>> >>>> Similarly, XSLTProcessor has the same registerPhpFunctions() method. >>>> For XSLT it's mostly used for performing arbitrary manipulations on input data. >>>> Normally the output of the function is put into the resulting document. >>>> >>>> >>>> So what's the problem? >>>> The current system doesn't allow you to call closures or object methods. >>>> There are tricks you can do with global variables and global functions to try to work around this, but that's quite cumbersome. >>>> >>>> There are two feature requests for this on the old bugtracker: >>>>     - https://bugs.php.net/bug.php?id=38595 >>>>     - https://bugs.php.net/bug.php?id=49567 >>>> >>>> It's not hard to implement support for this, the question is just what API we should go with. >>>> Based on what I've read, there are at least two obvious options: >>>> >>>> >>>> OPTION 1) Extend registerPHPFunctions() such that you can pass in callables >>>> >>>> ``` >>>> // Adapted from https://bugs.php.net/bug.php?id=38595 >>>> $xslt->registerPHPFunctions(array( >>>>      'functionblah', // Like we used to >>>>      'func2' => fn ($x) => ..., >>>>      'func3' => array($obj, 'method'), // etc >>>> )); >>>> ``` >>>> >>>> Example: Using php:function("func3") inside XPath/XSLT in this case will result in calling method on $obj. >>>> Similarly func2 will call the closure, and functionblah in the snippet just allowlists calling functionblah. >>>> >>>> It's a backwards compatible solution and a natural extension to the current method. >>>> It may be hard to discover this feature compared to having a new API though. >>>> >>>> Furthermore, once you pass in function names to registerPHPFunctions(), you're restricting what can be called. >>>> For example: imagine you want to call both ucfirst() and $obj->method(), so you pass in an entry like func3 in the above example. >>>> Now you have to pass in ucfirst to registerPHPFunctions() too, because registerPHPFunctions() acts as an allowlist. May be a bit inconvenient. >>>> >>>> >>>> OPTION 2) Add new methods to register / unregister callables >>>> >>>> This may be the cleaner way to go about it on first sight, but there's a potential BC break when new methods clash in user-defined subclasses. >>>> >>>> Question here is: what about the interaction with registerPHPFunction? >>>> What if both registerPHPFunction() and the register method add something with the same name? >>>> What if registerPHPFunction() didn't allowlist a function but the register method added it, may be a bit confusing for users. >>>> The interaction may be surprising. >>>> >>>> >>>> >>>> Please let me know your thoughts. >>>> >>>> Cheers >>>> Niels >>>> > > Again, whatever solution you choose, I am happy with all your contributions. > > Cheers, > Frederik > >