-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[Asset] Add support for preloading with links and HTTP/2 push #21478
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
Changes from 1 commit
64bd530
f920ca4
1de19cc
c5ca4bc
639d7d1
9c11a0f
d39b4f1
581c700
693107c
2d8d750
0b7fe14
dffb7b6
18412e5
78d7b6a
05134b6
a7f77b4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,28 +11,28 @@ | |
|
||
namespace Symfony\Component\Asset\EventListener; | ||
|
||
use Symfony\Component\Asset\Preload\HttpFoundationPreloadManager; | ||
use Symfony\Component\Asset\Preload\PreloadManager; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
use Symfony\Component\HttpKernel\Event\FilterResponseEvent; | ||
use Symfony\Component\HttpKernel\KernelEvents; | ||
|
||
/** | ||
* Adds preload's Link HTTP headers to the response. | ||
* Adds the preload Link HTTP header to the response. | ||
* | ||
* @author Kévin Dunglas <dunglas@gmail.com> | ||
*/ | ||
class PreloadListener implements EventSubscriberInterface | ||
{ | ||
private $preloadManager; | ||
|
||
public function __construct(HttpFoundationPreloadManager $preloadManager) | ||
public function __construct(PreloadManager $preloadManager) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the typehint must use the interface |
||
{ | ||
$this->preloadManager = $preloadManager; | ||
} | ||
|
||
public function onKernelResponse(FilterResponseEvent $event) | ||
{ | ||
$this->preloadManager->setLinkHeader($event->getResponse()); | ||
$event->getResponse()->headers->set('Link', $this->preloadManager->getLinkValue()); | ||
|
||
// Free memory | ||
$this->preloadManager->setResources(array()); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,14 +12,13 @@ | |
namespace Symfony\Component\Asset\Preload; | ||
|
||
use Symfony\Component\Asset\Exception\InvalidArgumentException; | ||
use Symfony\Component\HttpFoundation\Response; | ||
|
||
/** | ||
* Preload manager for the HttpFoundation component. | ||
* Manages preload HTTP headers. | ||
* | ||
* @author Kévin Dunglas <dunglas@gmail.com> | ||
*/ | ||
class HttpFoundationPreloadManager implements PreloadManagerInterface | ||
class PreloadManager implements PreloadManagerInterface | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. would forbid decoration for no reason imho - see comments on the interface: building a profiler panel could require decorating this class There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I meant decoration by inheritance of course. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There are 2 benefits:
I don't see connection between contract enforcing and "inheritance should not be prevented". I'd say the opposite. See similar opinion, for example: http://ocramius.github.io/blog/when-to-declare-classes-final/ /cc @Ocramius |
||
{ | ||
private $resources = array(); | ||
|
||
|
@@ -62,14 +61,12 @@ public function setResources(array $resources) | |
} | ||
|
||
/** | ||
* Sets preload Link HTTP header. | ||
* | ||
* @param Response $response | ||
* {@inheritdoc} | ||
*/ | ||
public function setLinkHeader(Response $response) | ||
public function getLinkValue() | ||
{ | ||
if (!$this->resources) { | ||
return; | ||
return null; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
} | ||
|
||
$parts = array(); | ||
|
@@ -86,6 +83,6 @@ public function setLinkHeader(Response $response) | |
$parts[] = $part; | ||
} | ||
|
||
$response->headers->set('Link', implode(',', $parts)); | ||
return implode(',', $parts); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,4 +42,11 @@ public function getResources(); | |
* @param array $resources | ||
*/ | ||
public function setResources(array $resources); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we have any use case for replacing all resources, except for clearing ? If no, we could simplify the code by allowing only clearing |
||
|
||
/** | ||
* Gets the value of the preload Link HTTP header. | ||
* | ||
* @return string|null | ||
*/ | ||
public function getLinkValue(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think |
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
missing class existence resource (or you could just conflict with asset < 3.3, and have all this in the XML file directly)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't get what you mean. But if it's ok to bump the dependency, I'll move this definition to the XML, it's cleaner.