-
-
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 |
---|---|---|
@@ -0,0 +1,135 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bridge\Twig\Extension; | ||
|
||
use Symfony\Component\Link\LinkManagerInterface; | ||
|
||
/** | ||
* Twig extension for the Symfony Preload component. | ||
* | ||
* @author Kévin Dunglas <dunglas@gmail.com> | ||
*/ | ||
class LinkExtension extends \Twig_Extension | ||
{ | ||
private $linkManager; | ||
|
||
public function __construct(LinkManagerInterface $linkManager) | ||
{ | ||
$this->linkManager = $linkManager; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getFunctions() | ||
{ | ||
return array( | ||
new \Twig_SimpleFunction('link', array($this, 'link')), | ||
new \Twig_SimpleFunction('preload', array($this, 'preload')), | ||
new \Twig_SimpleFunction('dns_prefetch', array($this, 'dnsPrefetch')), | ||
new \Twig_SimpleFunction('preconnect', array($this, 'preconnect')), | ||
new \Twig_SimpleFunction('prefetch', array($this, 'prefetch')), | ||
new \Twig_SimpleFunction('prerender', array($this, 'prerender')), | ||
); | ||
} | ||
|
||
/** | ||
* Adds a "Link" HTTP header. | ||
* | ||
* @param string $uri The relation URI | ||
* @param string $rel The relation type (e.g. "preload", "prefetch", "prerender" or "dns-prefetch") | ||
* @param array $attributes The attributes of this link (e.g. "array('as' => true)", "array('pr' => 0.5)") | ||
* | ||
* @return string The relation URI | ||
*/ | ||
public function link($uri, $rel, array $attributes = array()) | ||
{ | ||
$this->linkManager->add($uri, $rel, $attributes); | ||
|
||
return $uri; | ||
} | ||
|
||
/** | ||
* Preloads a resource. | ||
* | ||
* @param string $uri A public path | ||
* @param array $attributes The attributes of this link (e.g. "array('as' => true)", "array('crossorigin' => 'use-credentials')") | ||
* | ||
* @return string The path of the asset | ||
*/ | ||
public function preload($uri, array $attributes = array()) | ||
{ | ||
return $this->link($uri, 'preload', $attributes); | ||
} | ||
|
||
/** | ||
* Resolves a resource origin as early as possible. | ||
* | ||
* @param string $uri A public path | ||
* @param array $attributes The attributes of this link (e.g. "array('as' => true)", "array('pr' => 0.5)") | ||
* | ||
* @return string The path of the asset | ||
*/ | ||
public function dnsPrefetch($uri, array $attributes = array()) | ||
{ | ||
return $this->link($uri, 'dns-prefetch', $attributes); | ||
} | ||
|
||
/** | ||
* Initiates a early connection to a resource (DNS resolution, TCP handshake, TLS negotiation). | ||
* | ||
* @param string $uri A public path | ||
* @param array $attributes The attributes of this link (e.g. "array('as' => true)", "array('pr' => 0.5)") | ||
* | ||
* @return string The path of the asset | ||
*/ | ||
public function preconnect($uri, array $attributes = array()) | ||
{ | ||
return $this->link($uri, 'preconnect', $attributes); | ||
} | ||
|
||
/** | ||
* Indicates to the client that it should prefetch this resource . | ||
* | ||
* @param string $uri A public path | ||
* @param array $attributes The attributes of this link (e.g. "array('as' => true)", "array('pr' => 0.5)") | ||
* | ||
* @return string The path of the asset | ||
*/ | ||
public function prefetch($uri, array $attributes = array()) | ||
{ | ||
return $this->link($uri, 'prefetch', $attributes); | ||
} | ||
|
||
/** | ||
* Indicates to the client that it should prerender this resource . | ||
* | ||
* @param string $uri A public path | ||
* @param array $attributes The attributes of this link (e.g. "array('as' => true)", "array('pr' => 0.5)") | ||
* | ||
* @return string The path of the asset | ||
*/ | ||
public function prerender($uri, array $attributes = array()) | ||
{ | ||
return $this->link($uri, 'prerender', $attributes); | ||
} | ||
|
||
/** | ||
* Returns the name of the extension. | ||
* | ||
* @return string The extension name | ||
*/ | ||
public function getName() | ||
{ | ||
return 'link'; | ||
} | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bridge\Twig\Tests\Extension; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Bridge\Twig\Extension\LinkExtension; | ||
use Symfony\Component\Link\LinkManager; | ||
|
||
/** | ||
* @author Kévin Dunglas <dunglas@gmail.com> | ||
*/ | ||
class LinkExtensionTest extends TestCase | ||
{ | ||
public function testLink() | ||
{ | ||
$linkManager = new LinkManager(); | ||
$extension = new LinkExtension($linkManager); | ||
|
||
$this->assertEquals('/foo.css', $extension->link('/foo.css', 'preload', array('as' => 'style', 'nopush' => true))); | ||
$this->assertEquals('</foo.css>; rel=preload; as=style; nopush', $linkManager->buildValues()); | ||
} | ||
|
||
public function testPreload() | ||
{ | ||
$linkManager = new LinkManager(); | ||
$extension = new LinkExtension($linkManager); | ||
|
||
$this->assertEquals('/foo.css', $extension->preload('/foo.css', array('as' => 'style', 'crossorigin' => true))); | ||
$this->assertEquals('</foo.css>; rel=preload; as=style; crossorigin', $linkManager->buildValues()); | ||
} | ||
|
||
public function testDnsPrefetch() | ||
{ | ||
$linkManager = new LinkManager(); | ||
$extension = new LinkExtension($linkManager); | ||
|
||
$this->assertEquals('/foo.css', $extension->dnsPrefetch('/foo.css', array('as' => 'style', 'crossorigin' => true))); | ||
$this->assertEquals('</foo.css>; rel=dns-prefetch; as=style; crossorigin', $linkManager->buildValues()); | ||
} | ||
|
||
public function testPreconnect() | ||
{ | ||
$linkManager = new LinkManager(); | ||
$extension = new LinkExtension($linkManager); | ||
|
||
$this->assertEquals('/foo.css', $extension->preconnect('/foo.css', array('as' => 'style', 'crossorigin' => true))); | ||
$this->assertEquals('</foo.css>; rel=preconnect; as=style; crossorigin', $linkManager->buildValues()); | ||
} | ||
|
||
public function testPrefetch() | ||
{ | ||
$linkManager = new LinkManager(); | ||
$extension = new LinkExtension($linkManager); | ||
|
||
$this->assertEquals('/foo.css', $extension->prefetch('/foo.css', array('as' => 'style', 'crossorigin' => true))); | ||
$this->assertEquals('</foo.css>; rel=prefetch; as=style; crossorigin', $linkManager->buildValues()); | ||
} | ||
|
||
public function testPrerender() | ||
{ | ||
$linkManager = new LinkManager(); | ||
$extension = new LinkExtension($linkManager); | ||
|
||
$this->assertEquals('/foo.css', $extension->prerender('/foo.css', array('as' => 'style', 'crossorigin' => true))); | ||
$this->assertEquals('</foo.css>; rel=prerender; as=style; crossorigin', $linkManager->buildValues()); | ||
} | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,7 @@ | |
use Symfony\Component\Finder\Finder; | ||
use Symfony\Component\HttpKernel\DependencyInjection\Extension; | ||
use Symfony\Component\Config\FileLocator; | ||
use Symfony\Component\Link\LinkManagerInterface; | ||
use Symfony\Component\PropertyAccess\PropertyAccessor; | ||
use Symfony\Component\Serializer\Encoder\YamlEncoder; | ||
use Symfony\Component\Serializer\Encoder\CsvEncoder; | ||
|
@@ -208,6 +209,14 @@ public function load(array $configs, ContainerBuilder $container) | |
$this->registerPropertyInfoConfiguration($config['property_info'], $container, $loader); | ||
} | ||
|
||
if ($this->isConfigEnabled($container, $config['links'])) { | ||
There was a problem hiding this comment EED3 . Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should test for the Link component to be present first and throw a meaningful exception if not present. 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. Link should not be a mandatory component. You mean throwing if enabled is set to 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. yes, we did that recently for other optional dependencies too |
||
if (!interface_exists(LinkManagerInterface::class)) { | ||
throw new LogicException('Link support cannot be enabled as the Link component is not installed.'); | ||
} | ||
|
||
$loader->load('links.xml'); | ||
} | ||
|
||
$this->addAnnotatedClassesToCompile(array( | ||
'**Bundle\\Controller\\', | ||
'**Bundle\\Entity\\', | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?xml version="1.0" ?> | ||
|
||
<container xmlns="http://symfony.com/schema/dic/services" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> | ||
|
||
<services> | ||
|
||
<service id="links.link_manager" class="Symfony\Component\Link\LinkManager" public="false" /> | ||
There was a problem hiding this comment. Choose a reason for h 1CF5 iding this commentThe reason will be displayed to describe this comment to others. Learn more. this file is missing autowiring aliases |
||
|
||
<service id="links.link_listener" class="Symfony\Component\Link\EventListener\LinkListener"> | ||
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. Can be private? 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. IIRC, event listeners can't 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. since #20953 they can |
||
<argument type="service" id="links.link_manager" /> | ||
|
||
<tag name="kernel.event_subscriber" /> | ||
</service> | ||
|
||
</services> | ||
</container> |
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.
Symfony Link
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.
WebLink
is a good name ;)