4

Silex PHP microframework does a callback injection based on the automatic type hinting. For example, in Silex one can supply a Closure argument with arbitrary arguments like so:

$app->get('/blog/show/{postId}/{commentId}', function ($commentId, $postId) {
    //...
});
$app->get('/blog/show/{id}', function (Application $app, Request $request, $id) {
    //...
});
// following works just as well - order of arguments is not important
$app->get('/blog/show/{id}', function (Request $request, Application $app, $id) {
    //...
});

How do I do this? I'm not interested in getting parameter types as strings. I am looking for a "no strings" fully automatic solution. In other words,

  1. For a number of possible arguments:

    $possible_arguments = [
        new Class_A(), 
        new Class_B(), 
        new Class_C(), 
        new Another_Class, 
        $some_class
    ];
    
  2. For a closure with any number of arbitrary arguments, that can only include those defined above only once:

    $closure = function (Class_B $b, Another_Class, $a) {
        // Do something with $a and $b
    };
    
  3. I need to get only matching arguments in order to call the closure with them:

    // $arguments is now [$possible_arguments[1], $possible_arguments[3]]
    call_user_func_array($closure, $arguments);
    
Cœur
  • 37,241
  • 25
  • 195
  • 267
sanmai
  • 29,083
  • 12
  • 64
  • 76
  • 3
    They will be using [Reflection](http://php.net/manual/en/book.reflection.php). A closure is converted to an instance of the [Closure](http://php.net/manual/en/class.closure.php) class internally, reflection works on it just like any other object. You should be able to get the closure's argument list by inspecting the object's `__invoke()` method. – DaveRandom Dec 20 '12 at 08:42

2 Answers2

5

My guess would be using reflection.

http://php.net/manual/en/class.reflectionparameter.php

Super simple example:

function pre($var)
{
    echo '<pre>' . var_export($var, true) . '</pre>';
}

interface TestInterface
{
}

class TestClass
{
    public function __construct(TestInterface $testArg)
    {
    }
}

function TestFunc(TestInterface $testArg)
{
}

// for a class...

$className = 'TestClass';
$methodName = '__construct';

$argNumber = 0;

$ref = new ReflectionParameter([$className, $methodName], $argNumber);

pre($ref);
pre($ref->getClass());



// for a function...
$funcName = 'TestFunc';
$argNumber = 0;

$ref = new ReflectionParameter($funcName, $argNumber);

pre($ref);
pre($ref->getClass());

Another question I found on stackoverflow might be a better answer to your question: PHP Reflection - Get Method Parameter Type As String

Community
  • 1
  • 1
Supericy
  • 5,866
  • 1
  • 21
  • 25
3

Indeed they're using Reflection, specifically in the ControllerResolver class.

If you let me to simplify to include only one case of Closure and only object-type arguments:

$availableArguments = array($a, new B(), $c);
$arguments = array();
$reflection = new \ReflectionFunction($callback);  // $callback is a Closure
foreach ($reflection->getParameters() as $param) {
    if ($paramClass = $param->getClass()) {
        foreach ($availableArguments as $proposedArgument) {
            if ($paramClass->isInstance($proposedArgument)) {
                $arguments[] = $proposedArgument;
                break;
            }
        }
    }
}
call_user_func_array($callback, $arguments);
sanmai
  • 29,083
  • 12
  • 64
  • 76