Skip to content

Commit

Permalink
Prefixed assets with router
Browse files Browse the repository at this point in the history
  • Loading branch information
ElGigi committed Dec 4, 2024
1 parent 933a71a commit 7741f75
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 61 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ All notable changes to this project will be documented in this file. This projec
to [Semantic Versioning] (http://semver.org/). For change log format,
use [Keep a Changelog] (http://keepachangelog.com/).

## [2.3.0] - 2024-12-04

### Added

- Prefixed assets with router
- Conflict with `berlioz/router` < 2.5

## [2.2.0] - 2022-02-11

### Added
Expand Down
85 changes: 44 additions & 41 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,45 +1,48 @@
{
"name": "berlioz/twig-package",
"type": "berlioz-package",
"description": "Twig package for Berlioz Framework",
"license": "MIT",
"homepage": "https://getberlioz.com",
"authors": [
{
"name": "Ronan Giron",
"email": "ronan@getberlioz.com"
}
],
"autoload": {
"psr-4": {
"Berlioz\\Package\\Twig\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Berlioz\\Package\\Twig\\Tests\\": "tests/",
"Berlioz\\Package\\Twig\\TestProject\\": "tests_env/src/"
"name": "berlioz/twig-package",
"type": "berlioz-package",
"description": "Twig package for Berlioz Framework",
"license": "MIT",
"homepage": "https://getberlioz.com",
"authors": [
{
"name": "Ronan Giron",
"email": "ronan@getberlioz.com"
}
],
"autoload": {
"psr-4": {
"Berlioz\\Package\\Twig\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Berlioz\\Package\\Twig\\Tests\\": "tests/",
"Berlioz\\Package\\Twig\\TestProject\\": "tests_env/src/"
},
"exclude-from-classmap": [
"/tests_env/"
]
},
"minimum-stability": "stable",
"require": {
"php": "^8.0",
"ext-intl": "*",
"berlioz/core": "^2.2",
"berlioz/helpers": "^1.0",
"twig/twig": "^3.0"
},
"require-dev": {
"berlioz/http-core": "^2.0",
"berlioz/router": "^2.5",
"phpunit/phpunit": "^9.5"
},
"conflict": {
"berlioz/router": "<2.5"
},
"exclude-from-classmap": [
"/tests_env/"
]
},
"minimum-stability": "stable",
"require": {
"php": "^8.0",
"ext-intl": "*",
"berlioz/core": "^2.2",
"berlioz/helpers": "^1.0",
"twig/twig": "^3.0"
},
"require-dev": {
"berlioz/http-core": "^2.0",
"berlioz/router": "^2.0",
"phpunit/phpunit": "^9.5"
},
"config": {
"berlioz": {
"package": "Berlioz\\Package\\Twig\\BerliozPackage"
"config": {
"berlioz": {
"package": "Berlioz\\Package\\Twig\\BerliozPackage"
}
}
}
}
47 changes: 39 additions & 8 deletions src/Extension/AssetRuntimeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Berlioz\Core\Asset\EntryPoints;
use Berlioz\Core\Asset\Manifest;
use Berlioz\Core\Exception\AssetException;
use Berlioz\Router\Router;
use Throwable;
use Twig\Error\Error;
use Twig\Error\RuntimeError;
Expand All @@ -27,14 +28,20 @@ class AssetRuntimeExtension
const H2PUSH_CACHE_COOKIE = 'h2pushes';
private array $h2pushCache = [];

public function __construct(protected Assets $assets)
{
public function __construct(
protected Assets $assets,
protected Router $router,
) {
// Get cache from cookies
if (isset($_COOKIE[self::H2PUSH_CACHE_COOKIE]) && is_array($_COOKIE[self::H2PUSH_CACHE_COOKIE])) {
$this->h2pushCache = array_keys($_COOKIE[self::H2PUSH_CACHE_COOKIE]);
}
}

protected function getRouter(): Router
{
return $this->router;
}

/**
* Function asset to get generate asset path.
Expand All @@ -58,7 +65,7 @@ public function asset(string $key, ?Manifest $manifest = null): string
throw new RuntimeError(sprintf('Asset "%s" not found in manifest file', $key));
}

return $manifest->get($key);
return $this->getRouter()->finalizePath($manifest->get($key));
} catch (AssetException $exception) {
throw new RuntimeError('Manifest treatment error', previous: $exception);
}
Expand Down Expand Up @@ -120,7 +127,12 @@ public function entryPoints(

$output .= sprintf(
'<script%s></script>',
$this->attributes(array_replace($options, ['src' => $entryPoint])),
$this->attributes(
array_replace(
$options,
['src' => $this->getRouter()->finalizePath($entryPoint)]
)
),
) . PHP_EOL;
break;
case 'css':
Expand All @@ -137,7 +149,10 @@ public function entryPoints(
$this->attributes(
array_replace(
$options,
['rel' => 'stylesheet', 'href' => $entryPoint]
[
'rel' => 'stylesheet',
'href' => $this->getRouter()->finalizePath($entryPoint)
]
)
),
) . PHP_EOL;
Expand Down Expand Up @@ -165,7 +180,23 @@ public function entryPointsList(string|array $entry, ?string $type = null): arra
}

try {
return $this->assets->getEntryPoints()->get($entry, $type);
$list = $this->assets->getEntryPoints()->get($entry, $type);

return array_map(
function ($v) use ($type) {
$v = array_map(
fn($e) => $this->getRouter()->finalizePath($e),
(array)$v
);

if (null !== $type) {
return $v[0];
}

return $v;
},
$list
);
} catch (Throwable $exception) {
throw new RuntimeError('Entry points error', previous: $exception);
}
Expand Down Expand Up @@ -232,7 +263,7 @@ public function preload(string $link, array $parameters = []): string
}

if (true === $this->isHeadersSent()) {
return $link;
return $this->getRouter()->finalizePath($link);
}

$this->sendHeader($header, false);
Expand All @@ -255,7 +286,7 @@ public function preload(string $link, array $parameters = []): string
);
}

return $link;
return $this->getRouter()->finalizePath($link);
}

/**
Expand Down
78 changes: 66 additions & 12 deletions tests/Extension/AssetRuntimeExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

use Berlioz\Core\Asset\Assets;
use Berlioz\Package\Twig\Extension\AssetRuntimeExtension;
use Berlioz\Router\Router;
use PHPUnit\Framework\TestCase;
use Twig\Error\RuntimeError;

Expand All @@ -23,6 +24,7 @@ class AssetRuntimeExtensionTest extends TestCase

protected function setUp(): void
{
unset($_SERVER['HTTP_X_FORWARDED_PREFIX']);
$this->assets = new Assets(
__DIR__ . '/data/manifest.json',
__DIR__ . '/data/entrypoints.json',
Expand All @@ -31,7 +33,7 @@ protected function setUp(): void

public function testAsset()
{
$extensionRuntime = new AssetRuntimeExtension($this->assets);
$extensionRuntime = new AssetRuntimeExtension($this->assets, new Router());

$this->assertEquals('/assets/css/website.css', $extensionRuntime->asset('website.css'));
}
Expand All @@ -41,13 +43,13 @@ public function testAsset_notFound()
$this->expectException(RuntimeError::class);
$this->expectExceptionMessage('Asset "fake.css" not found in manifest file');

$extensionRuntime = new AssetRuntimeExtension($this->assets);
$extensionRuntime = new AssetRuntimeExtension($this->assets, new Router());
$extensionRuntime->asset('fake.css');
}

public function testEntryPoints()
{
$extensionRuntime = new AssetRuntimeExtension($this->assets);
$extensionRuntime = new AssetRuntimeExtension($this->assets, new Router());

$this->assertEquals(
'<link rel="stylesheet" href="/assets/css/website.css">' . PHP_EOL .
Expand All @@ -59,7 +61,7 @@ public function testEntryPoints()

public function testEntryPoints_withMultipleEntry()
{
$extensionRuntime = new AssetRuntimeExtension($this->assets);
$extensionRuntime = new AssetRuntimeExtension($this->assets, new Router());

$this->assertEquals(
'<link rel="stylesheet" href="/assets/css/website.css">' . PHP_EOL .
Expand All @@ -71,9 +73,24 @@ public function testEntryPoints_withMultipleEntry()
);
}

public function testEntryPoints_withMultipleEntryAndRouterOptions()
{
$_SERVER['HTTP_X_FORWARDED_PREFIX'] = '/prefix';
$extensionRuntime = new AssetRuntimeExtension($this->assets, new Router(['X-Forwarded-Prefix' => true]));

$this->assertEquals(
'<link rel="stylesheet" href="/prefix/assets/css/website.css">' . PHP_EOL .
'<link rel="stylesheet" href="/prefix/assets/css/admin.css">' . PHP_EOL .
'<script src="/prefix/assets/js/website.js"></script>' . PHP_EOL .
'<script src="/prefix/assets/js/vendor.js"></script>' . PHP_EOL .
'<script src="/prefix/assets/js/admin.js"></script>' . PHP_EOL,
$extensionRuntime->entryPoints(['website', 'admin'])
);
}

public function testEntryPoints_withType()
{
$extensionRuntime = new AssetRuntimeExtension($this->assets);
$extensionRuntime = new AssetRuntimeExtension($this->assets, new Router());

$this->assertEquals(
'<script src="/assets/js/website.js"></script>' . PHP_EOL .
Expand All @@ -82,9 +99,21 @@ public function testEntryPoints_withType()
);
}

public function testEntryPoints_withTypeAndRouterOptions()
{
$_SERVER['HTTP_X_FORWARDED_PREFIX'] = '/prefix';
$extensionRuntime = new AssetRuntimeExtension($this->assets, new Router(['X-Forwarded-Prefix' => true]));

$this->assertEquals(
'<script src="/prefix/assets/js/website.js"></script>' . PHP_EOL .
'<script src="/prefix/assets/js/vendor.js"></script>' . PHP_EOL,
$extensionRuntime->entryPoints('website', 'js')
);
}

public function testEntryPoints_withOptions()
{
$extensionRuntime = new AssetRuntimeExtension($this->assets);
$extensionRuntime = new AssetRuntimeExtension($this->assets, new Router());

$this->assertEquals(
'<link async defer attr="fake" data-first="value1" data-second="value2" data-third rel="stylesheet" href="/assets/css/website.css">' . PHP_EOL .
Expand All @@ -107,14 +136,14 @@ public function testEntryPoints_withOptions()

public function testEntryPoints_notFound()
{
$extensionRuntime = new AssetRuntimeExtension($this->assets);
$extensionRuntime = new AssetRuntimeExtension($this->assets, new Router());

$this->assertEquals('', $extensionRuntime->entryPoints('fake'));
}

public function testEntryPointsList()
{
$extensionRuntime = new AssetRuntimeExtension($this->assets);
$extensionRuntime = new AssetRuntimeExtension($this->assets, new Router());

$this->assertEquals(
['css' => ['/assets/css/website.css'], 'js' => ['/assets/js/website.js', '/assets/js/vendor.js']],
Expand All @@ -124,27 +153,49 @@ public function testEntryPointsList()

public function testEntryPointsList_withMultipleEntry()
{
$extensionRuntime = new AssetRuntimeExtension($this->assets);
$extensionRuntime = new AssetRuntimeExtension($this->assets, new Router());

$this->assertEquals(
['/assets/js/website.js', '/assets/js/vendor.js', '/assets/js/admin.js'],
$extensionRuntime->entryPointsList(['website', 'admin'], 'js')
);
}

public function testEntryPointsList_withMultipleEntryAndRouterOptions()
{
$_SERVER['HTTP_X_FORWARDED_PREFIX'] = '/prefix';
$extensionRuntime = new AssetRuntimeExtension($this->assets, new Router(['X-Forwarded-Prefix' => true]));

$this->assertEquals(
['/prefix/assets/js/website.js', '/prefix/assets/js/vendor.js', '/prefix/assets/js/admin.js'],
$extensionRuntime->entryPointsList(['website', 'admin'], 'js')
);
}

public function testEntryPointsList_withType()
{
$extensionRuntime = new AssetRuntimeExtension($this->assets);
$extensionRuntime = new AssetRuntimeExtension($this->assets, new Router());

$this->assertEquals(
['/assets/js/website.js', '/assets/js/vendor.js'],
$extensionRuntime->entryPointsList('website', 'js')
);
}

public function testEntryPointsList_withTypeAndRouterOptions()
{
$_SERVER['HTTP_X_FORWARDED_PREFIX'] = '/prefix';
$extensionRuntime = new AssetRuntimeExtension($this->assets, new Router(['X-Forwarded-Prefix' => true]));

$this->assertEquals(
['/prefix/assets/js/website.js', '/prefix/assets/js/vendor.js'],
$extensionRuntime->entryPointsList('website', 'js')
);
}

public function testEntryPointsList_notFound()
{
$extensionRuntime = new AssetRuntimeExtension($this->assets);
$extensionRuntime = new AssetRuntimeExtension($this->assets, new Router());

$this->assertEquals([], $extensionRuntime->entryPointsList('fake'));
}
Expand Down Expand Up @@ -211,8 +262,11 @@ public function testPreload(string $link, array $parameters, ?string $expectedHe

$assetRuntimeMock = $this->createPartialMock(
AssetRuntimeExtension::class,
['isHeadersSent', 'sendHeader', 'setCookie']
['getRouter', 'isHeadersSent', 'sendHeader', 'setCookie']
);
$assetRuntimeMock
->method('getRouter')
->willReturnCallback(fn() => new Router());
$assetRuntimeMock
->method('isHeadersSent')
->willReturnCallback(fn() => false);
Expand Down

0 comments on commit 7741f75

Please sign in to comment.