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 scans return type from factory instead of specified type for inject* methods #234

KacerCZ opened this issue Mar 18, 2020 · 1 comment


Copy link

KacerCZ commented Mar 18, 2020

Version: 3.0.3

Bug Description

When service is created and both factory and type is given then return type from factory is scanned instead of set type.

Steps To Reproduce

Simplified code just to illustrate dependencies between interface and classes.

There is interface for all repositories:

interface Repository {}

Concrete repository implementing interface:

class ArticleRepository implements Repository {
	public function injectService( Service $service ) {}

Factory for repositories implemented like this

class RepositoryFactory {
	public function createRepository( string $table ): Repository {
		if ( $table === 'articles' ) {
			return new ArticleRepository();
		// Other repositores are created here ...

Register service in extension

$builder->addDefinition( NULL )
	->setType( ArticleRepository::class )
	->setFactory( '@RepositoryFactory::createRepository', [ 'articles ] )
	->addTag( InjectExtension::TAG_INJECT );

Current Behavior

Then InjectExtension uses return type of RepositoryFactory::createRepository() (interface Repository) instead of type given in setType() from service definition (class ArticleRepository). So method ArticleRepository::injectService() is not found and added to generated DI container.

Expected Behavior

Expected result is that type from service definition should have priority over return type from factory.
With current state there is no way to override detected value.

Possible Solution

Problem is related to fix of issue #218.
When nette/di 3.0.1 is used then InjectExtension behaves as expected.
Another workaround is removal of return type from RepositoryFactory::createRepository().

@dg dg closed this as completed in 86eaa7f May 8, 2020
Copy link

KacerCZ commented May 18, 2020

Tested fix in version 3.0.4 and works as expected.
Thank you.

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

No branches or pull requests

1 participant