• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

nette / assets / 15132885063

20 May 2025 08:10AM UTC coverage: 94.667% (+0.1%) from 94.521%
15132885063

push

github

dg
nette/assets

142 of 150 new or added lines in 5 files covered. (94.67%)

142 of 150 relevant lines covered (94.67%)

0.95 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.0
/src/Assets/Registry.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Nette\Assets;
6

7

8
/**
9
 * Manages a collection of named asset Mappers and provides a central point
10
 * for retrieving Assets using qualified references (mapper:reference).
11
 * Includes a simple cache for resolved assets.
12
 */
13
class Registry
14
{
15
        public const DefaultScope = '';
16
        private const MaxCacheSize = 100;
17

18
        /** @var array<string, Mapper> */
19
        private array $mappers = [];
20

21
        /** @var array<string, ?Asset> */
22
        private array $cache = [];
23

24

25
        /**
26
         * Registers a new asset mapper under a specific identifier.
27
         * @throws \InvalidArgumentException If the identifier is already in use.
28
         */
29
        public function addMapper(string $id, Mapper $mapper): void
1✔
30
        {
31
                if (isset($this->mappers[$id])) {
1✔
32
                        throw new \InvalidArgumentException("Asset mapper '$id' is already registered");
1✔
33
                }
34
                $this->mappers[$id] = $mapper;
1✔
35
        }
1✔
36

37

38
        /**
39
         * Retrieves a registered asset mapper by its identifier.
40
         * @throws \InvalidArgumentException If the requested mapper identifier is unknown.
41
         */
42
        public function getMapper(string $id = self::DefaultScope): Mapper
1✔
43
        {
44
                return $this->mappers[$id] ?? throw new \InvalidArgumentException("Unknown asset mapper '$id'.");
1✔
45
        }
46

47

48
        /**
49
         * Retrieves an Asset instance using a qualified reference. Accepts either 'mapper:reference' or ['mapper', 'reference'].
50
         * Options passed directly to the underlying Mapper::getAsset() method.
51
         */
52
        public function getAsset(string|array $qualifiedRef, array $options = []): ?Asset
1✔
53
        {
54
                [$mapper, $reference] = is_string($qualifiedRef)
1✔
55
                        ? Helpers::parseReference($qualifiedRef)
1✔
56
                        : $qualifiedRef;
1✔
57

58
                $mapperDef = $mapper ?? self::DefaultScope;
1✔
59
                $reference = (string) $reference;
1✔
60
                $cacheKey = $this->generateCacheKey($mapperDef, $reference, $options);
1✔
61
                if ($cacheKey !== null && array_key_exists($cacheKey, $this->cache)) {
1✔
62
                        return $this->cache[$cacheKey];
1✔
63
                }
64

65
                $asset = $this->getMapper($mapperDef)->getAsset($reference, $options);
1✔
66

67
                if (count($this->cache) >= self::MaxCacheSize) {
1✔
68
                        array_shift($this->cache); // remove the oldest entry
1✔
69
                }
70

71
                return $this->cache[$cacheKey] = $asset;
1✔
72
        }
73

74

75
        private function generateCacheKey(string $mapper, string $reference, array $options): ?string
1✔
76
        {
77
                foreach ($options as $item) {
1✔
78
                        if ($item !== null && !is_scalar($item)) {
1✔
NEW
79
                                return null;
×
80
                        }
81
                }
82
                return $mapper . ':' . $reference . ($options ? ':' . hash('xxh128', serialize($options)) : '');
1✔
83
        }
84
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc