1 | <?php |
||||||
2 | /** |
||||||
3 | * Vite plugin for Craft CMS |
||||||
4 | * |
||||||
5 | * Allows the use of the Vite.js next generation frontend tooling with Craft CMS |
||||||
6 | * |
||||||
7 | * @link https://nystudio107.com |
||||||
0 ignored issues
–
show
Coding Style
introduced
by
![]() |
|||||||
8 | * @copyright Copyright (c) 2021 nystudio107 |
||||||
0 ignored issues
–
show
|
|||||||
9 | */ |
||||||
0 ignored issues
–
show
|
|||||||
10 | |||||||
11 | namespace nystudio107\pluginvite\services; |
||||||
12 | |||||||
13 | use Craft; |
||||||
14 | use craft\helpers\App; |
||||||
15 | use nystudio107\pluginvite\helpers\FileHelper; |
||||||
16 | |||||||
17 | /** |
||||||
0 ignored issues
–
show
|
|||||||
18 | * @author nystudio107 |
||||||
0 ignored issues
–
show
Content of the @author tag must be in the form "Display Name <[email protected]>"
![]() |
|||||||
19 | * @package Vite |
||||||
0 ignored issues
–
show
|
|||||||
20 | * @since 1.0.0 |
||||||
0 ignored issues
–
show
|
|||||||
21 | */ |
||||||
0 ignored issues
–
show
|
|||||||
22 | class VitePluginService extends ViteService |
||||||
23 | { |
||||||
24 | // Constants |
||||||
25 | // ========================================================================= |
||||||
26 | |||||||
27 | protected const MANIFEST_FILE_NAME = 'manifest.json'; |
||||||
28 | |||||||
29 | // Public Properties |
||||||
30 | // ========================================================================= |
||||||
31 | |||||||
32 | /** |
||||||
0 ignored issues
–
show
|
|||||||
33 | * @var string AssetBundle class name to get the published URLs from |
||||||
34 | */ |
||||||
35 | public string $assetClass = ''; |
||||||
36 | |||||||
37 | /** |
||||||
0 ignored issues
–
show
|
|||||||
38 | * @var string The environment variable to look for in order to enable the devServer; the value doesn't matter, |
||||||
39 | * it just needs to exist |
||||||
40 | */ |
||||||
41 | public string $pluginDevServerEnvVar = 'VITE_PLUGIN_DEVSERVER'; |
||||||
42 | |||||||
43 | /** |
||||||
0 ignored issues
–
show
|
|||||||
44 | * @var bool Normally the AssetBundle only needs to be registered for CP and Preview requests, and having it |
||||||
45 | * not load for frontend requests saves a db write: https://github.com/nystudio107/craft-plugin-vite/issues/27 |
||||||
46 | */ |
||||||
47 | public bool $useForAllRequests = false; |
||||||
48 | |||||||
49 | /** |
||||||
0 ignored issues
–
show
|
|||||||
50 | * @var array|string[] If the first segment of the request matches any items in the array, load the AssetBundle, too. |
||||||
51 | * Needed for things that add frontend preview targets like SEOmatic |
||||||
52 | */ |
||||||
53 | public array $firstSegmentRequests = [ |
||||||
54 | 'seomatic', |
||||||
55 | ]; |
||||||
56 | |||||||
57 | /** |
||||||
0 ignored issues
–
show
|
|||||||
58 | * @inheritDoc |
||||||
59 | */ |
||||||
0 ignored issues
–
show
|
|||||||
60 | public function init(): void |
||||||
61 | { |
||||||
62 | // See if the $pluginDevServerEnvVar env var exists, and if not, don't run off of the dev server |
||||||
63 | $useDevServer = (bool)App::env($this->pluginDevServerEnvVar); |
||||||
64 | if ($useDevServer === false) { |
||||||
65 | $this->useDevServer = false; |
||||||
66 | } |
||||||
67 | parent::init(); |
||||||
68 | // If we're in a plugin, make sure the caches are unique |
||||||
69 | if ($this->assetClass) { |
||||||
70 | $this->cacheKeySuffix = $this->assetClass; |
||||||
71 | } |
||||||
72 | if ($this->devServerRunning()) { |
||||||
73 | $this->invalidateCaches(); |
||||||
74 | } |
||||||
75 | // If we have no asset bundle class, or the dev server is running, don't swap in our `/cpresources/` paths |
||||||
76 | if (!$this->assetClass || $this->devServerRunning()) { |
||||||
77 | return; |
||||||
78 | } |
||||||
79 | // The Vite service is generally only needed for CP requests & previews, save a db write, see: |
||||||
80 | // https://github.com/nystudio107/craft-plugin-vite/issues/27 |
||||||
81 | $request = Craft::$app->getRequest(); |
||||||
82 | if (!$this->useForAllRequests && !$request->getIsConsoleRequest()) { |
||||||
83 | if (!$request->getIsCpRequest() && !$request->getIsPreview() && !in_array($request->getSegment(1), $this->firstSegmentRequests, true)) { |
||||||
84 | return; |
||||||
85 | } |
||||||
86 | } |
||||||
87 | // Map the $manifestPath and $serverPublic to the hashed `/cpresources/` path & URL for our AssetBundle |
||||||
88 | $bundle = new $this->assetClass(); |
||||||
89 | $baseAssetsUrl = Craft::$app->assetManager->getPublishedUrl( |
||||||
90 | $bundle->sourcePath, |
||||||
91 | true |
||||||
0 ignored issues
–
show
The call to
yii\web\AssetManager::getPublishedUrl() has too many arguments starting with true .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue. If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above. ![]() |
|||||||
92 | ); |
||||||
93 | $this->manifestPath = FileHelper::createUrl($bundle->sourcePath, self::MANIFEST_FILE_NAME); |
||||||
94 | if ($baseAssetsUrl !== false) { |
||||||
95 | $this->serverPublic = $baseAssetsUrl; |
||||||
96 | } |
||||||
97 | } |
||||||
98 | } |
||||||
99 |