Skip to content

Commit

Permalink
replaced AssetManager::registerMultiple() by AssetManager::register(A…
Browse files Browse the repository at this point in the history
…sset ...$assets)
  • Loading branch information
Chrico committed Apr 4, 2018
1 parent c31d180 commit b0a74ca
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 29 deletions.
9 changes: 2 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,7 @@ Inpsyde\Assets\assetManager()
->register($myStyle);

// or
Inpsyde\Assets\assetManager()->registerMultiple(
[
$myScript,
$myStyle,
]
);
Inpsyde\Assets\assetManager()->register($myScript, $myStyle);
```

## Using `AssetFactory`
Expand Down Expand Up @@ -75,7 +70,7 @@ In your application you can create all assets from that file by using the `Inpsy
<?php
$assets = Inpsyde\Assets\assetFactory()->createFromFile('config/asset.php');

Inpsyde\Assets\assetManager()->registerMultiple($assets);
Inpsyde\Assets\assetManager()->register(...$assets);
```

## Assets
Expand Down
28 changes: 17 additions & 11 deletions src/AssetManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ public function useDefaultHandlers(): AssetManager
{
$this->handlers = [
Asset::TYPE_SCRIPT => new ScriptHandler(wp_scripts()),
Asset::TYPE_STYLE => new StyleHandler(wp_styles()),
Asset::TYPE_STYLE => new StyleHandler(wp_styles()),
];

return $this;
}

public function withHandler(string $assetType, AssetHandler $handler): AssetManager
{
$this->handlers[$assetType] = $handler;
$this->handlers[ $assetType ] = $handler;

return $this;
}
Expand All @@ -49,7 +49,7 @@ public function handlers(): array
public function useDefaultOutputFilters(): AssetManager
{
$this->filters = [
AsyncStyleOutputFilter::class => new AsyncStyleOutputFilter(),
AsyncStyleOutputFilter::class => new AsyncStyleOutputFilter(),
AsyncScriptOutputFilter::class => new AsyncScriptOutputFilter(),
DeferScriptOutputFilter::class => new DeferScriptOutputFilter(),
];
Expand All @@ -59,7 +59,7 @@ public function useDefaultOutputFilters(): AssetManager

public function withOutputFilter(string $name, AssetOutputFilter $filter): AssetManager
{
$this->filters[$name] = $filter;
$this->filters[ $name ] = $filter;

return $this;
}
Expand All @@ -72,9 +72,15 @@ public function outputFilters(): array
return $this->filters;
}

public function register(Asset $asset): AssetManager
public function register(Asset ...$assets): AssetManager
{
$this->assets["{$asset->type()}_{$asset->handle()}"] = $asset;
array_walk(
$assets,
function (Asset $asset) {
$this->assets[ "{$asset->type()}_{$asset->handle()}" ] = $asset;
}
);


return $this;
}
Expand Down Expand Up @@ -115,10 +121,10 @@ public function setup(): bool

foreach ($this->assets as $asset) {
$type = $asset->type();
if (! isset($this->handlers[$type])) {
if (!isset($this->handlers[ $type ])) {
continue;
}
$handler = $this->handlers[$type];
$handler = $this->handlers[ $type ];
$handler->enqueue($asset);
$this->processFilters($asset, $handler->outputFilterHook());
}
Expand All @@ -127,7 +133,7 @@ public function setup(): bool
}

/**
* @param Asset $asset
* @param Asset $asset
* @param string $hook
*
* @return bool true when at least 1 filter is applied, otherwise false.
Expand All @@ -140,8 +146,8 @@ protected function processFilters(Asset $asset, string $hook): bool
$filters[] = $filter;
}
$filter = (string)$filter;
if (isset($this->filters[$filter])) {
$filters[] = $this->filters[$filter];
if (isset($this->filters[ $filter ])) {
$filters[] = $this->filters[ $filter ];
}
}

Expand Down
11 changes: 8 additions & 3 deletions src/OutputFilter/AsyncStyleOutputFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,15 @@ class AsyncStyleOutputFilter implements AssetOutputFilter

public function __invoke(string $html, Asset $asset): string
{
$url = $asset->url();
$version = $asset->version();
if ( $version !== '' ){
$url = add_query_arg('ver', $version, $url);
}

$output = sprintf(
'<link rel="preload" href="%s?ver=%s" as="style" onload="this.onload=null;this.rel=\'stylesheet\'">',
esc_url($asset->url()),
esc_attr($asset->version())
'<link rel="preload" href="%s" as="style" onload="this.onload=null;this.rel=\'stylesheet\'">',
esc_url($url)
);
$output .= '<noscript>'.$html.'</noscript>';

Expand Down
10 changes: 2 additions & 8 deletions tests/phpunit/Unit/AssetManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,7 @@ public function testRegisterMultiple()

static::assertSame(
$testee,
$testee->registerMultiple(
[
$expectedAsset1,
$expectedAsset2,
]
)
$testee->register($expectedAsset1, $expectedAsset2)
);

static::assertCount(2, $testee->assets());
Expand All @@ -129,8 +124,7 @@ public function testSetup()

$testee = (new AssetManager())
->withHandler(Asset::TYPE_SCRIPT, $expectedHandler)
->register($assetWithMatchingHandler)
->register($assetUndefinedType);
->register($assetWithMatchingHandler, $assetUndefinedType);


Monkey\Actions\expectDone(AssetManager::ACTION_SETUP);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ public function testRender()
$input = '<link rel="stylesheet" url="'.$expectedUrl.'" />';

Monkey\Functions\when('esc_url')->justReturn($expectedUrl);
Monkey\Functions\when('esc_attr')->justReturn($expectedUrl);

$stub = \Mockery::mock(Asset::class);
$stub->expects('url')->once()->andReturn($expectedUrl);
$stub->expects('version')->once()->andReturn('');

$output = $testee($input, $stub);

Expand Down

0 comments on commit b0a74ca

Please sign in to comment.