Skip to content
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

Web link 4.4 compat #84

Merged
merged 3 commits into from Nov 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 1 addition & 2 deletions composer.json
Expand Up @@ -32,8 +32,7 @@
"symfony/framework-bundle": "^3.4 || ^4.0 || ^5.0",
"symfony/phpunit-bridge": "^4.3.5 || ^5.0",
"symfony/twig-bundle": "^3.4 || ^4.0 || ^5.0",
"symfony/web-link": "^3.4 || ^4.0 || ^5.0",
"fig/link-util": "^1.0"
"symfony/web-link": "^3.4 || ^4.0 || ^5.0"
},
"extra": {
"thanks": {
Expand Down
30 changes: 24 additions & 6 deletions src/EventListener/PreLoadAssetsEventListener.php
Expand Up @@ -9,8 +9,10 @@

namespace Symfony\WebpackEncoreBundle\EventListener;

use Fig\Link\GenericLinkProvider;
use Fig\Link\Link;
use Fig\Link\GenericLinkProvider as FigGenericLinkProvider;
use Fig\Link\Link as FigLink;
use Symfony\Component\WebLink\GenericLinkProvider;
use Symfony\Component\WebLink\Link;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\WebpackEncoreBundle\Asset\TagRenderer;
Expand Down Expand Up @@ -39,16 +41,20 @@ public function onKernelResponse($event)
$request = $event->getRequest();

if (null === $linkProvider = $request->attributes->get('_links')) {
$request->attributes->set('_links', new GenericLinkProvider());
$request->attributes->set(
'_links',
// For backwards-compat with symfony/web-link 4.3 and lower
class_exists(GenericLinkProvider::class) ? new GenericLinkProvider() : new FigGenericLinkProvider()
);
}

/** @var GenericLinkProvider $linkProvider */
/** @var GenericLinkProvider|FigGenericLinkProvider $linkProvider */
$linkProvider = $request->attributes->get('_links');
$defaultAttributes = $this->tagRenderer->getDefaultAttributes();
$crossOrigin = $defaultAttributes['crossorigin'] ?? false;

foreach ($this->tagRenderer->getRenderedScripts() as $href) {
$link = (new Link('preload', $href))->withAttribute('as', 'script');
$link = ($this->createLink('preload', $href))->withAttribute('as', 'script');

if (false !== $crossOrigin) {
$link = $link->withAttribute('crossorigin', $crossOrigin);
Expand All @@ -58,7 +64,7 @@ public function onKernelResponse($event)
}

foreach ($this->tagRenderer->getRenderedStyles() as $href) {
$link = (new Link('preload', $href))->withAttribute('as', 'style');
$link = ($this->createLink('preload', $href))->withAttribute('as', 'style');

if (false !== $crossOrigin) {
$link = $link->withAttribute('crossorigin', $crossOrigin);
Expand All @@ -77,4 +83,16 @@ public static function getSubscribedEvents()
'kernel.response' => ['onKernelResponse', 50],
];
}

/**
* For backwards-compat with symfony/web-link 4.3 and lower.
*
* @return Link|FigLink
*/
private function createLink(string $rel, string $href)
{
$class = class_exists(Link::class) ? Link::class : FigLink::class;

return new $class($rel, $href);
}
}
20 changes: 13 additions & 7 deletions tests/EventListener/PreLoadAssetsEventListenerTest.php
Expand Up @@ -9,8 +9,10 @@

namespace Symfony\WebpackEncoreBundle\Tests\Asset;

use Fig\Link\GenericLinkProvider;
use Fig\Link\Link;
use Fig\Link\GenericLinkProvider as FigGenericLinkProvider;
use Fig\Link\Link as FigLink;
use Symfony\Component\WebLink\GenericLinkProvider;
use Symfony\Component\WebLink\Link;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
Expand All @@ -35,10 +37,12 @@ public function testItPreloadsAssets()
$listener = new PreLoadAssetsEventListener($tagRenderer);
$listener->onKernelResponse($event);
$this->assertTrue($request->attributes->has('_links'));
/** @var GenericLinkProvider $linkProvider */
/** @var GenericLinkProvider|FigGenericLinkProvider $linkProvider */
$linkProvider = $request->attributes->get('_links');
$this->assertInstanceOf(GenericLinkProvider::class, $linkProvider);
/** @var Link[] $links */

$expectedProviderClass = class_exists(GenericLinkProvider::class) ? GenericLinkProvider::class : FigGenericLinkProvider::class;
$this->assertInstanceOf($expectedProviderClass, $linkProvider);
/** @var Link[]|FigLink[] $links */
$links = array_values($linkProvider->getLinks());
$this->assertCount(2, $links);
$this->assertSame('/file1.js', $links[0]->getHref());
Expand All @@ -58,14 +62,16 @@ public function testItReusesExistingLinkProvider()
$tagRenderer->expects($this->once())->method('getRenderedStyles')->willReturn([]);

$request = new Request();
$linkProvider = new GenericLinkProvider([new Link('preload', 'bar.js')]);
$linkProviderClass = class_exists(GenericLinkProvider::class) ? GenericLinkProvider::class : FigGenericLinkProvider::class;
$linkClass = class_exists(Link::class) ? Link::class : FigLink::class;
$linkProvider = new $linkProviderClass([new $linkClass('preload', 'bar.js')]);
$request->attributes->set('_links', $linkProvider);

$response = new Response();
$event = $this->createResponseEvent($request, HttpKernelInterface::MASTER_REQUEST, $response);
$listener = new PreLoadAssetsEventListener($tagRenderer);
$listener->onKernelResponse($event);
/** @var GenericLinkProvider $linkProvider */
/** @var GenericLinkProvider|FigGenericLinkProvider $linkProvider */
$linkProvider = $request->attributes->get('_links');
$this->assertCount(2, $linkProvider->getLinks());
}
Expand Down
25 changes: 18 additions & 7 deletions tests/IntegrationTest.php
Expand Up @@ -14,7 +14,6 @@
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Bundle\TwigBundle\TwigBundle;
use Symfony\Bundle\TwigBundle\Controller\ExceptionController;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\Alias;
use Symfony\Component\DependencyInjection\ContainerBuilder;
Expand All @@ -23,6 +22,7 @@
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\HttpKernel\Log\Logger;
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
use Symfony\Component\Routing\RouteCollectionBuilder;
use Symfony\WebpackEncoreBundle\Asset\EntrypointLookupCollectionInterface;
use Symfony\WebpackEncoreBundle\Asset\EntrypointLookupInterface;
Expand Down Expand Up @@ -178,7 +178,7 @@ public function testAutowireDefaultBuildArgument()
}
}

class WebpackEncoreIntegrationTestKernel extends Kernel
abstract class AbstractWebpackEncoreIntegrationTestKernel extends Kernel
{
use MicroKernelTrait;

Expand All @@ -204,11 +204,6 @@ public function registerBundles()
];
}

protected function configureRoutes(RouteCollectionBuilder $routes)
{
$routes->add('/foo', 'kernel:'.(parent::VERSION_ID >= 40100 ? ':' : '').'renderFoo');
}

protected function configureContainer(ContainerBuilder $container, LoaderInterface $loader)
{
$container->loadFromExtension('framework', [
Expand Down Expand Up @@ -269,6 +264,22 @@ public function renderFoo()
}
}

if (method_exists(AbstractWebpackEncoreIntegrationTestKernel::class, 'configureRouting')) {
class WebpackEncoreIntegrationTestKernel extends AbstractWebpackEncoreIntegrationTestKernel {
protected function configureRouting(RoutingConfigurator $routes): void
{
$routes->add('/foo', 'kernel:'.(parent::VERSION_ID >= 40100 ? ':' : '').'renderFoo');
}
}
} else {
class WebpackEncoreIntegrationTestKernel extends AbstractWebpackEncoreIntegrationTestKernel {
protected function configureRoutes(RouteCollectionBuilder $routes)
{
$routes->add('/foo', 'kernel:'.(parent::VERSION_ID >= 40100 ? ':' : '').'renderFoo');
}
}
}

class WebpackEncoreCacheWarmerTester
{
private $entrypointCacheWarmer;
Expand Down