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

Fix setting url to http.request #43

Closed
wants to merge 4 commits into from

Conversation

jfilla
Copy link

@jfilla jfilla commented Apr 3, 2020

Service http.request can be any class implementing interface IRequest. If you have custom IRequest implementation and console.url config is set, DI container cache looks like this:

public function createServiceHttp__request(): Flowgate\FunctionalTests\RequestMock
{
	return new Nette\Http\Request(new Nette\Http\UrlScript('http://localhost'));
}

, which causes type error. It should look like this

public function createServiceHttp__request(): Flowgate\FunctionalTests\RequestMock
{
	return new Flowgate\FunctionalTests\RequestMock;
}

@mabar
Copy link
Contributor

mabar commented Apr 3, 2020

I believe this works in your case in tests, but it breaks behavior of ConsoleExtension for anyone who overrides Request for standard usage. Imho what should be done here is to override RequestFactory by ConsoleExtension instead so you are still able to override Request

@jfilla
Copy link
Author

jfilla commented Apr 3, 2020

I tried what i think is standard usage (is it?). Creating class

<?php


namespace App;


use Nette\Http\Request;
use Nette\Http\UrlScript;

class CustomRequest extends Request
{

    public function __construct()
    {
        parent::__construct(new UrlScript('http://whatever'));
    }

}

registering it as http.request

services:
    http.request: App\CustomRequest

in cli command i tried to call

$this->container->getByType(IRequest::class);

which fails with error

TypeError: Return value of Container_b2518d818a::createServiceHttp__request() must be an instance of App\CustomRequest, instance of Nette\Http\Request returned

because of this inside DI container

public function createServiceHttp__request(): App\CustomRequest
{
	return new Nette\Http\Request(new Nette\Http\UrlScript('http://localhost'));
}

I think with console.url enabled is imposible to override http.request anyway. I will think about it and figure out better solution.. and fix tests of course.

@@ -153,7 +153,9 @@ public function beforeCompile(): void
if ($config->url !== null && $builder->hasDefinition('http.request')) {
/** @var ServiceDefinition $httpDef */
$httpDef = $builder->getDefinition('http.request');
$httpDef->setFactory(Request::class, [new Statement(UrlScript::class, [$config->url])]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about this.

$httpDef->setFactory($httpDef->getFactory(), [new Statement(UrlScript::class, [$config->url])]);

You must preserver arguments order/position.

Copy link
Contributor

@mabar mabar Apr 3, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am worried it would still break due to possibly different constructor

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking about that too, but i didnt like the idea of preserving argument positions. Original idea was cleaner for me.

@jfilla
Copy link
Author

jfilla commented Apr 6, 2020

What about now? I created console requestFactory, which is used only when default requestFactory is used. Exception is thown on custom requestFactory implementation and console.url set. Console.url in combination with custom requestFactory is in my opinion useless, url should be set by custom requestFactory itself.

@mabar
Copy link
Contributor

mabar commented Apr 6, 2020

Looks way better. I will give it some final touches later today before release. Thanks

@f3l1x
Copy link
Member

f3l1x commented Jun 18, 2020

Hey @jfilla, I want to assure you we'll handle this issue. We didn't have a time yet.

@f3l1x
Copy link
Member

f3l1x commented Jan 3, 2024

Thank you @jfilla ❤️

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

Successfully merging this pull request may close these issues.

3 participants