Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/Attribute/Asset/Css.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public function __construct(
public bool $preprocess = false,
public string $category = 'component',
public readonly array $attributes = [],
public readonly bool $silenceNoMatches = false,
) {
if (str_starts_with($path, '/')) {
throw new \LogicException('Path must not begin with forward-slash');
Expand All @@ -49,8 +50,9 @@ public function getLibraryPaths(): AssetLibraryPaths
$glob = \glob($pattern);
if (false === $glob || [] === $glob) {
if (!\file_exists($pattern)) {
// No exceptions when globs are used, no files are allowed.
throw new \LogicException('File does not exist: ' . $pattern);
return $this->silenceNoMatches
? new AssetLibraryPaths()
: throw new \LogicException('File does not exist: ' . $pattern);
}

return new AssetLibraryPaths([['css', $this->category, $pattern]]);
Expand Down
6 changes: 4 additions & 2 deletions src/Attribute/Asset/Js.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public function __construct(
public bool $minified = false,
public bool $preprocess = false,
public readonly array $attributes = [],
public readonly bool $silenceNoMatches = false,
) {
if (str_starts_with($path, '/')) {
throw new \LogicException('Path must not begin with forward-slash');
Expand All @@ -46,8 +47,9 @@ public function getLibraryPaths(): AssetLibraryPaths
$glob = \glob($pattern);
if (false === $glob || [] === $glob) {
if (!\file_exists($pattern)) {
// No exceptions when globs are used, no files are allowed.
throw new \LogicException('File does not exist: ' . $pattern);
return $this->silenceNoMatches
? new AssetLibraryPaths()
: throw new \LogicException('File does not exist: ' . $pattern);
}

return new AssetLibraryPaths([['js', $pattern]]);
Expand Down
43 changes: 43 additions & 0 deletions tests/PintoAssetGlobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
declare(strict_types=1);

use PHPUnit\Framework\TestCase;
use Pinto\Asset\AssetLibraryPaths;
use Pinto\Attribute\Asset\Css;
use Pinto\Attribute\Asset\Js;
use Pinto\Library\LibraryBuilder;
use Pinto\tests\fixtures\Lists\AssetGlob\PintoListAssetGlob;

use function Safe\realpath;

/**
* Test asset globs.
*
Expand Down Expand Up @@ -47,4 +50,44 @@ public function testGlob(): void
]],
], iterator_to_array(LibraryBuilder::expandLibraryPaths(PintoListAssetGlob::Wildcard)));
}

public function testGlobNoMatchesExceptionCss(): void
{
$css = new Css('no-styles*.css');
$css->setPath(realpath(__DIR__ . '/fixtures/Assets/PintoListAssetGlob'));
static::expectExceptionMessage('File does not exist');
$css->getLibraryPaths();
}

public function testGlobNoMatchesExceptionJs(): void
{
$js = new Js('no-styles*.js');
$js->setPath(realpath(__DIR__ . '/fixtures/Assets/PintoListAssetGlob'));
static::expectExceptionMessage('File does not exist');
$js->getLibraryPaths();
}

public function testGlobNoMatchesExceptionSilencedCss(): void
{
$css = new Css('no-styles*.css', silenceNoMatches: true);
$css->setPath(realpath(__DIR__ . '/fixtures/Assets/PintoListAssetGlob'));
static::assertEquals(new AssetLibraryPaths(), $css->getLibraryPaths());

// When no glob is used, but glob is silenced, and no matches, ensure exception is still thrown:
$css = new Css('no-styles.css', silenceNoMatches: true);
$css->setPath(realpath(__DIR__ . '/fixtures/Assets/PintoListAssetGlob'));
static::assertEquals(new AssetLibraryPaths(), $css->getLibraryPaths());
}

public function testGlobNoMatchesExceptionSilencedJs(): void
{
$js = new Js('no-styles*.css', silenceNoMatches: true);
$js->setPath(realpath(__DIR__ . '/fixtures/Assets/PintoListAssetGlob'));
static::assertEquals(new AssetLibraryPaths(), $js->getLibraryPaths());

// When no glob is used, but glob is silenced, and no matches, ensure exception is still thrown:
$js = new Js('no-styles.css', silenceNoMatches: true);
$js->setPath(realpath(__DIR__ . '/fixtures/Assets/PintoListAssetGlob'));
static::assertEquals(new AssetLibraryPaths(), $js->getLibraryPaths());
}
}
Loading