Skip to content

InjectExtension scans return type from factory instead of specified type for inject* methods #234

@KacerCZ

Description

@KacerCZ

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().

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions