Skip to content

[3.1] [WebProfilerBundle] [DX] Feature allow forward and redirection detection in wdt #17589

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

Merged
merged 4 commits into from
Mar 31, 2016

Conversation

HeahDude
Copy link
Contributor

Q A
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets #14358, #17501
License MIT
Doc PR ?

This PR allows to :

  • track explicit forward from \Symfony\Bundle\FrameWorkBundle\Controller\Controller in the web debug toolbar.
  • or pass a request attribute _forwarded with the current request attributes (an instance of ParameterBag) as value to your sub request before handling it.
  • see if you've been redirected (require session enabled)

When redirected you will see the name of the route (if any) and a link to the profile of the original request.

redirect

In case of forwarding, the name of the controller is a file link and next to it there is a direct link to the profile of the sub request.

forward

This works pretty well in Silex too by registering SessionServiceProvider() for redirections or by providing this method for forwarding :

class App extends \Silex\Application
//  (php7 bootstrap) $app = new class extends \Silex\Application {
{
    public function forward($controller, array $path = array(), array $query = array()
    {
        if (!$this->booted) {
            throw new LogicException(sprintf('Method %s must be called from a controller.', __METHOD__));
        }

        $this->flush();

        $request = $this['request_stack']->getCurrentRequest();
        $path['_forwarded'] = $request->attributes;
        $path['_controller'] = $controller;
        $subRequest = $request->duplicate($query, null, $path);

        return $this['kernel']->handle($subRequest, HttpKernelInterface::SUB_REQUEST);
    }
}

* Gets the parsed forward controller.
*
* @return array|bool The parsed forward controller as an array of data
* with keys 'token' the forward profile token, and
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's nice to have good comments but I think you are revealing too many implementation details in this one.

@@ -64,8 +64,10 @@ protected function generateUrl($route, $parameters = array(), $referenceType = U
*/
protected function forward($controller, array $path = array(), array $query = array())
{
$request = $this->container->get('request_stack')->getCurrentRequest();
$path['_forwarded'] = $request;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think passing the whole parent request is a good idea. It adds a bunch of unrelated info in the subrequest attributes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stof It appears to me as the simplest way to keep the forward trace, fortunately it's just an object reference, any suggestion ? maybe pass only the attributes bag ?

@HeahDude HeahDude changed the title [WIP] [WDT] [HttpKernel] [WebProfilerBundle] Feature allow forward detection from controller in wdt [WIP] [WebProfilerBundle] Feature allow forward and redirection detection in wdt Jan 29, 2016
@HeahDude
Copy link
Contributor Author

Made some changes to allow catch redirections as well, and use only the master request attributes (ParameterBag) instead of the whole request to handle forward interception as @stof suggested.
Also updated title and description.
Now need some reviews, thanks.

@HeahDude
Copy link
Contributor Author

I wonder if it would be useful to add the redirection code to quickly see if it's the same as expected

@javiereguiluz
Copy link
Member

The designer just sent me the icons which match the style of the toolbar:

redirect.svg

<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"><path d="M23.06,7.83L14,0.38a1.25,1.25,0,0,0-2,.89V4.09a13.61,13.61,0,0,1-2.2.61l-1.3.47C8,5.35,7.59,5.6,7.12,5.81l-0.69.35-0.72.45a10.62,10.62,0,0,0-1.41,1A13.22,13.22,0,0,0,3,8.82a15.31,15.31,0,0,0-1.13,1.46A17.63,17.63,0,0,0,1,11.93c-0.18.58-.34,1.16-0.48,1.71S0.45,14.76.43,15.29a10.2,10.2,0,0,0,.16,1.5,5.72,5.72,0,0,0,.33,1.34c0.14,0.41.26,0.82,0.42,1.19,0.37,0.71.67,1.38,1,1.94l1,1.46c0.32,0.41.63,0.75,0.87,1s0.51,0.09.43-.22-0.23-.75-0.35-1.23L4,20.69c-0.1-.58-0.09-1.22-0.14-1.86,0-.32.05-0.65,0.08-1a3.44,3.44,0,0,1,.16-1A6.44,6.44,0,0,1,4.41,16l0.41-.8c0.2-.22.38-0.44,0.55-0.65L6,14c0.23-.14.5-0.24,0.72-0.37a7.52,7.52,0,0,1,.79-0.25,4.48,4.48,0,0,1,.84-0.15l0.41-.06H9.22c0.3,0,.56,0,0.85,0l0.72,0.07a3.77,3.77,0,0,1,1.2.21v3.17a1.25,1.25,0,0,0,2,.89l9-7.45A1.46,1.46,0,0,0,23.06,7.83Z" style="fill:#aaa"/></svg>

forward.svg

<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"><path d="M23.61,11.07L17.07,4.35A1.2,1.2,0,0,0,15,5.28V9H1.4A1.82,1.82,0,0,0,0,10.82v2.61A1.55,1.55,0,0,0,1.4,15H15v3.72a1.2,1.2,0,0,0,2.07.93l6.63-6.72A1.32,1.32,0,0,0,23.61,11.07Z" style="fill:#aaa"/></svg>

@HeahDude
Copy link
Contributor Author

HeahDude commented Feb 1, 2016

Thanks @javiereguiluz, committed and updated description screenshots.

if ($request->hasSession() && $response->isRedirect()) {
$request->getSession()->set('sf_redirect', array(
'token' => $response->headers->get('x-debug-token'),
'route' => $request->attributes->get('_route', 'n/a'),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about collecting controller too and making the route in wdt info panel a link to it ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could also catch here the redirection code if we want to display it in wdt.

@HeahDude
Copy link
Contributor Author

HeahDude commented Feb 1, 2016

@javiereguiluz WDYT of a redirect arrow pointing left instead of right, it would be less confusing and consistent with mail buttons definition for example.

Here's a screenshot from Mail app in OS X :

mail

@javiereguiluz
Copy link
Member

@HeahDude I don't agree with the last proposal. The reason is that our toolbar displays "redirected to" and the mail app shows "redirected/replied from". The tip of the arrow should point to the route name because that route is the result of the redirection or forward.

  • 200 -> @route can be read as "a 200 response redirected to @route".
  • 200 <- @route can be read as "@route redirects to 200".

@HeahDude
Copy link
Contributor Author

HeahDude commented Feb 2, 2016

@javiereguiluz alright. WDYT about collecting the redirected controller and/or redirect status code ? (see my notes)

@javiereguiluz javiereguiluz modified the milestone: 3.1 Feb 2, 2016
@HeahDude HeahDude force-pushed the feature-wdt_forward_detection branch from b6f795f to 411dd48 Compare February 15, 2016 00:47
@HeahDude HeahDude changed the title [WIP] [WebProfilerBundle] Feature allow forward and redirection detection in wdt [WebProfilerBundle] Feature allow forward and redirection detection in wdt Feb 15, 2016
@HeahDude
Copy link
Contributor Author

Hi @javiereguiluz, do I have to write some tests for this PR ?

Otherwise it is finished, I'm just waiting for some feedbacks on catching the redirection status code, and/or the route name which we've been redirected from.

Any lead ?

@@ -64,8 +64,10 @@ protected function generateUrl($route, $parameters = array(), $referenceType = U
*/
protected function forward($controller, array $path = array(), array $query = array())
{
$request = $this->container->get('request_stack')->getCurrentRequest();
$path['_forwarded'] = $request->attributes;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be moved to an event listener instead. Otherwise you couldn't track subrequests triggered by user code that doesn't use the base controller class.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@xabbuh, the point is not to catch subrequests as profiler already does, we can see them in the request panel.
This feature is about catching the "main" forward", using this special method, as it should be used only once in most use case to quickly click the profile token link of the original request in the wdt.

But I can try to go further, how would you imagine the presentation of sub requests token in the wdt ?

@xabbuh
Copy link
Member

xabbuh commented Feb 15, 2016

What when there is more than one redirection before the WDT is shown again?

@HeahDude
Copy link
Contributor Author

@xabbuh for chained redirection see my comment above but I could add a link in the top panel of the profiler for the case where intercept_redirect is false.

@HeahDude HeahDude changed the title [WebProfilerBundle] Feature allow forward and redirection detection in wdt [WebProfilerBundle] [DX] Feature allow forward and redirection detection in wdt Feb 15, 2016
@HeahDude
Copy link
Contributor Author

So in that it could worth to catch controller and route info from the redirection response

@HeahDude
Copy link
Contributor Author

:) So there is no squashing needed here ? 4f020b5 could be "fixup" in 227ac77 if we intend to keep it.

@fabpot
Copy link
Member

fabpot commented Mar 31, 2016

@HeahDude If you want to rearrange the commits before merge, please do so.

@HeahDude
Copy link
Contributor Author

@fabpot The question is, is it needed to keep them separately ?

@fabpot
Copy link
Member

fabpot commented Mar 31, 2016

I don't mind keeping several commits if they make sense and it seems they do :)

@HeahDude
Copy link
Contributor Author

Agreed :)

So we're done here 🎉

@fabpot
Copy link
Member

fabpot commented Mar 31, 2016

Thank you @HeahDude.

@fabpot fabpot merged commit 0a0e8af into symfony:master Mar 31, 2016
fabpot added a commit that referenced this pull request Mar 31, 2016
…nd redirection detection in wdt (HeahDude)

This PR was merged into the 3.1-dev branch.

Discussion
----------

[3.1] [WebProfilerBundle] [DX] Feature allow forward and redirection detection in wdt

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #14358, #17501
| License       | MIT
| Doc PR        | ?

This PR allows to :
- track explicit forward from `\Symfony\Bundle\FrameWorkBundle\Controller\Controller` in the web debug toolbar.
- or pass a request attribute `_forwarded` with the current request attributes (an instance of `ParameterBag`) as value to your sub request before handling it.
- see if you've been redirected (require session enabled)

When redirected you will see the name of the route (if any) and a link to the profile of the original request.

![redirect](https://cloud.githubusercontent.com/assets/10107633/12716952/9aacdcba-c8e4-11e5-9a64-d26fe27f1cae.jpg)

In case of forwarding, the name of the controller is a file link and next to it there is a direct link to the profile of the sub request.

![forward](https://cloud.githubusercontent.com/assets/10107633/12716968/ba6b1fbc-c8e4-11e5-85fc-7f71969cb372.jpg)

This works pretty well in __Silex__ too by registering `SessionServiceProvider()` for redirections or by providing this method for forwarding :

```php
class App extends \Silex\Application
//  (php7 bootstrap) $app = new class extends \Silex\Application {
{
    public function forward($controller, array $path = array(), array $query = array()
    {
        if (!$this->booted) {
            throw new LogicException(sprintf('Method %s must be called from a controller.', __METHOD__));
        }

        $this->flush();

        $request = $this['request_stack']->getCurrentRequest();
        $path['_forwarded'] = $request->attributes;
        $path['_controller'] = $controller;
        $subRequest = $request->duplicate($query, null, $path);

        return $this['kernel']->handle($subRequest, HttpKernelInterface::SUB_REQUEST);
    }
}
```

Commits
-------

0a0e8af [WebProfilerBundle] show the http method in wdt if not 'GET'
4f020b5 [FrameworkBundle] Extends the RequestDataCollector
227ac77 [WebProfilerBundle] [FrameworkBundle] profile forward controller action
0a1b284 [WebProfiler] [HttpKernel] profile redirections
@HeahDude
Copy link
Contributor Author

I would like to thank you and all the community for sharing symfony :)

I've learned so much thanks to you all since I use it, merci!

HeahDude added a commit to HeahDude/symfony that referenced this pull request Apr 2, 2016
@rvanlaak
Copy link
Contributor

rvanlaak commented Apr 3, 2016

Missed the development process of this feature, but it looks awesome @HeahDude ! 🚀

HeahDude added a commit to HeahDude/symfony that referenced this pull request Apr 3, 2016
HeahDude added a commit to HeahDude/symfony that referenced this pull request Apr 3, 2016
HeahDude added a commit to HeahDude/symfony that referenced this pull request Apr 6, 2016
fabpot added a commit that referenced this pull request Apr 7, 2016
This PR was merged into the 3.1-dev branch.

Discussion
----------

changelogs missing features after #17589

| Q             | A
| ------------- | ---
| Branch?       | master
| License       | MIT

Commits
-------

724fd3b updated changelogs after #17589
fabpot added a commit that referenced this pull request Apr 28, 2016
…ollector (HeahDude)

This PR was merged into the 3.1-dev branch.

Discussion
----------

[HttpKernel] tweaked redirection profiling in RequestDataCollector

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

- c8ba3b2 removes duplicated code forgotten in #17589

- a47d2e8 fixes the collecting of redirect data in first sub request instead of redirected master request.

Commits
-------

df19c14 use a request attribute flag for redirection profile
b26cb6d [HttpKernel] added RequestDataCollector::onKernelResponse()
c8ba3b2 [HttpKernel] remove legacy duplicated code
@fabpot fabpot mentioned this pull request May 13, 2016
wouterj added a commit to symfony/symfony-docs that referenced this pull request May 21, 2016
This PR was merged into the 3.1 branch.

Discussion
----------

[Reference] change namespace to point to new class

see symfony/symfony#17589 and #6568 (comment)

Commits
-------

10cf8d6 change namespace to point to new class
fabpot added a commit to silexphp/Silex-WebProfiler that referenced this pull request Jun 15, 2016
This PR was squashed before being merged into the 2.0.x-dev branch (closes #91).

Discussion
----------

Allow symfony 3.1

As I was updating my skeleton app, I found out the webprofiler did not support symfony 3.1 in composer.json.
So I tried to update it.

Everything works great, except for the web-profiler-bundle.
This PR broke it: symfony/symfony#17589

I don't know the best way to fix that. Maybe you have an idea? (I'm not that familiar with the silex and symfony development)

Commits
-------

0a09d2a Allow symfony 3.1
fabpot added a commit that referenced this pull request Oct 22, 2016
…(jakzal)

This PR was merged into the 3.1 branch.

Discussion
----------

[HttpKernel] Fix a regression in the RequestDataCollector

| Q             | A
| ------------- | ---
| Branch?       | 3.1
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #19701
| License       | MIT
| Doc PR        | -

The regression was introduced by refactoring made as part of #17589 (if/else statements where rearranged).

Commits
-------

57008ea [HttpKernel] Fix a regression in the RequestDataCollector
26b90e4 [HttpKernel] Refactor a RequestDataCollector test case to use a data provider
ostrolucky pushed a commit to ostrolucky/symfony that referenced this pull request Mar 25, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants