Skip to content

Namespace being removed and not replaced #26

@MetaTunes

Description

@MetaTunes

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 code
if(!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 <?php

The 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions