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

InjectExtension not work when set factory and type #218

Closed
MartkCz opened this issue Oct 17, 2019 · 4 comments
Closed

InjectExtension not work when set factory and type #218

MartkCz opened this issue Oct 17, 2019 · 4 comments

Comments

@MartkCz
Copy link
Contributor

MartkCz commented Oct 17, 2019

Version: 3.0.1

$definitions[$name] = $builder->addDefinition($this->prefix('controller.' . $name))
				->setType(IController::class)
				->addTag(InjectExtension::TAG_INJECT)
				->setFactory($controller);

InjectExtension search inject* methods in IController instead of $controller

https://github.com/nette/di/blob/master/src/DI/Extensions/InjectExtension.php#L64

Is it bug or do I have to remove interface as type?

@PavelJurasek
Copy link

I guess you should use addFactoryDefinition and then ->getResultDefition()->setType(...).

@MartkCz
Copy link
Contributor Author

MartkCz commented Oct 17, 2019

Thanks,

but IController is not a factory, I guess addFactoryDefinition is only for create* methods that returns new object of specific type

IController:

interface IController {

	public function start(IRequest $request, IClient $client): void;
	
}

@mabar
Copy link
Contributor

mabar commented Oct 17, 2019

I guess you should use addFactoryDefinition and then ->getResultDefition()->setType(...).

FactoryDefinition is for factories generated from interface, while setFactory() define how is instance created (@foo::getInstance() for $this->getService('foo')->getInstance() or Foo::class for new Foo), these things are not related.

InjectExtension not work when set factory and type

If you put definition into neon with factory and type keys defined - is it working? Imho it's problem with priorities, not with extension itself.

@MartkCz
Copy link
Contributor Author

MartkCz commented Oct 18, 2019

Not working:

factory: App\TestService
type: App\ITestService
inject: true

Working:

factory: App\TestService
inject: true

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

No branches or pull requests

3 participants