-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Add a new Link component #22273
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
Add a new Link component #22273
Changes from 1 commit
b7a1591
d83b244
7be2a6c
66c4ed6
aaa54c2
c73ae6c
949a207
fc4d63b
aba1dbb
4a82c3d
b136372
fd2dd50
f0ce807
209901b
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,8 +11,9 @@ | |
|
||
namespace Symfony\Bridge\Twig\Extension; | ||
|
||
use Fig\Link\GenericLinkProvider; | ||
use Fig\Link\Link; | ||
use Symfony\Component\WebLink\WebLinkManagerInterface; | ||
use Symfony\Component\HttpFoundation\RequestStack; | ||
|
||
/** | ||
* Twig extension for the Symfony WebLink component. | ||
|
@@ -21,11 +22,11 @@ | |
*/ | ||
class WebLinkExtension extends \Twig_Extension | ||
{ | ||
private $manager; | ||
private $requestStack; | ||
|
||
public function __construct(WebLinkManagerInterface $manager) | ||
public function __construct(RequestStack $requestStack) | ||
{ | ||
$this->manager = $manager; | ||
$this->requestStack = $requestStack; | ||
} | ||
|
||
/** | ||
|
@@ -54,12 +55,17 @@ public function getFunctions() | |
*/ | ||
public function link($uri, $rel, array $attributes = array()) | ||
{ | ||
if (!$request = $this->requestStack->getMasterRequest()) { | ||
return $uri; | ||
} | ||
|
||
$link = new Link($rel, $uri); | ||
foreach ($attributes as $key => $value) { | ||
$link = $link->withAttribute($key, $value); | ||
} | ||
|
||
$this->manager->add($link); | ||
$linkProvider = $request->attributes->get('_links', new GenericLinkProvider()); | ||
$request->attributes->set('_links', $linkProvider->withLink($link)); | ||
|
||
return $uri; | ||
} | ||
|
@@ -74,7 +80,7 @@ public function link($uri, $rel, array $attributes = array()) | |
*/ | ||
public function preload($uri, array $attributes = array()) | ||
{ | ||
return $this->link($uri, WebLinkManagerInterface::REL_PRELOAD, $attributes); | ||
return $this->link($uri, 'preload', $attributes); | ||
} | ||
|
||
/** | ||
|
@@ -87,7 +93,7 @@ public function preload($uri, array $attributes = array()) | |
*/ | ||
public function dnsPrefetch($uri, array $attributes = array()) | ||
{ | ||
return $this->link($uri, WebLinkManagerInterface::REL_DNS_PREFETCH, $attributes); | ||
return $this->link($uri, 'dns-prefetch', $attributes); | ||
} | ||
|
||
/** | ||
|
@@ -100,7 +106,7 @@ public function dnsPrefetch($uri, array $attributes = array()) | |
*/ | ||
public function preconnect($uri, array $attributes = array()) | ||
{ | ||
return $this->link($uri, WebLinkManagerInterface::REL_PRECONNECT, $attributes); | ||
return $this->link($uri, 'preconnect', $attributes); | ||
} | ||
|
||
/** | ||
|
@@ -113,7 +119,7 @@ public function preconnect($uri, array $attributes = array()) | |
*/ | ||
public function prefetch($uri, array $attributes = array()) | ||
{ | ||
return $this->link($uri, WebLinkManagerInterface::REL_PREFETCH, $attributes); | ||
return $this->link($uri, 'prefetch', $attributes); | ||
} | ||
|
||
/** | ||
|
@@ -126,7 +132,7 @@ public function prefetch($uri, array $attributes = array()) | |
*/ | ||
public function prerender($uri, array $attributes = array()) | ||
{ | ||
return $this->link($uri, WebLinkManagerInterface::REL_PRERENDER, $attributes); | ||
return $this->link($uri, 'prerender', $attributes); | ||
} | ||
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. We don't need this method. |
||
|
||
/** | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,16 +6,7 @@ | |
|
||
<services> | ||
|
||
<service id="web_link.manager" class="Symfony\Component\WebLink\WebLinkManager" public="false"> | ||
<argument type="service"> | ||
<service class="Fig\Link\GenericLinkProvider" /> | ||
</argument> | ||
</service> | ||
<service id="Symfony\Component\WebLink\WebLinkManagerInterface" alias="web_link.manager" public="false" /> | ||
|
||
<service id="web_link.add_link_header_listener" class="Symfony\Component\Link\WebLink\AddLinkHeaderListener" public="false"> | ||
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. It looks like there is a typo in class name here, should be: |
||
<argument type="service" id="web_link.manager" /> | ||
|
||
<tag name="kernel.event_subscriber" /> | ||
</service> | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,11 +11,11 @@ | |
|
||
namespace Symfony\Component\WebLink\EventListener; | ||
|
||
use Psr\Link\LinkProviderInterface; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
use Symfony\Component\HttpKernel\Event\FilterResponseEvent; | ||
use Symfony\Component\HttpKernel\KernelEvents; | ||
use Symfony\Component\WebLink\HttpHeaderSerializer; | ||
use Symfony\Component\WebLink\WebLinkManagerInterface; | ||
|
||
/** | ||
* Adds the Link HTTP header to the response. | ||
|
@@ -26,12 +26,10 @@ | |
*/ | ||
class AddLinkHeaderListener implements EventSubscriberInterface | ||
{ | ||
private $manager; | ||
private $serializer; | ||
|
||
public function __construct(WebLinkManagerInterface $manager) | ||
public function __construct() | ||
{ | ||
$this->manager = $manager; | ||
$this->serializer = new HttpHeaderSerializer(); | ||
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. having this class be instantiated directly here means it's an internal implementation detail to the listener 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. It's indeed an internal detail of the listener. But the I would keep this as is (btw the serializer is |
||
} | ||
|
||
|
@@ -41,12 +39,12 @@ public function onKernelResponse(FilterResponseEvent $event) | |
return; | ||
} | ||
|
||
if ($value = $this->serializer->serialize($this->manager->getLinkProvider())) { | ||
$event->getResponse()->headers->set('Link', $value, false); | ||
|
||
// Free memory | ||
$this->manager->clear(); | ||
$linkProvider = $event->getRequest()->attributes->get('_links'); | ||
if (!$linkProvider instanceof LinkProviderInterface || !($links = $linkProvider->getLinks())) { | ||
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. no need for the brackets after the |
||
return; | ||
} | ||
|
||
$event->getResponse()->headers->set('Link', $this->serializer->serialize($links), false); | ||
} | ||
|
||
/** | ||
|
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.
Is this ok or should we always populate this key? The current implementation is more memory efficient.
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.
Actually, I really think the ideal case would be for
Symfony\Component\HttpFoundation\Request
to implementPsr\Link\EvolvableLinkProviderInterface
. That's what I understand after reading PSR-13.But I understand that's not probably not possible considering the mutable nature of our
Request
. That said, I think using attributes for this is wrong.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 agree but as you pointed out, it's not possible.
Why?