From 890ed170909a1ea80b1dbee4e38c4e5b3a2a968a Mon Sep 17 00:00:00 2001 From: Nicolas Lemoine Date: Thu, 25 Mar 2021 17:00:12 +0100 Subject: [PATCH] AssetFactory // Add support for inline scripts --- src/AssetFactory.php | 12 ++++++++++ tests/phpunit/Unit/AssetFactoryTest.php | 29 ++++++++++++++++++++++++- 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/src/AssetFactory.php b/src/AssetFactory.php index b6cb8c1..4d36eea 100644 --- a/src/AssetFactory.php +++ b/src/AssetFactory.php @@ -74,6 +74,18 @@ public static function create(array $config): Asset $inFooter ? $asset->isInFooter() : $asset->isInHeader(); + + if (!empty($config['inline']['before']) && is_array($config['inline']['before'])) { + foreach ($config['inline']['before'] as $script) { + $asset->prependInlineScript($script); + } + } + + if (!empty($config['inline']['after']) && is_array($config['inline']['after'])) { + foreach ($config['inline']['after'] as $script) { + $asset->appendInlineScript($script); + } + } } if ($class === Style::class) { diff --git a/tests/phpunit/Unit/AssetFactoryTest.php b/tests/phpunit/Unit/AssetFactoryTest.php index 7f227f2..70411a4 100644 --- a/tests/phpunit/Unit/AssetFactoryTest.php +++ b/tests/phpunit/Unit/AssetFactoryTest.php @@ -129,6 +129,33 @@ public function provideDependencies(): \Generator ]; } + /** + * @test + */ + public function testInlineScripts(): void + { + + $inlineScripts = [ + 'before' => [ + 'var before = "foo"', + ], + 'after' => [ + 'var after = "bar"', + ], + ]; + + $asset = AssetFactory::create( + [ + 'handle' => 'foo', + 'url' => 'foo.js', + 'type' => Script::class, + 'inline' => $inlineScripts, + ] + ); + + static::assertSame($inlineScripts, $asset->inlineScripts()); + } + /** * @test */ @@ -182,7 +209,7 @@ public function testInvalidType(): void public function testCreateFromFile(): void { $content = <<