Skip to content

Commit

Permalink
Moved directLink configuration to per-bundle
Browse files Browse the repository at this point in the history
  • Loading branch information
juniwalk committed Apr 24, 2024
1 parent 1444f37 commit a2e4a9e
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 23 deletions.
2 changes: 2 additions & 0 deletions src/Bundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
interface Bundle
{
public function getName(): string;
public function setDirectLink(bool $directLink): void;
public function isDirectLink(): bool;
public function setExtendBundle(?string $extend): void;
public function getExtendBundle(): ?string;

Expand Down
22 changes: 8 additions & 14 deletions src/BundleManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,11 @@ final class BundleManager
private array $bundles = [];

public function __construct(
private ?Storage $storage = null,
private readonly Storage $storage,
) {
}


public function setStorage(?Storage $storage): void
{
$this->storage = $storage;
}


public function addBundle(Bundle $bundle): void
{
$this->bundles[$bundle->getName()] = $bundle;
Expand Down Expand Up @@ -62,11 +56,10 @@ public function getBundles(): array
* @throws BundleNotFoundException
* @throws BundleRecursionException
*/
public function compile(string $bundleName, Type $type): Bundle
public function compile(string $name, Type $type): Bundle
{
$bundle = $this->getBundle($bundleName);
$bundleType = $bundle->getAttribute('type');
$bundleName = $bundleName.$type->value;
$bundle = $this->getBundle($name);
$name = $name.$type->value;
$assets = [];

$this->detectRecursion($bundle);
Expand All @@ -76,15 +69,16 @@ public function compile(string $bundleName, Type $type): Bundle
}

foreach ($bundle->getAssets($type) as $asset) {
if (!$asset->isModule() && $bundleType <> 'module') {
$asset = $this->storage?->store($asset, $bundleName) ?? $asset;
if (!$asset->isModule() && !$bundle->isDirectLink()) {
$asset = $this->storage->store($asset, $name);
}

$assets[] = $asset;
}

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

return $output;
}
Expand Down
13 changes: 13 additions & 0 deletions src/Bundles/AssetBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
class AssetBundle implements Bundle
{
protected ?string $extend = null;
protected bool $directLink = false;

/** @var array<string, mixed> */
protected array $attributes = [];
Expand All @@ -40,6 +41,18 @@ public function getName(): string
}


public function setDirectLink(bool $directLink): void
{
$this->directLink = $directLink;
}


public function isDirectLink(): bool
{
return $this->directLink || $this->getAttribute('type') == 'module';
}


public function setExtendBundle(?string $extend): void
{
$this->extend = $extend;
Expand Down
1 change: 1 addition & 0 deletions src/DI/Bundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class Bundle
{
public ?string $cookieConsent = null;
public ?string $extend = null;
public bool $directLink = false;
public bool $isModule = false;
public bool $defer = false;
public bool $async = false;
Expand Down
2 changes: 1 addition & 1 deletion src/DI/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Config
{
public string $outputDir;
public bool $checkLastModified = true;
public bool $directLinking = false;
public bool $directLink = false;
public bool $debugMode = false;

/** @var string[] */
Expand Down
5 changes: 1 addition & 4 deletions src/DI/TessaExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,10 @@ public function loadConfiguration(): void
$manager = $builder->addDefinition($this->prefix('manager'))
->setFactory(BundleManager::class);

if ($config->directLinking) {
$manager->addSetup('setStorage', [null]);
}

foreach ($config->bundles as $bundleName => $bundle) {
$stmt = $builder->addDefinition($this->prefix('bundle.'.$bundleName))
->setFactory(AssetBundle::class, [$bundleName])
->addSetup('setDirectLink', [$bundle->directLink || $config->directLink])
->addSetup('setExtendBundle', [$bundle->extend]);

if ($bundle->isModule) {
Expand Down
6 changes: 2 additions & 4 deletions tests/Bundles/CompileBundleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ public function testBundleStandardCompilation(): void
$patterns = [
'defaultjs-script.js' => '#/static/defaultjs-script.js$#i',
'module.mjs' => '#/assets/module.mjs$#i',
// 'api.js' => '#/recaptcha/api.js$#i',
];

Assert::same(null, $bundle->getAttribute('type'));
Expand Down Expand Up @@ -91,7 +90,6 @@ public function testBundleExtendedCompilation(): void
'module.mjs' => '#/assets/module.mjs$#i',
'fullcalendar.mjs' => '#/assets/fullcalendar.mjs$#i',
'extendedjs-form.js' => '#/static/extendedjs-form.js$#i',
// 'api.js' => '#/recaptcha/api.js$#i',
];

Assert::same(null, $bundle->getAttribute('type'));
Expand All @@ -112,16 +110,16 @@ public function testBundleExtendedCompilation(): void

public function testBundleDirectLinkingCompilation(): void
{
$this->bundleManager->setStorage(null);
$this->bundleManager->getBundle('default')->setDirectLink(true);

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

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

foreach ($bundle->getAssets() as $asset) {
$file = $asset->getFile();
Expand Down

0 comments on commit a2e4a9e

Please sign in to comment.