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

nette / assets / 15132874389

20 May 2025 08:37AM UTC coverage: 94.945% (+0.3%) from 94.678%
15132874389

push

github

dg
DIExtension: parameters are passed by constructor (BC break)

6 of 6 new or added lines in 1 file covered. (100.0%)

4 existing lines in 1 file now uncovered.

432 of 455 relevant lines covered (94.95%)

0.95 hits per line

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

94.87
/src/Bridges/AssetsDI/DIExtension.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Nette\Bridges\Assets;
6

7
use Nette;
8
use Nette\Assets\FilesystemMapper;
9
use Nette\Assets\Registry;
10
use Nette\Assets\ViteMapper;
11
use Nette\Bridges\AssetsLatte\LatteExtension;
12
use Nette\DI\Definitions\Statement;
13
use Nette\Schema\Expect;
14

15

16
/**
17
 * Dependency injection extension that integrates asset management into Nette application.
18
 * Provides configuration of asset mappers and their mapping to URL paths.
19
 */
20
final class DIExtension extends Nette\DI\CompilerExtension
21
{
22
        private const VitePort = 5173;
23

24
        private int $needVariable;
25

26

27
        public function __construct(
1✔
28
                private string|Nette\Schema\DynamicParameter|null $baseUrl = null,
29
                private ?string $basePath = null,
30
                private bool $debugMode = false,
31
        ) {
32
        }
1✔
33

34

35
        public function getConfigSchema(): Nette\Schema\Schema
36
        {
37
                return Expect::structure([
1✔
38
                        'basePath' => Expect::string()->dynamic(),
1✔
39
                        'baseUrl' => Expect::string()->dynamic(),
1✔
40
                        'versioning' => Expect::bool(),
1✔
41
                        'mapping' => Expect::arrayOf(
1✔
42
                                Expect::anyOf(
1✔
43
                                        Expect::string(),
1✔
44
                                        Expect::structure([
1✔
45
                                                'path' => Expect::string()->dynamic(),
1✔
46
                                                'url' => Expect::string()->dynamic(),
1✔
47
                                                'extension' => Expect::anyOf(Expect::string(), Expect::arrayOf('string')),
1✔
48
                                                'versioning' => Expect::bool(),
1✔
49
                                        ]),
50
                                        Expect::structure([
1✔
51
                                                'type' => Expect::string('vite'),
1✔
52
                                                'path' => Expect::string('')->dynamic(),
1✔
53
                                                'url' => Expect::string()->dynamic(),
1✔
54
                                                'extension' => Expect::anyOf(Expect::string(), Expect::arrayOf('string')),
1✔
55
                                                'versioning' => Expect::bool(),
1✔
56
                                                'manifest' => Expect::string()->dynamic(),
1✔
57
                                                'devServer' => Expect::anyOf(Expect::string(), Expect::bool())->default(false),
1✔
58
                                        ]),
59
                                        Expect::type(Statement::class),
1✔
60
                                ),
61
                        )->default(['default' => 'assets']),
1✔
62
                ]);
63
        }
64

65

66
        public function loadConfiguration()
67
        {
68
                $builder = $this->getContainerBuilder();
1✔
69
                $registry = $builder->addDefinition($this->prefix('registry'))
1✔
70
                        ->setFactory(Registry::class);
1✔
71

72
                $this->needVariable = 0;
1✔
73
                $this->basePath = $this->config->basePath ?? $this->basePath ?? null;
1✔
74

75
                foreach ($this->config->mapping as $scope => $item) {
1✔
76
                        if (is_string($item)) {
1✔
77
                                $mapper = str_contains($item, '\\')
1✔
78
                                        ? new Statement($item)
1✔
79
                                        : $this->createFileMapper((object) ['path' => $item]);
1✔
80

81
                        } elseif (!$item instanceof \stdClass) {
1✔
UNCOV
82
                                $mapper = $item;
×
83

84
                        } elseif (($item->type ?? null) === 'vite') {
1✔
85
                                $mapper = $this->createViteMapper($item);
1✔
86

87
                        } else {
88
                                $mapper = $this->createFileMapper($item);
1✔
89
                        }
90

91
                        if ($this->needVariable === 1) {
1✔
92
                                $baseUrl = $this->config->baseUrl ?? $this->baseUrl ?? throw new \LogicException("Assets: 'baseUrl' is not defined");
1✔
93
                                $registry->addSetup('$baseUrl = new Nette\Http\UrlImmutable(?)', [new Statement("rtrim(?, '/') . '/'", [$baseUrl])]);
1✔
94
                        }
95

96
                        $registry->addSetup('addMapper', [$scope, $mapper]);
1✔
97
                }
98
        }
1✔
99

100

101
        private function createFileMapper(\stdClass $config): Statement
1✔
102
        {
103
                $this->needVariable++;
1✔
104
                return new Statement(FilesystemMapper::class, [
1✔
105
                        'baseUrl' => $this->resolveUrl($config),
1✔
106
                        'basePath' => $this->resolvePath($config),
1✔
107
                        'extensions' => (array) ($config->extension ?? null),
1✔
108
                        'versioning' => $config->versioning ?? $this->config->versioning ?? true,
1✔
109
                ]);
110
        }
111

112

113
        private function createViteMapper(\stdClass $config): Statement
1✔
114
        {
115
                return new Statement(ViteMapper::class, [
1✔
116
                        'baseUrl' => $this->resolveUrl($config),
1✔
117
                        'basePath' => new Statement('$path = ?', [$this->resolvePath($config)]),
1✔
118
                        'manifestPath' => $config->manifest ? new Statement('Nette\Utils\FileSystem::resolvePath($path, ?)', [$config->manifest]) : null,
1✔
119
                        'devServer' => $this->resolveDevServer($config),
1✔
120
                        'publicMapper' => $this->createFileMapper($config),
1✔
121
                ]);
122
        }
123

124

125
        public function beforeCompile()
126
        {
127
                $builder = $this->getContainerBuilder();
1✔
128
                if ($name = $builder->getByType(Nette\Bridges\ApplicationLatte\LatteFactory::class)) {
1✔
UNCOV
129
                        $builder->getDefinition($name)
×
UNCOV
130
                                ->getResultDefinition()
×
UNCOV
131
                                ->addSetup('addExtension', [new Statement(LatteExtension::class)]);
×
132
                }
133
        }
1✔
134

135

136
        private function resolvePath(\stdClass $config): string|Statement
1✔
137
        {
138
                $path = $this->basePath && $config->path
1✔
139
                        ? new Statement('Nette\Utils\FileSystem::resolvePath(?, ?)', [$this->basePath, $config->path])
1✔
140
                        : $config->path ?? $this->basePath ?? throw new \LogicException("Assets: 'basePath' is not defined");
1✔
141
                return new Statement("rtrim(?, '\\/')", [$path]);
1✔
142
        }
143

144

145
        private function resolveUrl(\stdClass $config): Statement
1✔
146
        {
147
                $url = new Statement('$baseUrl->resolve(?)->getAbsoluteUrl()', [$config->url ?? $config->path ?? '']);
1✔
148
                return new Statement("rtrim(?, '/')", [$url]);
1✔
149
        }
150

151

152
        private function resolveDevServer(\stdClass $config): Statement|string|null
1✔
153
        {
154
                if (!$this->debugMode || !$config->devServer) {
1✔
155
                        return null;
1✔
156
                }
157
                return is_string($config->devServer)
1✔
158
                        ? new Statement("rtrim(?, '/')", [$config->devServer])
1✔
159
                        : new Statement('(new Nette\Http\UrlImmutable(?))->withPort(?)->getHostUrl()', [$this->config->baseUrl ?? $this->baseUrl, self::VitePort]);
1✔
160
        }
161
}
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