Skip to content

Commit 3d8a023

Browse files
committed
Add a new Preload component
1 parent 64f9f7b commit 3d8a023

File tree

16 files changed

+197
-49
lines changed

16 files changed

+197
-49
lines changed

src/Symfony/Bridge/Twig/Extension/AssetExtension.php

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
namespace Symfony\Bridge\Twig\Extension;
1313

1414
use Symfony\Component\Asset\Packages;
15-
use Symfony\Component\Asset\Preload\PreloadManagerInterface;
1615

1716
/**
1817
* Twig extension for the Symfony Asset component.
@@ -22,12 +21,10 @@
2221
class AssetExtension extends \Twig_Extension
2322
{
2423
private $packages;
25-
private $preloadManager;
2624

27-
public function __construct(Packages $packages, PreloadManagerInterface $preloadManager = null)
25+
public function __construct(Packages $packages)
2826
{
2927
$this->packages = $packages;
30-
$this->preloadManager = $preloadManager;
3128
}
3229

3330
/**
@@ -38,7 +35,6 @@ public function getFunctions()
3835
return array(
3936
new \Twig_SimpleFunction('asset', array($this, 'getAssetUrl')),
4037
new \Twig_SimpleFunction('asset_version', array($this, 'getAssetVersion')),
41-
new \Twig_SimpleFunction('preload', array($this, 'preload')),
4238
);
4339
}
4440

@@ -71,26 +67,6 @@ public function getAssetVersion($path, $packageName = null)
7167
return $this->packages->getVersion($path, $packageName);
7268
}
7369

74-
/**
75-
* Preloads an asset.
76-
*
77-
* @param string $path A public path
78-
* @param string $as A valid destination according to https://fetch.spec.whatwg.org/#concept-request-destination
79-
* @param bool $nopush If this asset should not be pushed over HTTP/2
80-
*
81-
* @return string The path of the asset
82-
*/
83-
public function preload($path, $as = '', $nopush = false)
84-
{
85-
if (null === $this->preloadManager) {
86-
throw new \RuntimeException('A preload manager must be configured to use the "preload" function.');
87-
}
88-
89-
$this->preloadManager->addResource($path, $as, $nopush);
90-
91-
return $path;
92-
}
93-
9470
/**
9571
* Returns the name of the extension.
9672
*
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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\Preload\PreloadManagerInterface;
15+
16+
/**
17+
* Twig extension for the Symfony Preload component.
18+
*
19+
* @author Kévin Dunglas <dunglas@gmail.com>
20+
*/
21+
class PreloadExtension extends \Twig_Extension
22+
{
23+
private $preloadManager;
24+
25+
public function __construct(PreloadManagerInterface $preloadManager)
26+
{
27+
$this->preloadManager = $preloadManager;
28+
}
29+
30+
/**
31+
* {@inheritdoc}
32+
*/
33+
public function getFunctions()
34+
{
35+
return array(
36+
new \Twig_SimpleFunction('preload', array($this, 'preload')),
37+
);
38+
}
39+
40+
/**
41+
* Preloads an asset.
42+
*
43+
* @param string $path A public path
44+
* @param string $as A valid destination according to https://fetch.spec.whatwg.org/#concept-request-destination
45+
* @param bool $nopush If this asset should not be pushed over HTTP/2
46+
*
47+
* @return string The path of the asset
48+
*/
49+
public function preload($path, $as = '', $nopush = false)
50+
{
51+
$this->preloadManager->addResource($path, $as, $nopush);
52+
53+
return $path;
54+
}
55+
56+
/**
57+
* Returns the name of the extension.
58+
*
59+
* @return string The extension name
60+
*/
61+
public function getName()
62+
{
63+
return 'preload';
64+
}
65+
}

src/Symfony/Bridge/Twig/Tests/Extension/AssetExtensionTest.php renamed to src/Symfony/Bridge/Twig/Tests/Extension/PreloadExtensionTest.php

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,13 @@
1212
namespace Symfony\Bridge\Twig\Tests\Extension;
1313

1414
use PHPUnit\Framework\TestCase;
15-
use Symfony\Bridge\Twig\Extension\AssetExtension;
16-
use Symfony\Component\Asset\Packages;
17-
use Symfony\Component\Asset\Preload\PreloadManager;
15+
use Symfony\Bridge\Twig\Extension\PreloadExtension;
16+
use Symfony\Component\Preload\PreloadManager;
1817

1918
/**
2019
* @author Kévin Dunglas <dunglas@gmail.com>
2120
*/
22-
class AssetExtensionTest extends TestCase
21+
class PreloadExtensionTest extends TestCase
2322
{
2423
public function testGetAndPreloadAssetUrl()
2524
{
@@ -28,18 +27,9 @@ public function testGetAndPreloadAssetUrl()
2827
}
2928

3029
$preloadManager = new PreloadManager();
31-
$extension = new AssetExtension(new Packages(), $preloadManager);
30+
$extension = new PreloadExtension($preloadManager);
3231

3332
$this->assertEquals('/foo.css', $extension->preload('/foo.css', 'style', true));
3433
$this->assertEquals('</foo.css>; rel=preload; as=style; nopush', $preloadManager->buildLinkValue());
3534
}
36-
37-
/**
38-
* @expectedException \RuntimeException
39-
*/
40-
public function testNoConfiguredPreloadManager()
41-
{
42-
$extension = new AssetExtension(new Packages());
43-
$extension->preload('/foo.css');
44-
}
4535
}

src/Symfony/Bundle/TwigBundle/DependencyInjection/TwigExtension.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\DependencyInjection\Reference;
1717
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
1818
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
19+
use Symfony\Component\Preload\PreloadManagerInterface;
1920

2021
/**
2122
* TwigExtension.
@@ -48,6 +49,10 @@ public function load(array $configs, ContainerBuilder $container)
4849
$container->removeDefinition('twig.translation.extractor');
4950
}
5051

52+
if (!interface_exists(PreloadManagerInterface::class)) {
53+
$container->removeDefinition('twig.extension.preload');
54+
}
55+
5156
foreach ($configs as $key => $config) {
5257
if (isset($config['globals'])) {
5358
foreach ($config['globals'] as $name => $value) {

src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,10 @@
7070

7171
<service id="twig.extension.assets" class="Symfony\Bridge\Twig\Extension\AssetExtension" public="false">
7272
<argument type="service" id="assets.packages" />
73-
<argument type="service" id="assets.preload_manager" on-invalid="ignore" />
73+
</service>
74+
75+
<service id="twig.extension.preload" class="Symfony\Bridge\Twig\Extension\PreloadExtension" public="false">
76+
<argument type="service" id="assets.preload_manager" />
7477
</service>
7578

7679
<service id="twig.extension.code" class="Symfony\Bridge\Twig\Extension\CodeExtension" public="false">
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
vendor/
2+
composer.lock
3+
phpunit.xml
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CHANGELOG
2+
=========
3+
4+
3.3.0
5+
-----
6+
7+
* added the component

src/Symfony/Component/Asset/EventListener/PreloadListener.php renamed to src/Symfony/Component/Preload/EventListener/PreloadListener.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@
99
* file that was distributed with this source code.
1010
*/
1111

12-
namespace Symfony\Component\Asset\EventListener;
12+
namespace Symfony\Component\Preload\EventListener;
1313

14-
use Symfony\Component\Asset\Preload\PreloadManager;
15-
use Symfony\Component\Asset\Preload\PreloadManagerInterface;
14+
use Symfony\Component\Preload\PreloadManagerInterface;
1615
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
1716
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
1817
use Symfony\Component\HttpKernel\KernelEvents;

src/Symfony/Component/Preload/LICENSE

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2004-2017 Fabien Potencier
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is furnished
8+
to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in all
11+
copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.

src/Symfony/Component/Asset/Preload/PreloadManager.php renamed to src/Symfony/Component/Preload/PreloadManager.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* file that was distributed with this source code.
1010
*/
1111

12-
namespace Symfony\Component\Asset\Preload;
12+
namespace Symfony\Component\Preload;
1313

1414
/**
1515
* Manages preload HTTP headers.

src/Symfony/Component/Asset/Preload/PreloadManagerInterface.php renamed to src/Symfony/Component/Preload/PreloadManagerInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* file that was distributed with this source code.
1010
*/
1111

12-
namespace Symfony\Component\Asset\Preload;
12+
namespace Symfony\Component\Preload;
1313

1414
/**
1515
* Manages resources to preload according to the W3C "Preload" specification.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
Preload Component
2+
===============
3+
4+
The Preload component manages preloading and prefetching of HTTP resources through Link headers
5+
and HTTP/2 pushes.
6+
7+
Resources
8+
---------
9+
10+
* [Documentation](https://symfony.com/doc/current/components/preload/introduction.html)
11+
* [Contributing](https://symfony.com/doc/current/contributing/index.html)
12+
* [Report issues](https://github.com/symfony/symfony/issues) and
13+
[send Pull Requests](https://github.com/symfony/symfony/pulls)
14+
in the [main Symfony repository](https://github.com/symfony/symfony)

src/Symfony/Component/Asset/Tests/EventListener/PreloadListenerTest.php renamed to src/Symfony/Component/Preload/Tests/EventListener/PreloadListenerTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99
* file that was distributed with this source code.
1010
*/
1111

12-
namespace Symfony\Component\Asset\Tests\EventListener;
12+
namespace Symfony\Component\Preload\Tests\EventListener;
1313

1414
use PHPUnit\Framework\TestCase;
15-
use Symfony\Component\Asset\EventListener\PreloadListener;
16-
use Symfony\Component\Asset\Preload\PreloadManager;
15+
use Symfony\Component\Preload\EventListener\PreloadListener;
16+
use Symfony\Component\Preload\PreloadManager;
1717
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
1818
use Symfony\Component\HttpFoundation\Response;
1919
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;

src/Symfony/Component/Asset/Tests/Preload/PreloadManagerTest.php renamed to src/Symfony/Component/Preload/Tests/PreloadManagerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* file that was distributed with this source code.
1010
*/
1111

12-
namespace Symfony\Component\Asset\Preload;
12+
namespace Symfony\Component\Preload;
1313

1414
use PHPUnit\Framework\TestCase;
1515

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
"name": "symfony/preload",
3+
"type": "library",
4+
"description": "Symfony Preload Component",
5+
"keywords": ["HTTP/2", "preload", "prefetch", "push", "performance"],
6+
"homepage": "https://symfony.com",
7+
"license": "MIT",
8+
"authors": [
9+
{
10+
"name": "Kévin Dunglas",
11+
"email": "dunglas@gmail.com"
12+
},
13+
{
14+
"name": "Symfony Community",
15+
"homepage": "https://symfony.com/contributors"
16+
}
17+
],
18+
"require": {
19+
"php": ">=5.5.9"
20+
},
21+
"suggest": {
22+
"symfony/http-kernel": ""
23+
},
24+
"require-dev": {
25+
"symfony/http-kernel": "^2.8 || ^3.0"
26+
},
27+
"autoload": {
28+
"psr-4": { "Symfony\\Component\\Preload\\": "" },
29+
"exclude-from-classmap": [
30+
"/Tests/"
31+
]
32+
},
33+
"minimum-stability": "dev",
34+
"extra": {
35+
"branch-alias": {
36+
"dev-master": "3.3-dev"
37+
}
38+
}
39+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.1/phpunit.xsd"
5+
backupGlobals="false"
6+
colors="true"
7+
bootstrap="vendor/autoload.php"
8+
>
9+
<php>
10+
<ini name="error_reporting" value="-1" />
11+
</php>
12+
13+
<testsuites>
14+
<testsuite name="Symfony Preload Component Test Suite">
15+
<directory>./Tests/</directory>
16+
</testsuite>
17+
</testsuites>
18+
19+
<filter>
20+
<whitelist>
21+
<directory>./</directory>
22+
<exclude>
23+
<directory>./Tests</directory>
24+
<directory>./vendor</directory>
25+
</exclude>
26+
</whitelist>
27+
</filter>
28+
</phpunit>

0 commit comments

Comments
 (0)