0

I've used the Symfony route bundle and DI bundle in my non Symfony project. I inject dependencies to the controllers/actions by the code below:

    // Find the current route
    $parameters = $router->match($requestContext->getPathInfo());

    $parts = explode(':', $parameters['_controller']);

    $controller = $parts[0];
    $action = $parts[2];

    $arguments = array();
    $reflectionMethod = new ReflectionMethod($controller, $action);
    foreach( $reflectionMethod->getParameters() as $arg ) {
    if( isset($parameters[$arg->name]) ) {
        $arguments[$arg->name] = $parameters[$arg->name];
    }
    else if( $arg->isDefaultValueAvailable() ) {
        $arguments[$arg->name] = $arg->getDefaultValue();
    }
    else {
        echo $arg->getType();
        $arguments[$arg->name] = null;
    }
}

call_user_func_array( array($controller, $action) , $arguments );

My problem is that the code just finds the name of the arguments and not the classes. My question is how should I connect the founded argument to the registered service in the container based on the class type? and another problem is how can I realize if the argument is matched with a container's parameter(which is a class object)?

Fatemeh Gharri
  • 369
  • 2
  • 6
  • 20
  • You can use `ReflectionParameter->getType()` instead of `->name`. You have to typehint your parameters to do that. – thehennyy Sep 21 '20 at 13:22
  • @thehennyy Yes, but what about container's parameters? – Fatemeh Gharri Sep 21 '20 at 13:24
  • I can not help with your specific setup, I did not use that yet. But in general dependecies in the dependency injection container should be registered with their type as keys. So the connection is parameter-type to service-type. – thehennyy Sep 21 '20 at 13:28

0 Answers0