di icon indicating copy to clipboard operation
di copied to clipboard

Non-static method Nette\Http\RequestFactory::fromGlobals() cannot be called statically

Open rixafy opened this issue 9 months ago • 3 comments

Version: 3.3-dev (previously 3.2-dev)

Bug Description

When generating container, instead of service getter, static class call is used, resulting from this code

$builder->addDefinition($this->prefix('requestFactory'))
	->setFactory(Nette\Http\RequestFactory::class)
	->addSetup('setProxy', [$config->proxy]);

$request = $builder->addDefinition($this->prefix('request'))
	->setFactory('@Nette\Http\RequestFactory::fromGlobals');

Steps To Reproduce

<?php

use Nette\Bridges\HttpDI\HttpExtension;
use Nette\DI\Compiler;
use Nette\Http\Request;

include __DIR__ . '/vendor/autoload.php';

$compiler = new Compiler();
$compiler->addExtension('http', new HttpExtension());

$code = $compiler->setClassName($class = 'Container')->compile();

file_put_contents(__DIR__ . '/code.php', "<?php\n\n$code");
require __DIR__ . '/code.php';

new $class()->getByType(Request::class);

and it generates container like this:

public function createServiceHttp__request(): Nette\Http\Request
{
	return Nette\Http\RequestFactory::fromGlobals();
}

public function createServiceHttp__requestFactory(): Nette\Http\RequestFactory
{
	$service = new Nette\Http\RequestFactory;
	$service->setProxy([]);
	return $service;
}

Expected Behavior

public function createServiceHttp__request(): Nette\Http\Request
{
-	return Nette\Http\RequestFactory::fromGlobals();
+	return $this->getService('http.requestFactory')->fromGlobals();
}

public function createServiceHttp__requestFactory(): Nette\Http\RequestFactory
{
	$service = new Nette\Http\RequestFactory;
	$service->setProxy([]);
	return $service;
}

Possible Solution

I'm not sure, I found some commits that affect code generation, https://github.com/nette/di/commit/07921b828dcb9050b96a2dadbb304bdf9379b9f3, https://github.com/nette/di/commit/5b7fe4e0ae68d9f6bdf625ba26b35ef7a0f5cbbc

In latest stable and maybe even master version it works fine, it looks like 3.2 branch has this bug.

rixafy avatar May 31 '25 12:05 rixafy

It was caused by this commit https://github.com/nette/di/commit/a5812de601c44f66bdea81fbeeaa85ba633962f6, but I don't know why exactly yet, but when reverted, it works.

rixafy avatar Jun 13 '25 19:06 rixafy

I moved the commits to the new 3.3 branch, so 3.2 should be ok.

dg avatar Jun 18 '25 18:06 dg

Ok, I changed target of https://github.com/nette/di/pull/321 to v3.3, since it's a new feature, and I would like to test it on some projects but I'm unable to run v3.3 because of this error, so far I had no luck figuring out why exactly is it happening, it seems that mentioned commit also changed some code logic, maybe complete and normalize are called in different order in some cases.

rixafy avatar Jun 20 '25 16:06 rixafy