Skip to content

Commit

Permalink
New method Manifest::getAll()
Browse files Browse the repository at this point in the history
  • Loading branch information
michaljurecko committed Dec 5, 2018
1 parent 3430e21 commit 49632d9
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 17 deletions.
24 changes: 17 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,22 +173,18 @@ Generated output:

Output URL type - relative or absolute - is defined by fourth macro parameter or using `absolute` key (default `false`).

If `absolute => true` is set or asset path is prefixed with `//` eg. (`//assets/js/main.js`), the absolute URL (`$baseUrl` is used) will be generated instead of a relative URL (`$basePath` is used).
If `absolute => true` or asset path is prefixed with `//` eg. (`//assets/js/main.js`), the absolute URL will be generated instead of a relative URL.

```latte
{asset 'js/vendor.js'}
{asset '//js/vendor.js'}
{asset 'js/vendor.js', absolute => true}
{asset 'js/vendor.js', absolute => false}
{asset 'js/vendor.js'} {* equal to {asset 'js/vendor.js', absolute => false} *}
{asset '//js/vendor.js'} {* equal to {asset 'js/vendor.js', absolute => true} *}
```

Generated output:
```html
<script src="/base/path/js/vendor.d67fbce193.js"></script>
<script src="http://www.example.com/base/path/js/vendor.d67fbce193.js"></script>
<script src="/base/path/js/vendor.d67fbce193.js"></script>
<script src="http://www.example.com/base/path/js/vendor.d67fbce193.js"></script>
```

Expand Down Expand Up @@ -217,6 +213,10 @@ assetMacro:
- versions.json
- manifest.json
- rev-manifest.json
# Absolute path to assets dir
assetsPath: %wwwDir%/ # %wwwDir%/assets
# Public path to "assetsPath"
publicPath: / # /assets
# Action if missing asset file: exception, notice, or ignore
missingAsset: notice
# Action if missing manifest file: exception, notice, or ignore
Expand All @@ -227,6 +227,16 @@ assetMacro:
format: '%%url%%' # character % is escaped by %%
```

## `ManifestService`

It is also possible to access the manifest from your code using `Webrouse\AssetMacro\ManifestService` (from DI container).

```php
/** @var ManifestService $manifestService */
$cssAssets = $manifestService->getManifest()->getAll('/.*\.css$/');
```

## Examples

Examples based on [nette/sandbox](https://github.com/nette/sandbox):
Expand Down
28 changes: 28 additions & 0 deletions src/Webrouse/AssetMacro/Manifest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Nette\Utils\Json;
use Nette\Utils\JsonException;
use Nette\Utils\Strings;
use Webrouse\AssetMacro\Exceptions\AssetNotFoundException;
use Webrouse\AssetMacro\Exceptions\ManifestJsonException;
use Webrouse\AssetMacro\Exceptions\RevisionNotFound;
Expand Down Expand Up @@ -47,6 +48,33 @@ public function __construct(Config $config, ?string $path, array $data = null)
}


/**
* @param null|string|callable $filter regexp pattern or callable
* @param bool $needed
* @return array|Asset[]
*/
public function getAll($filter = null, $needed = true): array
{
assert($filter === null || is_string($filter) || is_callable($filter));

$out = [];
foreach (array_keys($this->data) as $asset) {
if (is_string($filter)) {
if (!Strings::match($asset, $filter)) {
continue;
}
} elseif (is_callable($filter)) {
if (!$filter($asset)) {
continue;
}
}
$out[$asset] = $this->getAsset($asset, $needed);
}

return $out;
}


public function getAsset(string $path, bool $needed = true): ?Asset
{
$revision = self::getRevision($path, $needed);
Expand Down
53 changes: 51 additions & 2 deletions tests/AssetMacroTests/PathsTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ declare(strict_types=1);
namespace Webrouse\AssetMacro;

use Latte;
use Nette\Utils\Strings;
use Tester\Assert;

include '../bootstrap.php';
Expand Down Expand Up @@ -447,9 +448,57 @@ class PathsTest extends TestCase
}


protected function createLatte(array $config = []): Latte\Engine
public function testManifestGetAll(): void
{
return parent::createLatte(array_merge([
$service = $this->createService();
$assets = $service->getManifest()->getAll();
Assert::same([
'assets/compiled/main.css' => 'http://www.example.com/base/path/fixtures/assets/compiled/main.b82916016edc7.css',
'assets/compiled/main.js' => 'http://www.example.com/base/path/fixtures/assets/compiled/main.fc730c89c4255.js',
'assets/compiled/other.js' => 'http://www.example.com/base/path/fixtures/assets/compiled/other.8h9hfj5vvh4jf.js',
'assets/compiled/escape.js' => '__null__',
], array_map(function (?Asset $asset) use ($service) {
return $asset ? $service->formatOutput($asset, '%url%', true) : '__null__';
}, $assets));
}


public function testManifestGetAllFilterGlob(): void
{
$service = $this->createService();
$assets1 = $service->getManifest()->getAll('/.*\.css$/');
Assert::same([
'assets/compiled/main.css' => 'http://www.example.com/base/path/fixtures/assets/compiled/main.b82916016edc7.css',
], array_map(function (?Asset $asset) use ($service) {
return $asset ? $service->formatOutput($asset, '%url%', true) : '__null__';
}, $assets1));

$assets2 = $service->getManifest()->getAll('/.*\.js/');
Assert::same([
'assets/compiled/main.js' => 'http://www.example.com/base/path/fixtures/assets/compiled/main.fc730c89c4255.js',
'assets/compiled/other.js' => 'http://www.example.com/base/path/fixtures/assets/compiled/other.8h9hfj5vvh4jf.js',
'assets/compiled/escape.js' => '__null__',
], array_map(function (?Asset $asset) use ($service) {
return $asset ? $service->formatOutput($asset, '%url%', true) : '__null__';
}, $assets2));
}


public function testManifestGetAllFilterCallback(): void
{
$service = $this->createService();
$assets = $service->getManifest()->getAll(function (string $asset) {return Strings::contains($asset, 'other.js');});
Assert::same([
'assets/compiled/other.js' => 'http://www.example.com/base/path/fixtures/assets/compiled/other.8h9hfj5vvh4jf.js',
], array_map(function (?Asset $asset) use ($service) {
return $asset ? $service->formatOutput($asset, '%url%', true) : '__null__';
}, $assets));
}


protected function createService(array $config = []): ManifestService
{
return parent::createService(array_merge([
'missingAsset' => 'ignore',
'missingManifest' => 'ignore',
'missingRevision' => 'ignore',
Expand Down
53 changes: 51 additions & 2 deletions tests/AssetMacroTests/VersionsTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ declare(strict_types=1);
namespace Webrouse\AssetMacro;

use Latte;
use Nette\Utils\Strings;
use Tester\Assert;

include '../bootstrap.php';
Expand Down Expand Up @@ -448,9 +449,57 @@ class VersionsTest extends TestCase
}


protected function createLatte(array $config = []): Latte\Engine
public function testManifestGetAll(): void
{
return parent::createLatte(array_merge([
$service = $this->createService();
$assets = $service->getManifest()->getAll();
Assert::same([
'assets/compiled/main.css' => 'http://www.example.com/base/path/fixtures/assets/compiled/main.css?v=32ecae4b82916016edc74db0f5a11ceb',
'assets/compiled/main.js' => 'http://www.example.com/base/path/fixtures/assets/compiled/main.js?v=8c48f58dfc7330c89c42550963c81546',
'assets/compiled/other.js' => 'http://www.example.com/base/path/fixtures/assets/compiled/other.js?v=8h9hfj5vvh4jffokvzj6h1fjfnfd9c',
'assets/compiled/escape.js' => 'http://www.example.com/base/path/fixtures/assets/compiled/escape.js?v="escape"',
], array_map(function (?Asset $asset) use ($service) {
return $asset ? $service->formatOutput($asset, '%url%', true) : '__null__';
}, $assets));
}


public function testManifestGetAllFilterGlob(): void
{
$service = $this->createService();
$assets1 = $service->getManifest()->getAll('/.*\.css$/');
Assert::same([
'assets/compiled/main.css' => 'http://www.example.com/base/path/fixtures/assets/compiled/main.css?v=32ecae4b82916016edc74db0f5a11ceb',
], array_map(function (?Asset $asset) use ($service) {
return $asset ? $service->formatOutput($asset, '%url%', true) : '__null__';
}, $assets1));

$assets2 = $service->getManifest()->getAll('/.*\.js/');
Assert::same([
'assets/compiled/main.js' => 'http://www.example.com/base/path/fixtures/assets/compiled/main.js?v=8c48f58dfc7330c89c42550963c81546',
'assets/compiled/other.js' => 'http://www.example.com/base/path/fixtures/assets/compiled/other.js?v=8h9hfj5vvh4jffokvzj6h1fjfnfd9c',
'assets/compiled/escape.js' => 'http://www.example.com/base/path/fixtures/assets/compiled/escape.js?v="escape"',
], array_map(function (?Asset $asset) use ($service) {
return $asset ? $service->formatOutput($asset, '%url%', true) : '__null__';
}, $assets2));
}


public function testManifestGetAllFilterCallback(): void
{
$service = $this->createService();
$assets = $service->getManifest()->getAll(function (string $asset) {return Strings::contains($asset, 'other.js');});
Assert::same([
'assets/compiled/other.js' => 'http://www.example.com/base/path/fixtures/assets/compiled/other.js?v=8h9hfj5vvh4jffokvzj6h1fjfnfd9c',
], array_map(function (?Asset $asset) use ($service) {
return $asset ? $service->formatOutput($asset, '%url%', true) : '__null__';
}, $assets));
}


protected function createService(array $config = []): ManifestService
{
return parent::createService(array_merge([
'missingAsset' => 'ignore',
'missingManifest' => 'ignore',
'missingRevision' => 'ignore',
Expand Down
16 changes: 10 additions & 6 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,26 @@ protected function createLatte(array $config = []): Latte\Engine
AssetMacro::install($engine->getCompiler());
};

$latte->addProvider(AssetMacro::MANIFEST_PROVIDER, $this->createService($config));

return $latte;
}


protected function createService(array $config = []): ManifestService
{
/** @var IRequest|Mock $httpRequest */
$httpRequest = Mockery::mock(IRequest::class);
$httpRequest->shouldReceive('getUrl')->andReturn($this->getFakeUrl());

$config = new Config(array_merge(Extension::DEFAULTS, [
$configService = new Config(array_merge(Extension::DEFAULTS, [
'cache' => false,
'assetsPath' => WWW_FIXTURES_DIR,
'publicPath' => '/fixtures',
'format' => '%url%',
], $config));

$service = new ManifestService($config, $httpRequest);

$latte->addProvider(AssetMacro::MANIFEST_PROVIDER, $service);

return $latte;
return new ManifestService($configService, $httpRequest);
}


Expand Down

0 comments on commit 49632d9

Please sign in to comment.