Skip to content

Commit 1b2ded7

Browse files
author
Jannik
authored
Merge pull request #36 from tkasper/allow_cors_embed
2 parents 06d72d7 + 8366d52 commit 1b2ded7

File tree

6 files changed

+87
-41
lines changed

6 files changed

+87
-41
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
2.6.8
2+
=====
3+
4+
* (improvement) Add global `allow_cors` option for assets.
5+
6+
17
2.6.7
28
=====
39

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ becklyn_assets:
115115
public_path: '%kernel.project_dir%/public'
116116
# relative path to the directory, where the assets are copied to (relative to `public_path`)
117117
output_dir: 'assets'
118+
# allow crossorigin assets e.g needed for basic auth if using safari browser
119+
allow_cors: true
118120
```
119121
120122
Commands

src/DependencyInjection/BecklynAssetsConfiguration.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ public function getConfigTreeBuilder ()
3030
->defaultValue('assets')
3131
->info("The relative path to the assets output dir. Relative to `public_path`.")
3232
->end()
33+
->booleanNode("allow_cors")
34+
->defaultFalse()
35+
->end()
3336
->arrayNode("dependency_maps")
3437
->setDeprecated("The `becklyn_assets.dependency_maps` option is deprecated, as the the maps will always be automatically loaded.")
3538
->scalarPrototype()->end()

src/DependencyInjection/BecklynAssetsExtension.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Becklyn\AssetsBundle\DependencyInjection;
44

5+
use Becklyn\AssetsBundle\Html\AssetHtmlGenerator;
56
use Becklyn\AssetsBundle\Namespaces\NamespaceRegistry;
67
use Becklyn\AssetsBundle\RouteLoader\AssetsRouteLoader;
78
use Becklyn\AssetsBundle\Storage\AssetStorage;
@@ -43,6 +44,9 @@ public function load (array $configs, ContainerBuilder $container) : void
4344

4445
$container->getDefinition(AssetsRouteLoader::class)
4546
->setArgument('$outputDir', $config["output_dir"]);
47+
48+
$container->getDefinition(AssetHtmlGenerator::class)
49+
->setArgument('$allowCors', $config["allow_cors"]);
4650
}
4751

4852

src/Html/AssetHtmlGenerator.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,21 @@ class AssetHtmlGenerator
5151
private $htmlBuilder;
5252

5353

54+
/**
55+
* @var bool
56+
*/
57+
private $allowCors;
58+
59+
5460
/**
5561
*/
5662
public function __construct (
5763
AssetsRegistry $registry,
5864
AssetUrl $assetUrl,
5965
FileTypeRegistry $fileTypeRegistry,
6066
bool $isDebug,
61-
DependencyMapFactory $dependencyMapFactory
67+
DependencyMapFactory $dependencyMapFactory,
68+
bool $allowCors = false
6269
)
6370
{
6471
$this->registry = $registry;
@@ -67,6 +74,7 @@ public function __construct (
6774
$this->isDebug = $isDebug;
6875
$this->dependencyMap = $dependencyMapFactory->getDependencyMap();
6976
$this->htmlBuilder = new HtmlBuilder();
77+
$this->allowCors = $allowCors;
7078
}
7179

7280

@@ -144,6 +152,11 @@ public function linkAssets (array $assetPaths, bool $withDependencies = true) :
144152
}
145153
}
146154

155+
if ($this->allowCors)
156+
{
157+
$embed->setAttribute("crossorigin", "anonymous");
158+
}
159+
147160
try
148161
{
149162
$html .= $this->htmlBuilder->buildElement($fileType->buildElementForEmbed($embed));

tests/Html/AssetHtmlGeneratorTest.php

Lines changed: 58 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -38,46 +38,6 @@ protected function setUp () : void
3838
}
3939

4040

41-
protected function buildGenerator (bool $isDebug)
42-
{
43-
$registry = $this->getMockBuilder(AssetsRegistry::class)
44-
->disableOriginalConstructor()
45-
->getMock();
46-
47-
$assetUrl = $this->getMockBuilder(AssetUrl::class)
48-
->disableOriginalConstructor()
49-
->getMock();
50-
51-
$importRewriter = $this->getMockBuilder(CssImportRewriter::class)
52-
->disableOriginalConstructor()
53-
->getMock();
54-
55-
$importRewriter
56-
->method("rewriteRelativeImports")
57-
->willReturnArgument(1);
58-
59-
$fileTypeRegistry = new FileTypeRegistry(new GenericFile(), new ServiceLocator([
60-
"js" => function () { return new JavaScriptFile(); },
61-
"css" => function () use ($importRewriter) { return new CssFile($importRewriter); },
62-
]));
63-
64-
$dependencyMapFactory = $this->getMockBuilder(DependencyMapFactory::class)
65-
->disableOriginalConstructor()
66-
->getMock();
67-
68-
$dependencyMapFactory
69-
->method("getDependencyMap")
70-
->willReturn(new DependencyMap());
71-
72-
$assetUrl
73-
->method("generateUrl")
74-
->willReturnCallback(function (Asset $asset) { return $asset->getAssetPath(); });
75-
76-
$generator = new AssetHtmlGenerator($registry, $assetUrl, $fileTypeRegistry, $isDebug, $dependencyMapFactory);
77-
return [$generator, $registry, $assetUrl, $fileTypeRegistry];
78-
}
79-
80-
8141
public function testDebugJS () : void
8242
{
8343
/**
@@ -314,4 +274,62 @@ public function testHttpImports (array $assets, string $expectedOutput) : void
314274
$html = $generator->linkAssets($assets);
315275
self::assertSame($expectedOutput, $html);
316276
}
277+
278+
279+
/**
280+
*
281+
*/
282+
public function testAllowCors () : void
283+
{
284+
/**
285+
* @var AssetHtmlGenerator
286+
* @var \PHPUnit_Framework_MockObject_MockObject $registry
287+
*/
288+
[$generator] = $this->buildGenerator(false, true);
289+
290+
self::assertSame(
291+
'<script crossorigin="anonymous" defer src="http://example.org/test.js"></script>',
292+
$generator->linkAssets(["http://example.org/test.js"])
293+
);
294+
}
295+
296+
297+
protected function buildGenerator (bool $isDebug, bool $allowCors = false)
298+
{
299+
$registry = $this->getMockBuilder(AssetsRegistry::class)
300+
->disableOriginalConstructor()
301+
->getMock();
302+
303+
$assetUrl = $this->getMockBuilder(AssetUrl::class)
304+
->disableOriginalConstructor()
305+
->getMock();
306+
307+
$importRewriter = $this->getMockBuilder(CssImportRewriter::class)
308+
->disableOriginalConstructor()
309+
->getMock();
310+
311+
$importRewriter
312+
->method("rewriteRelativeImports")
313+
->willReturnArgument(1);
314+
315+
$fileTypeRegistry = new FileTypeRegistry(new GenericFile(), new ServiceLocator([
316+
"js" => function () { return new JavaScriptFile(); },
317+
"css" => function () use ($importRewriter) { return new CssFile($importRewriter); },
318+
]));
319+
320+
$dependencyMapFactory = $this->getMockBuilder(DependencyMapFactory::class)
321+
->disableOriginalConstructor()
322+
->getMock();
323+
324+
$dependencyMapFactory
325+
->method("getDependencyMap")
326+
->willReturn(new DependencyMap());
327+
328+
$assetUrl
329+
->method("generateUrl")
330+
->willReturnCallback(function (Asset $asset) { return $asset->getAssetPath(); });
331+
332+
$generator = new AssetHtmlGenerator($registry, $assetUrl, $fileTypeRegistry, $isDebug, $dependencyMapFactory, $allowCors);
333+
return [$generator, $registry, $assetUrl, $fileTypeRegistry];
334+
}
317335
}

0 commit comments

Comments
 (0)