8000 Add a new Link component by dunglas · Pull Request #22273 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

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

Closed
wants to merge 14 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Rename the component Link and add generic Link support
  • Loading branch information
dunglas committed Apr 10, 2017
commit d83b2447effb06bec9390f85eb468e32b5adb23a
135 changes: 135 additions & 0 deletions src/Symfony/Bridge/Twig/Extension/LinkExtension.php
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.
Copy link
Member

Choose a reason for hiding this comment

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

Symfony Link

Copy link
Contributor

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 ;)

*
* @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';
}
}
65 changes: 0 additions & 65 deletions src/Symfony/Bridge/Twig/Extension/PreloadExtension.php

This file was deleted.

76 changes: 76 additions & 0 deletions src/Symfony/Bridge/Twig/Tests/Extension/LinkExtensionTest.php
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());
}
}
35 changes: 0 additions & 35 deletions src/Symfony/Bridge/Twig/Tests/Extension/PreloadExtensionTest.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
use Symfony\Component\Form\Form;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Link\LinkManagerInterface;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Translation\Translator;
use Symfony\Component\Validator\Validation;
Expand Down Expand Up @@ -101,6 +101,7 @@ public function getConfigTreeBuilder()
$this->addPropertyInfoSection($rootNode);
$this->addCacheSection($rootNode);
$this->addPhpErrorsSection($rootNode);
$this->addLinksSection($rootNode);

return $treeBuilder;
}
Expand Down Expand Up @@ -806,4 +807,16 @@ private function addPhpErrorsSection(ArrayNodeDefinition $rootNode)
->end()
;
}

private function addLinksSection(ArrayNodeDefinition $rootNode)
{
$rootNode
->children()
->arrayNode('links')
->info('links configuration')
->{!class_exists(FullStack::class) && interface_exists(LinkManagerInterface::class) ? 'canBeDisabled' : 'canBeEnabled'}()
->end()
->end()
;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -208,6 +209,14 @@ public function load(array $configs, ContainerBuilder $container)
$this->registerPropertyInfoConfiguration($config['property_info'], $container, $loader);
}

if ($this->isConfigEnabled($container, $config['links'])) {
Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Member Author
@dunglas dunglas Apr 5, 2017

Choose a reason for hiding this comment

The 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 true but the component not installed?

Copy link
Member

Choose a reason for hiding this comment

The 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\\',
Expand Down
18 changes: 18 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/Resources/config/links.xml
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" />
Copy link
Member

Choose a reason for h 1CF5 iding this comment

The 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">
D330
Copy link
Member

Choose a reason for hiding this comment

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

Can be private?

Copy link
Member Author

Choose a reason for hiding this comment

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

IIRC, event listeners can't

Copy link
Member

Choose a reason for hiding this comment

The 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>
Loading
0