Skip to content

Commit

Permalink
AssetLocator: entrypoints with absolute url should not be changed (#37)
Browse files Browse the repository at this point in the history
* AssetLocator: move locateInBuildDirectory and locateInPublicPath methods login to method locateInPath

* AssetLocator: entrypoints with absolute url should not be changed
  • Loading branch information
martenb authored Oct 11, 2021
1 parent 21a23a0 commit 105ec89
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
24 changes: 18 additions & 6 deletions src/AssetLocator.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,33 @@ public function __construct(
$this->ignoredAssetNames = $ignoredAssetNames;
}

public function locateInPublicPath(string $asset): string
private function locateInPath(string $path, string $asset): string
{
if ($this->devServer->isAvailable() && \in_array($asset, $this->ignoredAssetNames, true)) {
return 'data:,';
}

return \rtrim($this->publicPathProvider->getPublicPath(), '/') . '/' . \ltrim($this->assetResolver->resolveAssetName($asset), '/');
$assetName = $this->assetResolver->resolveAssetName($asset);

if ($this->isAbsoluteUrl($assetName)) {
return $assetName;
}

return \rtrim($path, '/') . '/' . \ltrim($assetName, '/');
}

public function locateInPublicPath(string $asset): string
{
return $this->locateInPath($this->publicPathProvider->getPublicPath(), $asset);
}

public function locateInBuildDirectory(string $asset): string
{
if ($this->devServer->isAvailable() && \in_array($asset, $this->ignoredAssetNames, true)) {
return 'data:,';
}
return $this->locateInPath($this->directoryProvider->getBuildDirectory(), $asset);
}

return \rtrim($this->directoryProvider->getBuildDirectory(), '/') . '/' . \ltrim($this->assetResolver->resolveAssetName($asset), '/');
private function isAbsoluteUrl(string $url): bool
{
return strpos($url, '://') !== false || substr($url, 0, 2) === '//';
}
}
15 changes: 12 additions & 3 deletions tests/AssetLocatorTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -19,36 +19,45 @@ final class AssetLocatorTest extends TestCase
{
$directoryProvider = createBuildDirectoryProvider('/home/user');
$pathProvider = createPublicPathProvider('/foo');
$assetResolver = createAssetNameResolver(['bar.js' => 'bar.js']);
$assetResolver = createAssetNameResolver(['bar.js' => 'bar.js', 'foo1.js' => 'http://localhost/foo1.js', 'foo2.js' => '//localhost/foo2.js']);
$devServer = createDisabledDevServer();

$assetLocator = new AssetLocator($directoryProvider, $pathProvider, $assetResolver, $devServer, []);
Assert::same('/home/user/bar.js', $assetLocator->locateInBuildDirectory('bar.js'));
Assert::same('http://localhost/foo1.js', $assetLocator->locateInBuildDirectory('foo1.js'));
Assert::same('//localhost/foo2.js', $assetLocator->locateInBuildDirectory('foo2.js'));
}

public function testLocateInPublicPath(): void
{
$directoryProvider = createBuildDirectoryProvider('/home/user');
$pathProvider = createPublicPathProvider('/foo');
$assetResolver = createAssetNameResolver(['bar.js' => 'bar.js']);
$assetResolver = createAssetNameResolver(['bar.js' => 'bar.js', 'foo1.js' => 'http://localhost/foo1.js', 'foo2.js' => '//localhost/foo2.js']);
$devServer = createDisabledDevServer();

$assetLocator = new AssetLocator($directoryProvider, $pathProvider, $assetResolver, $devServer, []);
Assert::same('/foo/bar.js', $assetLocator->locateInPublicPath('bar.js'));
Assert::same('http://localhost/foo1.js', $assetLocator->locateInBuildDirectory('foo1.js'));
Assert::same('//localhost/foo2.js', $assetLocator->locateInBuildDirectory('foo2.js'));
}

public function testIgnoredAssets(): void
{
$directoryProvider = createBuildDirectoryProvider('/home/user');
$pathProvider = createPublicPathProvider('/foo');
$assetResolver = createAssetNameResolver(['bar.js' => 'bar.js']);
$assetResolver = createAssetNameResolver(['bar.js' => 'bar.js', 'foo1.js' => 'http://localhost/foo1.js', 'foo2.js' => '//localhost/foo2.js']);
$devServer = createEnabledDevServer(true);

$assetLocator = new AssetLocator($directoryProvider, $pathProvider, $assetResolver, $devServer, ['foo.css']);
Assert::same('data:,', $assetLocator->locateInBuildDirectory('foo.css'));
Assert::same('data:,', $assetLocator->locateInPublicPath('foo.css'));
Assert::same('/home/user/bar.js', $assetLocator->locateInBuildDirectory('bar.js'));
Assert::same('/foo/bar.js', $assetLocator->locateInPublicPath('bar.js'));
Assert::same('http://localhost/foo1.js', $assetLocator->locateInBuildDirectory('foo1.js'));
Assert::same('http://localhost/foo1.js', $assetLocator->locateInPublicPath('foo1.js'));
Assert::same('//localhost/foo2.js', $assetLocator->locateInBuildDirectory('foo2.js'));
Assert::same('//localhost/foo2.js', $assetLocator->locateInPublicPath('foo2.js'));
Assert::same('data:,', $assetLocator->locateInBuildDirectory('foo.css'));
}
}

Expand Down

0 comments on commit 105ec89

Please sign in to comment.