|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +namespace Nette\PHPStan\Assets; |
| 4 | + |
| 5 | +use PHPStan\Reflection\ReflectionProvider; |
| 6 | +use PHPStan\Type\ObjectType; |
| 7 | +use function pathinfo, strpos, strtolower, substr; |
| 8 | + |
| 9 | + |
| 10 | +/** |
| 11 | + * Resolves mapper IDs to mapper class types and asset references to asset class types. |
| 12 | + * Mapper IDs are resolved from a flat map configured in NEON. |
| 13 | + * Asset types are resolved from file extensions using hardcoded mapping mirroring Helpers::createAssetFromUrl(). |
| 14 | + */ |
| 15 | +class MapperTypeResolver |
| 16 | +{ |
| 17 | + private const ExtensionToAssetClass = [ |
| 18 | + 'avif' => 'Nette\Assets\ImageAsset', |
| 19 | + 'gif' => 'Nette\Assets\ImageAsset', |
| 20 | + 'ico' => 'Nette\Assets\ImageAsset', |
| 21 | + 'jpeg' => 'Nette\Assets\ImageAsset', |
| 22 | + 'jpg' => 'Nette\Assets\ImageAsset', |
| 23 | + 'png' => 'Nette\Assets\ImageAsset', |
| 24 | + 'svg' => 'Nette\Assets\ImageAsset', |
| 25 | + 'webp' => 'Nette\Assets\ImageAsset', |
| 26 | + 'js' => 'Nette\Assets\ScriptAsset', |
| 27 | + 'mjs' => 'Nette\Assets\ScriptAsset', |
| 28 | + 'css' => 'Nette\Assets\StyleAsset', |
| 29 | + 'aac' => 'Nette\Assets\AudioAsset', |
| 30 | + 'flac' => 'Nette\Assets\AudioAsset', |
| 31 | + 'm4a' => 'Nette\Assets\AudioAsset', |
| 32 | + 'mp3' => 'Nette\Assets\AudioAsset', |
| 33 | + 'ogg' => 'Nette\Assets\AudioAsset', |
| 34 | + 'wav' => 'Nette\Assets\AudioAsset', |
| 35 | + 'avi' => 'Nette\Assets\VideoAsset', |
| 36 | + 'mkv' => 'Nette\Assets\VideoAsset', |
| 37 | + 'mov' => 'Nette\Assets\VideoAsset', |
| 38 | + 'mp4' => 'Nette\Assets\VideoAsset', |
| 39 | + 'ogv' => 'Nette\Assets\VideoAsset', |
| 40 | + 'webm' => 'Nette\Assets\VideoAsset', |
| 41 | + 'woff' => 'Nette\Assets\FontAsset', |
| 42 | + 'woff2' => 'Nette\Assets\FontAsset', |
| 43 | + 'ttf' => 'Nette\Assets\FontAsset', |
| 44 | + ]; |
| 45 | + |
| 46 | + private const KnownMappers = [ |
| 47 | + 'Nette\Assets\FilesystemMapper', |
| 48 | + 'Nette\Assets\ViteMapper', |
| 49 | + ]; |
| 50 | + |
| 51 | + |
| 52 | + /** |
| 53 | + * @param array<string, string> $mapping mapper ID → type keyword ('file', 'vite') or FQCN |
| 54 | + */ |
| 55 | + public function __construct( |
| 56 | + private ReflectionProvider $reflectionProvider, |
| 57 | + private array $mapping = [], |
| 58 | + ) { |
| 59 | + } |
| 60 | + |
| 61 | + |
| 62 | + /** |
| 63 | + * Resolves a mapper ID to an ObjectType for the mapper class. |
| 64 | + */ |
| 65 | + public function resolveMapper(string $mapperId): ?ObjectType |
| 66 | + { |
| 67 | + if (!isset($this->mapping[$mapperId])) { |
| 68 | + return null; |
| 69 | + } |
| 70 | + |
| 71 | + $className = $this->inferMapperClass($this->mapping[$mapperId]); |
| 72 | + return $this->reflectionProvider->hasClass($className) |
| 73 | + ? new ObjectType($className) |
| 74 | + : null; |
| 75 | + } |
| 76 | + |
| 77 | + |
| 78 | + private function inferMapperClass(string $value): string |
| 79 | + { |
| 80 | + return match ($value) { |
| 81 | + 'file' => 'Nette\Assets\FilesystemMapper', |
| 82 | + 'vite' => 'Nette\Assets\ViteMapper', |
| 83 | + default => $value, |
| 84 | + }; |
| 85 | + } |
| 86 | + |
| 87 | + |
| 88 | + /** |
| 89 | + * Checks whether the mapper for a given ID is a known mapper type |
| 90 | + * (FilesystemMapper or ViteMapper) whose asset types can be narrowed. |
| 91 | + */ |
| 92 | + public function isKnownMapper(string $mapperId): bool |
| 93 | + { |
| 94 | + $mapperType = $this->resolveMapper($mapperId); |
| 95 | + if ($mapperType === null) { |
| 96 | + return false; |
| 97 | + } |
| 98 | + |
| 99 | + foreach (self::KnownMappers as $knownClass) { |
| 100 | + if ((new ObjectType($knownClass))->isSuperTypeOf($mapperType)->yes()) { |
| 101 | + return true; |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + return false; |
| 106 | + } |
| 107 | + |
| 108 | + |
| 109 | + /** |
| 110 | + * Resolves an asset reference to an ObjectType based on its file extension. |
| 111 | + */ |
| 112 | + public function resolveAssetType(string $reference): ?ObjectType |
| 113 | + { |
| 114 | + $extension = strtolower(pathinfo($reference, PATHINFO_EXTENSION)); |
| 115 | + return isset(self::ExtensionToAssetClass[$extension]) |
| 116 | + ? new ObjectType(self::ExtensionToAssetClass[$extension]) |
| 117 | + : null; |
| 118 | + } |
| 119 | + |
| 120 | + |
| 121 | + /** |
| 122 | + * Splits a qualified reference 'mapper:reference' into [mapperId, assetPath]. |
| 123 | + * @return array{string, string} |
| 124 | + */ |
| 125 | + public function parseReference(string $ref): array |
| 126 | + { |
| 127 | + $pos = strpos($ref, ':'); |
| 128 | + return $pos !== false |
| 129 | + ? [substr($ref, 0, $pos), substr($ref, $pos + 1)] |
| 130 | + : ['default', $ref]; |
| 131 | + } |
| 132 | +} |
0 commit comments