How handle router with container's services/parameters dynamically?

63 Views Asked by At

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)?

0

There are 0 best solutions below