bug #12443 [HttpKernel][2.6] Adding support for invokable controllers in the RequestDataCollector (jameshalsall)

This PR was submitted for the master branch but it was merged into the 2.5 branch instead (closes #12443).

Discussion
----------

[HttpKernel][2.6] Adding support for invokable controllers in the RequestDataCollector

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | N/A
| License       | MIT
| Doc PR        | N/A

As part of https://github.com/symfony/symfony/pull/11193 support for controllers using `__invoke()` was added.

The `RequestDataCollector` did not support controllers that were defined in the routing as...

```php
route_name:
    path: /{id}
    defaults: { _controller: acme_app.page.controller.page }
    requirements:
        id: \d+
```

Where the controller was defined as...

```php
class PageController
{
    public function __invoke()
    {
        //
    }
}
```

This PR adds that support. Tests have been updated.

Commits
-------

f1d043a [HttpKernel][2.6] Adding support for invokable controllers in the RequestDataCollector
This commit is contained in:
Fabien Potencier 2014-11-12 09:14:41 +01:00
commit af4f95936d
2 changed files with 25 additions and 0 deletions

View File

@ -147,6 +147,14 @@ class RequestDataCollector extends DataCollector implements EventSubscriberInter
'file' => $r->getFilename(),
'line' => $r->getStartLine(),
);
} elseif (is_object($controller)) {
$r = new \ReflectionClass($controller);
$this->data['controller'] = array(
'class' => $r->getName(),
'method' => null,
'file' => $r->getFileName(),
'line' => $r->getStartLine(),
);
} else {
$this->data['controller'] = (string) $controller ?: 'n/a';
}

View File

@ -59,6 +59,7 @@ class RequestDataCollectorTest extends \PHPUnit_Framework_TestCase
// make sure we always match the line number
$r1 = new \ReflectionMethod($this, 'testControllerInspection');
$r2 = new \ReflectionMethod($this, 'staticControllerMethod');
$r3 = new \ReflectionClass($this);
// test name, callable, expected
$controllerTests = array(
array(
@ -132,6 +133,17 @@ class RequestDataCollectorTest extends \PHPUnit_Framework_TestCase
'line' => 'n/a',
),
),
array(
'Invokable controller',
$this,
array(
'class' => 'Symfony\Component\HttpKernel\Tests\DataCollector\RequestDataCollectorTest',
'method' => null,
'file' => __FILE__,
'line' => $r3->getStartLine(),
),
),
);
$c = new RequestDataCollector();
@ -202,4 +214,9 @@ class RequestDataCollectorTest extends \PHPUnit_Framework_TestCase
{
throw new \LogicException('Unexpected method call');
}
public function __invoke()
{
throw new \LogicException('Unexpected method call');
}
}