Skip to content
This repository has been archived by the owner on Jul 4, 2018. It is now read-only.

Commit

Permalink
feature #1508 Added WebLink component integration (skalpa)
Browse files Browse the repository at this point in the history
This PR was merged into the 2.1.x-dev branch.

Discussion
----------

Added WebLink component integration

I made the necessary changes to support the new WebLink component:

- `HttpKernelServiceProvider` registers the `AddLinkHeaderListener` if the component is available
- `TwigServiceProvider` adds the `WebLinkExtension` if the component and the Twig bridge are available
- Both changes are tested
- The documentation has been amended (the link to the Symfony website doesn't work as 3.3 hasn't been released yet but I believe it's the good URL)

Commits
-------

37bd5ae Added WebLink component integration
  • Loading branch information
fabpot committed May 3, 2017
2 parents 24ad5c3 + 37bd5ae commit 590ca76
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 1 deletion.
3 changes: 2 additions & 1 deletion composer.json
Expand Up @@ -47,7 +47,8 @@
"twig/twig": "~1.28|~2.0",
"doctrine/dbal": "~2.2",
"swiftmailer/swiftmailer": "~5",
"monolog/monolog": "^1.4.1"
"monolog/monolog": "^1.4.1",
"symfony/web-link": "^3.3"
},
"replace": {
"silex/api": "self.version",
Expand Down
9 changes: 9 additions & 0 deletions doc/providers/twig.rst
Expand Up @@ -134,6 +134,15 @@ If you are using the ``SecurityServiceProvider``, you will have access to the
`Symfony Security documentation
<http://symfony.com/doc/current/book/security.html#access-control-in-templates>`_.

Web Link Support
~~~~~~~~~~~~~~~~

If you are using the ``symfony/web-link`` component, you will have access to the
``preload()``, ``prefetch()``, ``prerender()``, ``dns_prefetch()``,
``preconnect()`` and ``link()`` functions in templates. You can find more
information in the `Symfony WebLink documentation
<https://symfony.com/doc/current/components/weblink/introduction.html>`_.

Global Variable
~~~~~~~~~~~~~~~

Expand Down
6 changes: 6 additions & 0 deletions src/Silex/Provider/HttpKernelServiceProvider.php
Expand Up @@ -24,6 +24,8 @@
use Symfony\Component\HttpKernel\EventListener\ResponseListener;
use Symfony\Component\HttpKernel\HttpKernel;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\WebLink\EventListener\AddLinkHeaderListener;
use Symfony\Component\WebLink\HttpHeaderSerializer;

class HttpKernelServiceProvider implements ServiceProviderInterface, EventListenerProviderInterface
{
Expand Down Expand Up @@ -91,5 +93,9 @@ public function subscribe(Container $app, EventDispatcherInterface $dispatcher)
$dispatcher->addSubscriber(new MiddlewareListener($app));
$dispatcher->addSubscriber(new ConverterListener($app['routes'], $app['callback_resolver']));
$dispatcher->addSubscriber(new StringToResponseListener());

if (class_exists(HttpHeaderSerializer::class)) {
$dispatcher->addSubscriber(new AddLinkHeaderListener());
}
}
}
6 changes: 6 additions & 0 deletions src/Silex/Provider/TwigServiceProvider.php
Expand Up @@ -23,9 +23,11 @@
use Symfony\Bridge\Twig\Extension\SecurityExtension;
use Symfony\Bridge\Twig\Extension\HttpFoundationExtension;
use Symfony\Bridge\Twig\Extension\HttpKernelExtension;
use Symfony\Bridge\Twig\Extension\WebLinkExtension;
use Symfony\Bridge\Twig\Form\TwigRendererEngine;
use Symfony\Bridge\Twig\Form\TwigRenderer;
use Symfony\Bridge\Twig\Extension\HttpKernelRuntime;
use Symfony\Component\WebLink\HttpHeaderSerializer;

/**
* Twig integration for Silex.
Expand Down Expand Up @@ -142,6 +144,10 @@ public function register(Container $app)
if (class_exists(HttpKernelRuntime::class)) {
$twig->addRuntimeLoader($app['twig.runtime_loader']);
}

if (class_exists(HttpHeaderSerializer::class) && class_exists(WebLinkExtension::class)) {
$twig->addExtension(new WebLinkExtension($app['request_stack']));
}
}

return $twig;
Expand Down
18 changes: 18 additions & 0 deletions tests/Silex/Tests/ApplicationTest.php
Expand Up @@ -11,6 +11,8 @@

namespace Silex\Tests;

use Fig\Link\GenericLinkProvider;
use Fig\Link\Link;
use Silex\Application;
use Silex\ControllerCollection;
use Silex\Api\ControllerProviderInterface;
Expand Down Expand Up @@ -655,6 +657,22 @@ public function testViewListenersResponsesAreNotUsedIfNull()
$this->assertEquals('Hello view listener', $response->getContent());
}

public function testWebLinkListener()
{
$app = new Application();

$app->get('/', function () {
return 'hello';
});

$request = Request::create('/');
$request->attributes->set('_links', (new GenericLinkProvider())->withLink(new Link('preload', '/foo.css')));

$response = $app->handle($request);

$this->assertEquals('</foo.css>; rel="preload"', $response->headers->get('Link'));
}

public function testDefaultRoutesFactory()
{
$app = new Application();
Expand Down
22 changes: 22 additions & 0 deletions tests/Silex/Tests/Provider/TwigServiceProviderTest.php
Expand Up @@ -11,11 +11,13 @@

namespace Silex\Tests\Provider;

use Fig\Link\Link;
use Silex\Application;
use Silex\Provider\CsrfServiceProvider;
use Silex\Provider\FormServiceProvider;
use Silex\Provider\TwigServiceProvider;
use Silex\Provider\AssetServiceProvider;
use Symfony\Bridge\Twig\Extension\WebLinkExtension;
use Symfony\Component\HttpFoundation\Request;

/**
Expand Down Expand Up @@ -138,4 +140,24 @@ public function testFormatParameters()
$this->assertSame($timezone, $twig->getExtension('Twig_Extension_Core')->getTimezone());
$this->assertSame(array(2, ',', ' '), $twig->getExtension('Twig_Extension_Core')->getNumberFormat());
}

public function testWebLinkIntegration()
{
if (!class_exists(WebLinkExtension::class)) {
$this->markTestSkipped('Twig WebLink extension not available.');
}

$app = new Application();
$app['request_stack']->push($request = Request::create('/'));
$app->register(new TwigServiceProvider(), array(
'twig.templates' => array(
'preload' => '{{ preload("/foo.css") }}',
),
));

$this->assertEquals('/foo.css', $app['twig']->render('preload'));

$link = new Link('preload', '/foo.css');
$this->assertEquals(array($link), array_values($request->attributes->get('_links')->getLinks()));
}
}

0 comments on commit 590ca76

Please sign in to comment.