Skip to content
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

[RTM] Support for replaying headers #749

Merged
merged 14 commits into from
Jun 13, 2017
Merged

[RTM] Support for replaying headers #749

merged 14 commits into from
Jun 13, 2017

Conversation

Toflar
Copy link
Member

@Toflar Toflar commented Apr 1, 2017

Implements our terminal42/header-replay-bundle to support "splitting up" user context provided by cookies (or authorization header or whatever you like) into whatever headers you like that are then replayed as master request onto the application so you can vary on them. Please read the whole README of the header-replay-bundle to understand the concept.

  • Brings back the support for the mobile page layout because we can now Vary on it
  • Always show the "live" content when logged in

ToDo's

  • Unit tests for the two listeners
  • Wait for the FOSHttpCache and FOSHttpCacheBundle to be released as 2.0.0 stable so I can release terminal42/header-replay-bundle 1.0.0 stable too

Adjust your AppCache according to the README. PR for the Managed Edition: contao/manager-bundle#26

composer.json Outdated
@@ -122,5 +124,7 @@
"dev-develop": "4.4.x-dev"
},
"contao-manager-plugin": "Contao\\CoreBundle\\ContaoManager\\Plugin"
}
},
"minimum-stability": "dev",
Copy link
Member

Choose a reason for hiding this comment

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

This should not be necessary if you suffix the requirement with @dev. If that's an issue with an included package, simply require it in the root as well (with @dev).

}

$headers = $event->getHeaders();
$headers->set('Contao-Page-Layout', $mobile ? 'mobile' : 'desktop');
Copy link
Member

Choose a reason for hiding this comment

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

I would suggest to call this Contao-Mobile-Layout with true only value or the header not being present. Also, creating a $headers variable is fairly unnecessary here, but I guess you're aware of that. Just a matter of choice :D

Copy link
Member Author

Choose a reason for hiding this comment

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

I like my version better. This applies to both, the header name and the variable.

Copy link
Member

Choose a reason for hiding this comment

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

The idea was that the header would not need to be present for 90% of pages (if not in mobile mode). Also, the Vary would only be needed if the current page actually has a mobile layout.

Copy link
Contributor

Choose a reason for hiding this comment

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

We could introduce new layout types using the same header with @Toflar's variant. Therefore I am also in favor of his approach.

arguments:
- "%contao.security.disable_ip_check%"
tags:
- { name: kernel.event_listener, event: t42.header_replay, method: onReplay }
Copy link
Member

Choose a reason for hiding this comment

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

as far as I remember Symfony events, the correct way to name the method would be according to the event name. So t42.header_replay would result in an onHeaderReplay method. Also, if you follow that convention, you do not need to name the method (it will be discovered automatically).

On a side note, I think the event should be called terminal42.header_replay to not introduce another "namespace".

Copy link
Member Author

Choose a reason for hiding this comment

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

This would look shit, which is why I decided not to do it. onTerminal42HeaderReplay() is not a nice method name at all.

Copy link
Member

Choose a reason for hiding this comment

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

it would be onHeaderReplay. The key is not used. Think of kernel.response => onResponse

Copy link
Member Author

Choose a reason for hiding this comment

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

No, it's onKernelResponse.

@Toflar
Copy link
Member Author

Toflar commented Apr 2, 2017 via email

@leofeyer leofeyer added this to the 4.4.0 milestone Apr 5, 2017
@leofeyer leofeyer force-pushed the develop branch 6 times, most recently from cd4aa42 to 51a60f7 Compare May 12, 2017 11:08
@leofeyer leofeyer force-pushed the develop branch 3 times, most recently from cf5c60f to fbee6c7 Compare May 19, 2017 14:57
@Toflar Toflar changed the base branch from develop to release/4.4.0 June 12, 2017 11:54
@Toflar Toflar changed the title [WIP] Support for replaying headers [RTM] Support for replaying headers Jun 12, 2017
@leofeyer leofeyer requested a review from ausi June 12, 2017 14:49
@@ -43,7 +44,7 @@ class Plugin implements BundlePluginInterface, RoutingPluginInterface
public function getBundles(ParserInterface $parser)
{
return [
BundleConfig::create(KnpMenuBundle::class),
Copy link
Member

Choose a reason for hiding this comment

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

sure this is correct?

Copy link
Member Author

Choose a reason for hiding this comment

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

Nope, this is a rebase-bug! Fixed in 9fdb93e.

{
$request = $event->getRequest();

$this->framework->initialize();
Copy link
Member

Choose a reason for hiding this comment

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

pretty sure we should check for a Contao scope before booting the framework on EVERY request?

Copy link
Member Author

Choose a reason for hiding this comment

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

On every request with a Cookie or Authorization header, yeah. We could do that? How do I do this now?

Copy link
Member

Choose a reason for hiding this comment

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

No I mean only for routes with _scope=frontend.

*/
public function onReplay(HeaderReplayEvent $event)
{
$request = $event->getRequest();
Copy link
Member

Choose a reason for hiding this comment

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

Same applies here. The event listener should only be triggered on $request->attributes->get('_scope') === 'backend' (actually use the constant!)

@Toflar
Copy link
Member Author

Toflar commented Jun 12, 2017

Scope checks implemented.

Copy link
Member

@leofeyer leofeyer left a comment

Choose a reason for hiding this comment

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

Besides my two comments and a lot of missing phpDocs the PR looks good to me. 😄

case 'desktop':
$mobile = false;
}
}
Copy link
Member

Choose a reason for hiding this comment

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

We could avoid initializing the framework if there is a TL_VIEW cookie:

if ($request->cookies->has('TL_VIEW')) {
    $mobile = 'mobile' === $request->cookies->get('TL_VIEW');
} else {
    $this->framework->initialize();
    $mobile = $this->framework->getAdapter(Environment::class)->get('agent')->mobile;
}

// TODO: Add support for proxies so they can vary on member context and page layout
if (FE_USER_LOGGED_IN === true || BE_USER_LOGGED_IN === true || $objPage->isMobile || $objPage->protected || $this->hasAuthenticatedBackendUser())
// Vary on page layout
$response->setVary(['Contao-Page-Layout'], false);
Copy link
Member

Choose a reason for hiding this comment

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

This is legacy code, so we must not use the short array notation.

@Toflar
Copy link
Member Author

Toflar commented Jun 13, 2017

@leofeyer comments addressed.

@leofeyer leofeyer merged commit b21599c into contao:release/4.4.0 Jun 13, 2017
@Toflar Toflar deleted the feature/replay-headers branch June 13, 2017 12:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants