Open MetaTunes opened 3 years ago
Hi @ryancramerdesign. This issue still seems to be open. Any comment?
I got bitten by this bug. I had to change to:
<?php namespace ProcessWire;
echo \ProcessWire\wire('page')->title;
Its odd as the first line gets added by default, though as mentioned here gets stripped
@ryancramerdesign - this still seems to be open. I assume the intention is not to add the namespace if it isn't in the hanna PHP code, otherwise it should be added and so should the 'die'.
I think, in that case, the fix is simple - replace the line
$code = str_replace($openPHP, "$openPHP\n$firstLine\n", $code);
by
$code = str_replace($php, "$openPHP\n$firstLine\n", $code);
The existing line does not work because, if namespace was in the code it has been removed, but $opnPHP includes it, so the str_replace does not operate. I have created a PR for this
See comment at https://processwire.com/talk/topic/3745-hanna-code/page/18/?tab=comments#comment-217245 In the TextformatterHannaCode::getPHP() method, the lines
if(strpos($code, 'namespace') && preg_match('/(namespace\s+ProcessWire(?:;|\s*;))/', $code, $matches)) { $openPHP = $openPHPNS; $code = str_replace($matches[1], '', $code); }
have the effect of removing the namespace if it is included in the code. Furthermore, the codeif(!defined("PROCESSWIRE")) die("no direct access");
is not added.To demonstrate this, write a simple (PHP) Hanna as follows:
echo 'This is a test';
just like that with no opening <?phpThe resulting cached code is:
<?php if(!defined("PROCESSWIRE")) die("no direct access"); echo 'This is a test';
I think the namespace should have been included there by default.
Now change the Hanna code to
<?php echo 'This is a test';
The resulting cached code is similarly:
<?php if(!defined("PROCESSWIRE")) die("no direct access"); echo 'This is a test';
which is arguably the correct result, having deliberately not included the namespace in the opening line (although I do not buy that argument - see comment below).
Now change the Hanna code to
<?php namespace ProcessWire; echo 'This is a test';
This yields the following cached code:
<?php echo 'This is a test';
i.e. no namespace and no die()
I'm not sure what exactly was intended with the namespacing changes, but something seems wrong whatever. I would have thought that the ProcessWire namespace should be included by default in all instances (including where there is <?php on the opening line otherwise any any called functions will not work without the \ProcessWire\ prefix and backwards compatibility is lost). The only time it would not be included is if a different namespace was explicitly referenced.
I have done a hack which is shown in the forum comment referred to, but I am sure it is not the right answer as I don't know what was intended.