Skip to content
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

Unable to extend UrlScript #187

Closed
jakubboucek opened this issue Nov 14, 2020 · 2 comments · Fixed by #188
Closed

Unable to extend UrlScript #187

jakubboucek opened this issue Nov 14, 2020 · 2 comments · Fixed by #188

Comments

@jakubboucek
Copy link
Contributor

jakubboucek commented Nov 14, 2020

Version: 3.0.5
PHP: 7.3.24

Bug Description

UrlScript is not work correctly when is extended. The method withPath() is calling themself recursive in infinite loop and crash with error:

Maximum function nesting level of '256' reached, aborting!

The reason is the callback parent::withPath in call_user_func() function is always called in top of descendants hierarchy context.

That's mean:

class CustomUrl extends UrlScript      parent::withPath()   call_user_func('parent::withPath')
                                         |                                    |
        +--------------------------------+------------------------------------+
        |  +-------------------------------------------------------------------------------------+
        v  v                                                                                     |
class UrlScript extends UrlImmutable   parent::withPath()   call_user_func('parent::withPath')   |
                                         |                                    |                  |       
           +-----------------------------+                                    +------------------+
           |
           v
class UrlImmutable

Steps To Reproduce

class CustomUrl extends \Nette\Http\UrlScript {}

$url = new CustomUrl('http://example.com')'
$url->withPath('foo'); // crash

Expected Behavior

Don't crash ;-)

Possible Solution

Only reason why is UrlScript using call_user_func() function to call parent method through is calling in cloned object context.

UrlScript is inherits from UrlImmuitable, UrlImmuitable::withPath() is always create clone too. When calls UrlScript::withPath() it's clone class twice.

Possible Solution is stop cloning object in UrlScript scope and rely it to UrlImmuitable.

I suggest to change method withPath() like:

public function withPath(string $path, string $scriptPath = '')
{
    $dolly = parent::withPath($path);
    $dolly->scriptPath = $scriptPath;
    return $dolly;
}

Is it my considerations right? No, isn't, see comment below.

@jakubboucek
Copy link
Contributor Author

Update: Suggested solution doesn't works :-/

@jakubboucek
Copy link
Contributor Author

This code is works very well but ugly:

public function withPath(string $path, string $scriptPath = '')
{
    $dolly = clone $this;
    $dolly->scriptPath = $scriptPath;
    return call_user_func([$dolly, '\Nette\Http\UrlImmutable::withPath'], $path);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant