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

Any way to use wildcards via a factory/callback and create an instance from the container? #797

Open
defunctl opened this issue Oct 19, 2021 · 2 comments

Comments

@defunctl
Copy link

defunctl commented Oct 19, 2021

Is something like this at all possible without creating the instance manually e.g. new $class( ... and having to manually build all the instance dependencies when I just need to dynamically define one?

The example definitions below would of course return an instance of the AutowireDefinitionHelper and not the real class instance.

Imagine later we call $container->get( My\Namespace\Fun\Things\Controller ); I'd like to tell PHP-DI that when the controller needs a concrete of say, DifferentClassInterface, that it would inject My\Namespace\Fun\Things\DifferentClass.

Example definitions:

return [
	'My\Namespace\*\*\Controller' => static function( DI\Factory\RequestedEntry $r ): ControllerInterface {
		$customDepdendency = DI\create( 'My\Namespace\*\*\DifferentClass' );

		// or? $customDepdendency = DI\get( 'My\Namespace\*\*\DifferentClass' );
		
		$class = $r->getName();

                // Imagine $class has 4 or 5 dependencies that can be resolved via autowiring
		return DI\autowire( $class )->constructorParameter( 'customDepdendency', $customDepdendency );
	},
];
@SvenRtbg
Copy link
Contributor

It seems like you have a certain 1:1 mapping from a X\Controller requiring the proper X\DifferentClass. Unless you can provider a better explanation of why you'd want what you want, I'd suggest that simply requiring X\DifferentClass as a controller constructor parameter will easily be autowired out of the box.

@mnapoli
Copy link
Member

mnapoli commented Oct 23, 2021

I don't understand the context too I think. Isn't what the DI\Factory\RequestedEntry parameter meant for?

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