Skip to content

Commit

Permalink
Merge pull request #25 from nlemoine/master
Browse files Browse the repository at this point in the history
AssetFactory // Add support for inline scripts
  • Loading branch information
Chrico authored Mar 26, 2021
2 parents 8a69055 + 890ed17 commit c14c733
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/AssetFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
29 changes: 28 additions & 1 deletion tests/phpunit/Unit/AssetFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -182,7 +209,7 @@ public function testInvalidType(): void
public function testCreateFromFile(): void
{
$content = <<<FILE
<?php
<?php
use Inpsyde\Assets\Asset;
use Inpsyde\Assets\Script;
use Inpsyde\Assets\Style;
Expand Down

0 comments on commit c14c733

Please sign in to comment.