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

InjectorDependence #33

Open
mj4444ru opened this issue Nov 13, 2020 · 2 comments
Open

InjectorDependence #33

mj4444ru opened this issue Nov 13, 2020 · 2 comments

Comments

@mj4444ru
Copy link

mj4444ru commented Nov 13, 2020

$injector->addDependenceInjector(
    new DependenceInjector()
        ->withParamClass(TestInterface::class)
        ->withParamName('testA')
        ->referenceTo(TestA::class)
);

$injector->addDependenceInjector(
    new DependenceInjector()
        ->withClass(MethodСlass::class)
        ->withParamClass(TestInterface::class)
        ->referenceTo(TestB::class)
);

$injector->addDependenceInjector(
    new DependenceInjector()
        ->withClass(MethodСlass::class)
        ->withMethod('actionTest')
        ->withParamName('testA')
        ->referenceTo('containerID')
);

// The following code is just for example. I believe that you need to make
// dependencies not by number, but by parameter name.
$injector->addDependenceInjector(
    new DependenceInjector()
        ->withClass(MethodСlass::class)
        ->withMethod('__construct')
        ->withParamPos(1)
        ->referenceTo('containerID')
);

// Not singleton
$injector->addDependenceInjector(
    new DependenceInjector()
        ->withParamClass(TestInterface::class)
        ->handler(function(ContainerInterface $container, Param $param) {
            $paramClass = $param->getParamClass();
            $paramName = $param->getParamName();
            $paramPos = $param->getParamPos();
            $methodClass = $param->getMethodClass();
            $methodName = $param->getMethodName();
            ...
            // We can return the singleton from the container.
            // We can create an object and place it in a container for reuse.
    })
);

$defaultDependenceInjectors = [
    new DependenceInjector()->handler(function(ContainerInterface $container, Param $param) {
        return $container->get($param->getParamClass());
    }),
    new DependenceInjector()->handler(function(ContainerInterface $container, Param $param) {
        return $container->get($param->getParamClass() . '$' . $param->getParamName());
    }),
];
@roxblnfk
Copy link
Member

Will the Injector become too complex and confusing? Not only in terms of code, but also in terms of usage. This can also affect performance.

Maybe you should think not in the direction of configuration, but in the direction of reassigning the resolveParemeter handler?

@mj4444ru
Copy link
Author

Will the Injector become too complex and confusing? Not only in terms of code, but also in terms of usage.

This can provide additional functionality to external modules. We will be able to query objects that do not live without context (for example, a database or a database table).

This can also affect performance.

I think it is necessary to impose some restrictions, such as the required parameter ParamClass or Class and create indexes when added. Thus, we will simplify calculations when looking for dependencies and the additional functionality will not affect performance.

Maybe you should think not in the direction of configuration, but in the direction of reassigning the resolveParemeter handler?

How do you imagine that?

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

No branches or pull requests

3 participants