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

This comment has been minimized.

Copy link

@PavelJurasek PavelJurasek commented Oct 17, 2019

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

@MartkCz

This comment has been minimized.

Copy link
Contributor Author

@MartkCz 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

This comment has been minimized.

Copy link
Contributor

@mabar 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

This comment has been minimized.

Copy link
Contributor Author

@MartkCz MartkCz commented Oct 18, 2019

Not working:

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

Working:

factory: App\TestService
inject: true
@dg dg closed this in 9af1874 Oct 22, 2019
dg added a commit that referenced this issue Oct 31, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.