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

dakujem / peat / 16454919909

22 Jul 2025 08:33PM UTC coverage: 80.42%. Remained the same
16454919909

push

github

dakujem
name tweak

0 of 1 new or added line in 1 file covered. (0.0%)

115 of 143 relevant lines covered (80.42%)

5.59 hits per line

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

61.22
/src/ViteBridge.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Dakujem\Peat;
6

7
use LogicException;
8
use RuntimeException;
9

10
/**
11
 * A factory service providing Vite entry locators.
12
 * Also provides an ability to populate a PHP cache file for performance improvement.
13
 *
14
 * @author Andrej Rypak <xrypak@gmail.com>
15
 */
16
final class ViteBridge
17
{
18
    private string $manifestFile;
19
    private ?string $cacheFile;
20
    private string $assetPathPrefix;
21
    private ?string $devServerUrl;
22
    private bool $strict;
23

24
    /**
25
     * The $assetPath can be used to force absolute paths or set the base path. Ignored by the dev server.
26
     *
27
     * @param string $manifestFile Path to the Vite-generated manifest json file.
28
     * @param string|null $cacheFile This is where this locator stores (and reads from) its cache file. Must be writable.
29
     * @param string $assetPathPrefix This will typically be relative path from the public dir to the dir with assets, or empty string ''.
30
     * @param ?string $devServerUrl Vite's dev server URL (development only).
31
     * @param bool $strict Locators throw exceptions in strict mode, silently fail in lax mode.
32
     */
33
    public function __construct(
34
        string $manifestFile,
35
        ?string $cacheFile = null,
36
        string $assetPathPrefix = '',
37
        ?string $devServerUrl = null,
38
        bool $strict = false
39
    ) {
40
        $this->manifestFile = $manifestFile;
7✔
41
        $this->cacheFile = $cacheFile;
7✔
42
        $this->assetPathPrefix = $assetPathPrefix;
7✔
43
        $this->devServerUrl = $devServerUrl;
7✔
44
        $this->strict = $strict;
7✔
45
    }
7✔
46

47
    // TODO create a separate friction reducer
48
    public static function makeEntryLocator(
49
        bool $detectDevelopmentServer,
50
        string $devServerTellFileName,
51
        string $manifestFile,
52
        string $assetPathPrefix = '',
53
        ?string $devServerUrl = null,
54
        ?string $cacheFile = null,
55
        bool $strict = false
56
    ): ViteLocatorContract {
57
        $locators = [];
×
58

59
        // The detection should not run in production.
60
        if ($detectDevelopmentServer) {
×
NEW
61
            $locators[] = new TellFileDevelopmentLocator(
×
62
                $devServerTellFileName,
63
                $devServerUrl,
64
            );
65
        }
66

67
        // The bundle locator is always active and is the default.
68
        // When the dev server detection is off, the assets are served from a bundle (build).
69
        $locators[] = new ViteBuildLocator(
×
70
            $manifestFile,
71
            $cacheFile,
72
            $assetPathPrefix,
73
            $strict,
74
        );
75

76
        if ($strict) {
×
77
            // In strict mode, the final step is to throw an exception.
78
            $locators[] = function (string $name) {
×
79
                throw new RuntimeException('Not found: ' . $name);
×
80
            };
81
        }
82

83
        // This is a micro optimization: When there is only a single detector (the `ViteBuildLocator`), return it directly.
84
        if (count($locators) === 1) {
×
85
            return $locators[0];
×
86
        }
87

88
        return new CollectiveLocator(...$locators);
×
89
    }
90

91
    /**
92
     * Returns a preconfigured asset entry locator.
93
     * The method attempts to tell if the dev server is running by detecting the presence of a "tell" file.
94
     * Vite should be configured to create such a file when starting the dev server.
95
     *
96
     * @param bool $detectServer Set this to `false` in production or when the assets should always be located from a bundle.
97
     * @param string $tellFileName The location of the "tell" file.
98
     */
99
    public function makeServerTellEntryLocator(
100
        bool $detectServer,
101
        string $tellFileName
102
    ): ViteLocatorContract {
103
        return self::makeEntryLocator(
×
104
            $detectServer,
×
105
            $tellFileName,
×
106
            $this->manifestFile,
×
107
            $this->assetPathPrefix,
×
108
            $this->devServerUrl,
×
109
            $this->cacheFile,
×
110
            $this->strict,
×
111
        );
112
    }
113

114
    /**
115
     * Returns a preconfigured asset entry locator.
116
     * The locator reads the manifest file (or cache file) and serves asset objects.
117
     *
118
     * If the $useDevServer is `true`, links to Vite dev server are returned by the locator.
119
     *
120
     * This call does not detect whether the dev server is actually running or not (hence "passive").
121
     *
122
     * @param bool $useDevServer Set this to `false` in production or when the assets should be located from a bundle.
123
     */
124
    public function makePassiveEntryLocator(
125
        bool $useDevServer = false
126
    ): ViteLocatorContract {
127
        // If the dev server is used, it serves all the assets.
128
        if ($useDevServer) {
7✔
129
            return $this->makeDevServerEntryLocator();
7✔
130
        }
131
        // Otherwise, the assets are served from a bundle (build).
132
        return $this->makeBundleEntryLocator();
7✔
133
    }
134

135
    /**
136
     * Returns a preconfigured asset entry locator for development.
137
     * The locator returns entries pointing to the Vite development server for any asset being resolved.
138
     */
139
    public function makeDevServerEntryLocator(?string $devServerUrl = null): ViteLocatorContract
140
    {
141
        $devServerUrl ??= $this->devServerUrl;
7✔
142
        if ($devServerUrl === null) {
7✔
143
            throw new LogicException('The development server URL has not been provided.');
7✔
144
        }
145

146
        return new ViteServerLocator($devServerUrl);
7✔
147
    }
148

149
    /**
150
     * Returns a preconfigured asset entry locator for production builds.
151
     * The locator reads the manifest file (or cache file) and serves asset objects.
152
     */
153
    public function makeBundleEntryLocator(): ViteLocatorContract
154
    {
155
        $bundleLocator = new ViteBuildLocator(
7✔
156
            $this->manifestFile,
7✔
157
            $this->cacheFile,
7✔
158
            $this->assetPathPrefix,
7✔
159
            $this->strict,
7✔
160
        );
161
        if (!$this->strict) {
7✔
162
            return $bundleLocator;
7✔
163
        }
164
        // In strict mode, the final step is to throw an exception.
165
        return new CollectiveLocator(
7✔
166
            $bundleLocator,
167
            function (string $name) {
7✔
168
                throw new RuntimeException('Not found: ' . $name);
×
169
            },
7✔
170
        );
171
    }
172

173
    /**
174
     * Populates a cache file to be included instead of parsing the Vite bundle's JSON manifest file.
175
     * Should be called during the deployment/CI process as one of the build steps.
176
     */
177
    public function populateCache(): void
178
    {
179
        (new ViteBuildLocator(
7✔
180
            $this->manifestFile,
7✔
181
            $this->cacheFile,
7✔
182
            $this->assetPathPrefix,
7✔
183
            $this->strict,
7✔
184
        ))
185
            ->populateCache();
7✔
186
    }
7✔
187
}
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