Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit ff6b790

Browse filesBrowse files
committed
Rename the component Link and add generic Link support
1 parent 2ac282e commit ff6b790
Copy full SHA for ff6b790

31 files changed

+468
-272
lines changed
+135Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\Twig\Extension;
13+
14+
use Symfony\Component\Link\LinkManagerInterface;
15+
16+
/**
17+
* Twig extension for the Symfony Preload component.
18+
*
19+
* @author Kévin Dunglas <dunglas@gmail.com>
20+
*/
21+
class LinkExtension extends \Twig_Extension
22+
{
23+
private $linkManager;
24+
25+
public function __construct(LinkManagerInterface $linkManager)
26+
{
27+
$this->linkManager = $linkManager;
28+
}
29+
30+
/**
31+
* {@inheritdoc}
32+
*/
33+
public function getFunctions()
34+
{
35+
return array(
36+
new \Twig_SimpleFunction('link', array($this, 'link')),
37+
new \Twig_SimpleFunction('preload', array($this, 'preload')),
38+
new \Twig_SimpleFunction('dns_prefetch', array($this, 'dnsPrefetch')),
39+
new \Twig_SimpleFunction('preconnect', array($this, 'preconnect')),
40+
new \Twig_SimpleFunction('prefetch', array($this, 'prefetch')),
41+
new \Twig_SimpleFunction('prerender', array($this, 'prerender')),
42+
);
43+
}
44+
45+
/**
46+
* Adds a "Link" HTTP header.
47+
*
48+
* @param string $uri The relation URI
49+
* @param string $rel The relation type (e.g. "preload", "prefetch", "prerender" or "dns-prefetch")
50+
* @param array $attributes The attributes of this link (e.g. "array('as' => true)", "array('pr' => 0.5)")
51+
*
52+
* @return string The relation URI
53+
*/
54+
public function link($uri, $rel, array $attributes = array())
55+
{
56+
$this->linkManager->add($uri, $rel, $attributes);
57+
58+
return $uri;
59+
}
60+
61+
/**
62+
* Preloads a resource.
63+
*
64+
* @param string $uri A public path
65+
* @param array $attributes The attributes of this link (e.g. "array('as' => true)", "array('crossorigin' => 'use-credentials')")
66+
*
67+
* @return string The path of the asset
68+
*/
69+
public function preload($uri, array $attributes = array())
70+
{
71+
return $this->link($uri, 'preload', $attributes);
72+
}
73+
74+
/**
75+
* Resolves a resource origin as early as possible.
76+
*
77+
* @param string $uri A public path
78+
* @param array $attributes The attributes of this link (e.g. "array('as' => true)", "array('pr' => 0.5)")
79+
*
80+
* @return string The path of the asset
81+
*/
82+
public function dnsPrefetch($uri, array $attributes = array())
83+
{
84+
return $this->link($uri, 'dns-prefetch', $attributes);
85+
}
86+
87+
/**
88+
* Initiates a early connection to a resource (DNS resolution, TCP handshake, TLS negotiation).
89+
*
90+
* @param string $uri A public path
91+
* @param array $attributes The attributes of this link (e.g. "array('as' => true)", "array('pr' => 0.5)")
92+
*
93+
* @return string The path of the asset
94+
*/
95+
public function preconnect($uri, array $attributes = array())
96+
{
97+
return $this->link($uri, 'preconnect', $attributes);
98+
}
99+
100+
/**
101+
* Indicates to the client that it should prefetch this resource .
102+
*
103+
* @param string $uri A public path
104+
* @param array $attributes The attributes of this link (e.g. "array('as' => true)", "array('pr' => 0.5)")
105+
*
106+
* @return string The path of the asset
107+
*/
108+
public function prefetch($uri, array $attributes = array())
109+
{
110+
return $this->link($uri, 'prefetch', $attributes);
111+
}
112+
113+
/**
114+
* Indicates to the client that it should prerender this resource .
115+
*
116+
* @param string $uri A public path
117+
* @param array $attributes The attributes of this link (e.g. "array('as' => true)", "array('pr' => 0.5)")
118+
*
119+
* @return string The path of the asset
120+
*/
121+
public function prerender($uri, array $attributes = array())
122+
{
123+
return $this->link($uri, 'prerender', $attributes);
124+
}
125+
126+
/**
127+
* Returns the name of the extension.
128+
*
129+
* @return string The extension name
130+
*/
131+
public function getName()
132+
{
133+
return 'link';
134+
}
135+
}

‎src/Symfony/Bridge/Twig/Extension/PreloadExtension.php

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Twig/Extension/PreloadExtension.php
-65Lines changed: 0 additions & 65 deletions
This file was deleted.
+83Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\Twig\Tests\Extension;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Bridge\Twig\Extension\LinkExtension;
16+
use Symfony\Component\Link\LinkManager;
17+
18+
/**
19+
* @author Kévin Dunglas <dunglas@gmail.com>
20+
*/
21+
class LinkExtensionTest extends TestCase
22+
{
23+
protected function setUp()
24+
{
25+
if (!class_exists(LinkManager::class)) {
26+
$this->markTestSkipped('Requires Link.');
27+
}
28+
}
29+
30+
public function testLink()
31+
{
32+
$linkManager = new LinkManager();
33+
$extension = new LinkExtension($linkManager);
34+
35+
$this->assertEquals('/foo.css', $extension->link('/foo.css', 'preload', array('as' => 'style', 'nopush' => true)));
36+
$this->assertEquals('</foo.css>; rel=preload; as=style; nopush', $linkManager->buildValues());
37+
}
38+
39+
public function testPreload()
40+
{
41+
$linkManager = new LinkManager();
42+
$extension = new LinkExtension($linkManager);
43+
44+
$this->assertEquals('/foo.css', $extension->preload('/foo.css', array('as' => 'style', 'crossorigin' => true)));
45+
$this->assertEquals('</foo.css>; rel=preload; as=style; crossorigin', $linkManager->buildValues());
46+
}
47+
48+
public function testDnsPrefetch()
49+
{
50+
$linkManager = new LinkManager();
51+
$extension = new LinkExtension($linkManager);
52+
53+
$this->assertEquals('/foo.css', $extension->dnsPrefetch('/foo.css', array('as' => 'style', 'crossorigin' => true)));
54+
$this->assertEquals('</foo.css>; rel=dns-prefetch; as=style; crossorigin', $linkManager->buildValues());
55+
}
56+
57+
public function testPreconnect()
58+
{
59+
$linkManager = new LinkManager();
60+
$extension = new LinkExtension($linkManager);
61+
62+
$this->assertEquals('/foo.css', $extension->preconnect('/foo.css', array('as' => 'style', 'crossorigin' => true)));
63+
$this->assertEquals('</foo.css>; rel=preconnect; as=style; crossorigin', $linkManager->buildValues());
64+
}
65+
66+
public function testPrefetch()
67+
{
68+
$linkManager = new LinkManager();
69+
$extension = new LinkExtension($linkManager);
70+
71+
$this->assertEquals('/foo.css', $extension->prefetch('/foo.css', array('as' => 'style', 'crossorigin' => true)));
72+
$this->assertEquals('</foo.css>; rel=prefetch; as=style; crossorigin', $linkManager->buildValues());
73+
}
74+
75+
public function testPrerender()
76+
{
77+
$linkManager = new LinkManager();
78+
$extension = new LinkExtension($linkManager);
79+
80+
$this->assertEquals('/foo.css', $extension->prerender('/foo.css', array('as' => 'style', 'crossorigin' => true)));
81+
$this->assertEquals('</foo.css>; rel=prerender; as=style; crossorigin', $linkManager->buildValues());
82+
}
83+
}

‎src/Symfony/Bridge/Twig/Tests/Extension/PreloadExtensionTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Bridge/Twig/Tests/Extension/PreloadExtensionTest.php
-35Lines changed: 0 additions & 35 deletions
This file was deleted.

‎src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php
+14-1Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
1919
use Symfony\Component\Config\Definition\ConfigurationInterface;
2020
use Symfony\Component\Form\Form;
21-
use Symfony\Component\HttpFoundation\Request;
21+
use Symfony\Component\Link\LinkManagerInterface;
2222
use Symfony\Component\Serializer\Serializer;
2323
use Symfony\Component\Translation\Translator;
2424
use Symfony\Component\Validator\Validation;
@@ -101,6 +101,7 @@ public function getConfigTreeBuilder()
101101
$this->addPropertyInfoSection($rootNode);
102102
$this->addCacheSection($rootNode);
103103
$this->addPhpErrorsSection($rootNode);
104+
$this->addLinksSection($rootNode);
104105

105106
return $treeBuilder;
106107
}
@@ -806,4 +807,16 @@ private function addPhpErrorsSection(ArrayNodeDefinition $rootNode)
806807
->end()
807808
;
808809
}
810+
811+
private function addLinksSection(ArrayNodeDefinition $rootNode)
812+
{
813+
$rootNode
814+
->children()
815+
->arrayNode('links')
816+
->info('links configuration')
817+
->{!class_exists(FullStack::class) && interface_exists(LinkManagerInterface::class) ? 'canBeDisabled' : 'canBeEnabled'}()
818+
->end()
819+
->end()
820+
;
821+
}
809822
}

‎src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,10 @@ public function load(array $configs, ContainerBuilder $container)
208208
$this->registerPropertyInfoConfiguration($config['property_info'], $container, $loader);
209209
}
210210

211+
if ($this->isConfigEnabled($container, $config['links'])) {
212+
$loader->load('links.xml');
213+
}
214+
211215
$this->addAnnotatedClassesToCompile(array(
212216
'**Bundle\\Controller\\',
213217
'**Bundle\\Entity\\',
+18Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" ?>
2+
3+
<container xmlns="http://symfony.com/schema/dic/services"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
6+
7+
<services>
8+
9+
<service id="links.link_manager" class="Symfony\Component\Link\LinkManager" public="false" />
10+
11+
<service id="links.link_listener" class="Symfony\Component\Link\EventListener\LinkListener">
12+
<argument type="service" id="links.link_manager" />
13+
14+
<tag name="kernel.event_subscriber" />
15+
</service>
16+
17+
</services>
18+
</container>

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.