8000 [AssetMapper] Changing to an enum · symfony/symfony@7b7863e · GitHub
[go: up one dir, main page]

Skip to content

Commit 7b7863e

Browse files
committed
[AssetMapper] Changing to an enum
1 parent f2996e7 commit 7b7863e

File tree

3 files changed

+27
-22
lines changed

3 files changed

+27
-22
lines changed

src/Symfony/Component/AssetMapper/ImportMap/ImportMapEntry.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@
1818
*/
1919
final class ImportMapEntry
2020
{
21-
public const TYPE_JS = 'js';
22-
public const TYPE_CSS = 'css';
23-
2421
public function __construct(
2522
/**
2623
* The logical path to this asset if local or downloaded.
@@ -30,12 +27,7 @@ public function __construct(
3027
public readonly ?string $url = null,
3128
public readonly bool $isDownloaded = false,
3229
public readonly bool $preload = false,
33-
public readonly string $type = self::TYPE_JS,
30+
public readonly ImportMapType $type = ImportMapType::JS,
3431
) {
3532
}
36-
37-
public static function getValidTypes(): array
38-
{
39-
return [self::TYPE_JS, self::TYPE_CSS];
40-
}
4133
}

src/Symfony/Component/AssetMapper/ImportMap/ImportMapManager.php

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ private function requirePackages(array $packagesToRequire, array &$importMapEntr
293293
foreach ($resolvedPackages as $resolvedPackage) {
294294
$importName = $resolvedPackage->requireOptions->importName ?: $resolvedPackage->requireOptions->packageName;
295295
$path = null;
296-
$type = str_ends_with($resolvedPackage->url, '.css') ? ImportMapEntry::TYPE_CSS : ImportMapEntry::TYPE_JS;
296+
$type = str_ends_with($resolvedPackage->url, '.css') ? ImportMapType::CSS : ImportMapType::JS;
297297
if ($resolvedPackage->requireOptions->download) {
298298
if (null === $resolvedPackage->content) {
299299
throw new \LogicException(sprintf('The contents of package "%s" were not downloaded.', $resolvedPackage->requireOptions->packageName));
@@ -349,18 +349,13 @@ private function loadImportMapEntries(): array
349349
throw new \InvalidArgumentException(sprintf('The following keys are not valid for the importmap entry "%s": "%s". Valid keys are: "%s".', $importName, implode('", "', $invalidKeys), implode('", "', $validKeys)));
350350
}
351351

352-
$type = $data['type'] ?? ImportMapEntry::TYPE_JS;
353-
if (!\in_array($type, ImportMapEntry::getValidTypes(), true)) {
354-
throw new \InvalidArgumentException(sprintf('The "type" for import "%s" must be one of "%s", "%s" given.', $importName, implode(', ', ImportMapEntry::getValidTypes()), $type));
355-
}
356-
357352
$entries[$importName] = new ImportMapEntry(
358353
$importName,
359354
path: $data['path'] ?? $data['downloaded_to'] ?? null,
360355
url: $data['url'] ?? null,
361356
isDownloaded: isset($data['downloaded_to']),
362357
preload: $data['preload'] ?? false,
363-
type: $type,
358+
type: isset($data['type']) ? ImportMapType::tryFrom($data['type']) : ImportMapType::JS
364359
);
365360
}
366361

@@ -397,8 +392,8 @@ private function writeImportMapConfig(array $entries): void
397392
if ($entry->preload) {
398393
$config['preload'] = $entry->preload;
399394
}
400-
if (ImportMapEntry::TYPE_JS !== $entry->type) {
401-
$config['type'] = $entry->type;
395+
if (ImportMapType::JS !== $entry->type) {
396+
$config['type'] = $entry->type->value;
402397
}
403398
$importMapConfig[$entry->importName] = $config;
404399
}
@@ -462,7 +457,7 @@ private function convertEntriesToImports(array $entries, bool $isTopLevel): arra
462457
throw new \InvalidArgumentException(sprintf('The package "%s" mentioned in "%s" must have a "path" or "url" key.', $entryOptions->importName, basename($this->importMapConfigPath)));
463458
}
464459

465-
if (ImportMapEntry::TYPE_CSS === $entryOptions->type) {
460+
if (ImportMapType::CSS === $entryOptions->type) {
466461
if ($entryOptions->preload) {
467462
// importmap is a noop because this will be rendered as a link tag
468463
$this->linkTags[] = $path;
@@ -493,7 +488,7 @@ private function convertEntriesToImports(array $entries, bool $isTopLevel): arra
493488
$dependency->asset->logicalPath,
494489
// if parent is preload & dependency is not lazy, then preload
495490
preload: $entryOptions->preload && !$dependency->isLazy,
496-
type: 'css' === $dependency->asset->publicExtension ? ImportMapEntry::TYPE_CSS : ImportMapEntry::TYPE_JS,
491+
type: 'css' === $dependency->asset->publicExtension ? ImportMapType::CSS : ImportMapType::JS,
497492
);
498493
}, $dependencies);
499494

@@ -504,12 +499,12 @@ private function convertEntriesToImports(array $entries, bool $isTopLevel): arra
504499
return $imports;
505500
}
506501

507-
private function downloadPackage(string $packageName, string $packageContents, string $importMapType): string
502+
private function downloadPackage(string $packageName, string $packageContents, ImportMapType $importMapType): string
508503
{
509504
$vendorPath = $this->vendorDir.'/'.$packageName;
510505
// add an extension of there is none
511506
if (!str_contains($packageName, '.')) {
512-
$vendorPath .= '.'.$importMapType;
507+
$vendorPath .= '.'.$importMapType->value;
513508
}
514509< 67F4 /code>

515510
@mkdir(\dirname($vendorPath), 0777, true);
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\AssetMapper\ImportMap;
13+
14+
enum ImportMapType : string
15+
{
16+
case JS = 'js';
17+
case CSS = 'css';
18+
}

0 commit comments

Comments
 (0)
0