Skip to content

Commit

Permalink
Compiled asset are yielded with list of attributes from Bundle; Closes
Browse files Browse the repository at this point in the history
…#23; Closes #24
  • Loading branch information
juniwalk committed Apr 24, 2024
1 parent 30af4e3 commit 8119137
Show file tree
Hide file tree
Showing 10 changed files with 96 additions and 70 deletions.
11 changes: 11 additions & 0 deletions src/Asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,15 @@ public function getContent(): string;

public function setModule(bool $module): void;
public function isModule(): bool;

/**
* @return array<string, mixed>
*/
public function getAttributes(): array;
public function getAttribute(string $name): mixed;

/**
* @param array<string, mixed> $attributes
*/
public function setAttributes(array $attributes): void;
}
3 changes: 3 additions & 0 deletions src/Assets/FileAsset.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@

use JuniWalk\Tessa\Asset;
use JuniWalk\Tessa\Exceptions\AssetContentException;
use JuniWalk\Tessa\Traits\Attributes;

class FileAsset implements Asset
{
use Attributes;

protected string $file;
protected string $ext;
protected bool $isModule = false;
Expand Down
21 changes: 7 additions & 14 deletions src/BundleManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
namespace JuniWalk\Tessa;

use JuniWalk\Tessa\Bundle;
use JuniWalk\Tessa\Bundles\AssetBundle;
use JuniWalk\Tessa\Enums\Type;
use JuniWalk\Tessa\Exceptions\BundleNotFoundException;
use JuniWalk\Tessa\Exceptions\BundleRecursionException;
Expand Down Expand Up @@ -55,32 +54,26 @@ public function getBundles(): array
/**
* @throws BundleNotFoundException
* @throws BundleRecursionException
* @return Asset[]
*/
public function compile(string $name, Type $type): Bundle
public function compile(string $name, Type $type): iterable
{
$bundle = $this->getBundle($name);
$name = $name.$type->value;
$assets = [];

$this->detectRecursion($bundle);

if ($extend = $bundle->getExtendBundle()) {
$assets = $this->compile($extend, $type)->getAssets();
yield from $this->compile($extend, $type);
}

foreach ($bundle->getAssets($type) as $asset) {
$asset->setAttributes($bundle->getAttributes());

if (!$asset->isModule() && !$bundle->isDirectLink()) {
$asset = $this->storage->store($asset, $name);
$asset = $this->storage->store($asset, $name.$type->value);
}

$assets[] = $asset;
yield $asset;
}

$output = new AssetBundle($name, ...$assets);
$output->setAttributes($bundle->getAttributes());
$output->setDirectLink($bundle->isDirectLink());

return $output;
}


Expand Down
36 changes: 3 additions & 33 deletions src/Bundles/AssetBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@
use JuniWalk\Tessa\Bundle;
use JuniWalk\Tessa\Enums\Type;
use JuniWalk\Tessa\Exceptions\AssetTypeException;
use JuniWalk\Tessa\Traits\Attributes;
use JuniWalk\Utils\Strings;

class AssetBundle implements Bundle
{
use Attributes;

protected ?string $extend = null;
protected bool $directLink = false;

/** @var array<string, mixed> */
protected array $attributes = [];

/** @var Asset[] */
protected array $assets;

Expand Down Expand Up @@ -65,36 +65,6 @@ public function getExtendBundle(): ?string
}


public function setAttribute(string $name, mixed $value): void
{
$this->attributes[$name] = $value;
}


public function getAttribute(string $name): mixed
{
return $this->attributes[$name] ?? null;
}


/**
* @param array<string, mixed> $attributes
*/
public function setAttributes(array $attributes): void
{
$this->attributes = $attributes;
}


/**
* @return array<string, mixed>
*/
public function getAttributes(): array
{
return $this->attributes;
}


public function addAsset(Asset $asset): void
{
$this->assets[] = $asset;
Expand Down
1 change: 1 addition & 0 deletions src/Storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ public function store(Asset $asset, ?string $prefix = null): Asset

$file = $this->outputDir.'/'.$prefix.$asset->getName();
$temp = new FileAsset($file, $asset->getExt());
$temp->setAttributes($asset->getAttributes());
$temp->setModule($asset->isModule());

if (!$this->isOutOfDate($asset, $file)) {
Expand Down
16 changes: 8 additions & 8 deletions src/TessaRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public function render(Type|string $type): void
*/
private function compile(string $bundle, Type $type): array
{
$bundle = $this->bundleManager->compile($bundle, $type);
$assets = $this->bundleManager->compile($bundle, $type);
$create = match ($type) {
Type::StyleSheet => $this->createStyleSheet(...),
Type::JavaScript => $this->createJavaScript(...),
Expand All @@ -116,15 +116,15 @@ private function compile(string $bundle, Type $type): array

$output = [];

foreach ($bundle->getAssets() as $asset) {
$output[] = $create($asset, $bundle);
foreach ($assets as $asset) {
$output[] = $create($asset);
}

return array_filter($output);
}


private function createStyleSheet(Asset $asset, Bundle $bundle): ?Html
private function createStyleSheet(Asset $asset): ?Html
{
$path = $this->createFilePath($asset);

Expand All @@ -140,20 +140,20 @@ private function createStyleSheet(Asset $asset, Bundle $bundle): ?Html
}


private function createJavaScript(Asset $asset, Bundle $bundle): ?Html
private function createJavaScript(Asset $asset): ?Html
{
$path = $this->createFilePath($asset);
$type = $bundle->getAttribute('type');
$type = $asset->getAttribute('type');

if ($this->history[$path] ?? false) {
return null;
}

$html = Html::el('script type="text/javascript"')
->addAttributes($bundle->getAttributes())
->addAttributes($asset->getAttributes())
->setSrc($path);

if ($bundle->getAttribute('cookie-consent')) {
if ($asset->getAttribute('cookie-consent')) {
$html->setAttribute('type', 'text/plain');
}

Expand Down
43 changes: 43 additions & 0 deletions src/Traits/Attributes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php declare(strict_types=1);

/**
* @copyright (c) Martin Procházka
* @license MIT License
*/

namespace JuniWalk\Tessa\Traits;

trait Attributes
{
/** @var array<string, mixed> */
protected array $attributes = [];

/**
* @param array<string, mixed> $attributes
*/
public function setAttributes(array $attributes): void
{
$this->attributes = $attributes;
}


public function setAttribute(string $name, mixed $value): void
{
$this->attributes[$name] = $value;
}


/**
* @return array<string, mixed>
*/
public function getAttributes(): array
{
return $this->attributes;
}


public function getAttribute(string $name): mixed
{
return $this->attributes[$name] ?? null;
}
}
26 changes: 13 additions & 13 deletions tests/Bundles/CompileBundleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ public function tearDown() {

public function testBundleStandardCompilation(): void
{
$bundle = $this->bundleManager->compile('default', Type::JavaScript);
$assets = $this->bundleManager->compile('default', Type::JavaScript);
$patterns = [
'defaultjs-script.js' => '#/static/defaultjs-script.js$#i',
'module.mjs' => '#/assets/module.mjs$#i',
];

Assert::same(null, $bundle->getAttribute('type'));
// Assert::same(null, $bundle->getAttribute('type'));

foreach ($bundle->getAssets() as $asset) {
foreach ($assets as $asset) {
$file = $asset->getFile();
$name = $asset->getName();

Expand All @@ -63,15 +63,15 @@ public function testBundleStandardCompilation(): void

public function testBundleModuleCompilation(): void
{
$bundle = $this->bundleManager->compile('module', Type::JavaScript);
$assets = $this->bundleManager->compile('module', Type::JavaScript);
$patterns = [
'script.js' => '#/assets/script.js$#i',
'module.mjs' => '#/assets/module.mjs$#i',
];

Assert::same('module', $bundle->getAttribute('type'));
// Assert::same('module', $bundle->getAttribute('type'));

foreach ($bundle->getAssets() as $asset) {
foreach ($assets as $asset) {
$file = $asset->getFile();
$name = $asset->getName();

Expand All @@ -84,17 +84,17 @@ public function testBundleModuleCompilation(): void

public function testBundleExtendedCompilation(): void
{
$bundle = $this->bundleManager->compile('extended', Type::JavaScript);
$assets = $this->bundleManager->compile('extended', Type::JavaScript);
$patterns = [
'defaultjs-script.js' => '#/static/defaultjs-script.js$#i',
'module.mjs' => '#/assets/module.mjs$#i',
'fullcalendar.mjs' => '#/assets/fullcalendar.mjs$#i',
'extendedjs-form.js' => '#/static/extendedjs-form.js$#i',
];

Assert::same(null, $bundle->getAttribute('type'));
// Assert::same(null, $bundle->getAttribute('type'));

foreach ($bundle->getAssets() as $asset) {
foreach ($assets as $asset) {
$file = $asset->getFile();
$name = $asset->getName();

Expand All @@ -112,16 +112,16 @@ public function testBundleDirectLinkingCompilation(): void
{
$this->bundleManager->getBundle('default')->setDirectLink(true);

$bundle = $this->bundleManager->compile('default', Type::JavaScript);
$assets = $this->bundleManager->compile('default', Type::JavaScript);
$patterns = [
'script.js' => '#/assets/script.js$#i',
'module.mjs' => '#/assets/module.mjs$#i',
];

Assert::same(null, $bundle->getAttribute('type'));
Assert::true($bundle->isDirectLink());
// Assert::same(null, $bundle->getAttribute('type'));
// Assert::true($bundle->isDirectLink());

foreach ($bundle->getAssets() as $asset) {
foreach ($assets as $asset) {
$file = $asset->getFile();
$name = $asset->getName();

Expand Down
4 changes: 4 additions & 0 deletions tests/Rendering/TessaRendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ public function testRenderClassicAssets(): void

Assert::contains('/static/defaultjs-script.js', $output);
Assert::contains('/assets/module.mjs', $output);

Assert::notContains('defer', $output);
}


Expand All @@ -88,6 +90,8 @@ public function testRenderClearPreviousAssets(): void
Assert::notContains('/static/defaultjs-script.js', $output);
Assert::notContains('/assets/module.mjs', $output);
Assert::contains('/assets/fullcalendar.mjs', $output);

Assert::contains('defer', $output);
}


Expand Down
5 changes: 3 additions & 2 deletions tests/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,17 @@ tessa:

calendar:
isModule: true
defer: true
assets:
- %wwwDir%/assets/fullcalendar.mjs

standard:
extend: 'default'
extend: default
isModule: true
assets:
- %wwwDir%/assets/fullcalendar.mjs

extended:
extend: 'standard'
extend: standard
assets:
- %wwwDir%/assets/form.js

0 comments on commit 8119137

Please sign in to comment.