nystudio107 /
craft-plugin-vite
| 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
Loading history...
|
|||||||
| 8 | * @copyright Copyright (c) 2021 nystudio107 |
||||||
|
0 ignored issues
–
show
|
|||||||
| 9 | */ |
||||||
|
0 ignored issues
–
show
|
|||||||
| 10 | |||||||
| 11 | namespace nystudio107\pluginvite\variables; |
||||||
| 12 | |||||||
| 13 | use craft\helpers\Template; |
||||||
| 14 | use nystudio107\pluginvite\services\ViteService; |
||||||
| 15 | use Twig\Markup; |
||||||
| 16 | use yii\base\InvalidConfigException; |
||||||
| 17 | |||||||
| 18 | /** |
||||||
|
0 ignored issues
–
show
|
|||||||
| 19 | * @author nystudio107 |
||||||
|
0 ignored issues
–
show
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
|
|||||||
| 20 | * @package Vite |
||||||
|
0 ignored issues
–
show
|
|||||||
| 21 | * @since 1.0.4 |
||||||
|
0 ignored issues
–
show
|
|||||||
| 22 | */ |
||||||
|
0 ignored issues
–
show
|
|||||||
| 23 | trait ViteVariableTrait |
||||||
| 24 | { |
||||||
| 25 | // Public Properties |
||||||
| 26 | // ========================================================================= |
||||||
| 27 | |||||||
| 28 | /** |
||||||
|
0 ignored issues
–
show
|
|||||||
| 29 | * @var null|ViteService the Vite service |
||||||
| 30 | */ |
||||||
| 31 | public ?ViteService $viteService = null; |
||||||
| 32 | |||||||
| 33 | // Public Methods |
||||||
| 34 | // ========================================================================= |
||||||
| 35 | |||||||
| 36 | /** |
||||||
| 37 | * Return the appropriate tags to load the Vite script, either via the dev server or |
||||||
| 38 | * extracting it from the manifest.json file |
||||||
| 39 | * |
||||||
| 40 | * @param string $path |
||||||
|
0 ignored issues
–
show
|
|||||||
| 41 | * @param bool $asyncCss |
||||||
|
0 ignored issues
–
show
|
|||||||
| 42 | * @param array $scriptTagAttrs |
||||||
|
0 ignored issues
–
show
|
|||||||
| 43 | * @param array $cssTagAttrs |
||||||
|
0 ignored issues
–
show
|
|||||||
| 44 | * |
||||||
| 45 | * @return Markup |
||||||
| 46 | */ |
||||||
| 47 | public function script(string $path, bool $asyncCss = true, array $scriptTagAttrs = [], array $cssTagAttrs = []): Markup |
||||||
| 48 | { |
||||||
| 49 | return Template::raw( |
||||||
| 50 | $this->viteService->script($path, $asyncCss, $scriptTagAttrs, $cssTagAttrs) |
||||||
|
0 ignored issues
–
show
The method
script() does not exist on null.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. Loading history...
|
|||||||
| 51 | ); |
||||||
| 52 | } |
||||||
| 53 | |||||||
| 54 | /** |
||||||
| 55 | * Register the appropriate tags to the Craft View to load the Vite script, either via the dev server or |
||||||
| 56 | * extracting it from the manifest.json file |
||||||
| 57 | * |
||||||
| 58 | * @param string $path |
||||||
|
0 ignored issues
–
show
|
|||||||
| 59 | * @param bool $asyncCss |
||||||
|
0 ignored issues
–
show
|
|||||||
| 60 | * @param array $scriptTagAttrs |
||||||
|
0 ignored issues
–
show
|
|||||||
| 61 | * @param array $cssTagAttrs |
||||||
|
0 ignored issues
–
show
|
|||||||
| 62 | * |
||||||
| 63 | * @return Markup |
||||||
| 64 | * @throws InvalidConfigException |
||||||
| 65 | */ |
||||||
| 66 | public function register(string $path, bool $asyncCss = true, array $scriptTagAttrs = [], array $cssTagAttrs = []): Markup |
||||||
| 67 | { |
||||||
| 68 | $this->viteService->register($path, $asyncCss, $scriptTagAttrs, $cssTagAttrs); |
||||||
| 69 | |||||||
| 70 | return Template::raw(''); |
||||||
| 71 | } |
||||||
| 72 | |||||||
| 73 | /** |
||||||
| 74 | * Return the URL for the given entry |
||||||
| 75 | * |
||||||
| 76 | * @param string $path |
||||||
|
0 ignored issues
–
show
|
|||||||
| 77 | * |
||||||
| 78 | * @return Markup |
||||||
| 79 | */ |
||||||
| 80 | public function entry(string $path): Markup |
||||||
| 81 | { |
||||||
| 82 | return Template::raw( |
||||||
| 83 | $this->viteService->entry($path) |
||||||
| 84 | ); |
||||||
| 85 | } |
||||||
| 86 | |||||||
| 87 | /** |
||||||
| 88 | * Return the integrity hash (or an empty string if not present) for the given entry |
||||||
| 89 | * |
||||||
| 90 | * @param string $path |
||||||
|
0 ignored issues
–
show
|
|||||||
| 91 | * |
||||||
| 92 | * @return string |
||||||
| 93 | */ |
||||||
| 94 | public function integrity(string $path): string |
||||||
| 95 | { |
||||||
| 96 | return Template::raw( |
||||||
| 97 | $this->viteService->integrity($path) |
||||||
| 98 | ); |
||||||
| 99 | } |
||||||
| 100 | |||||||
| 101 | /** |
||||||
|
0 ignored issues
–
show
|
|||||||
| 102 | * Return the URL for the given asset |
||||||
| 103 | * |
||||||
| 104 | * @param string $path |
||||||
|
0 ignored issues
–
show
|
|||||||
| 105 | * |
||||||
| 106 | * @return Markup |
||||||
| 107 | */ |
||||||
| 108 | public function asset(string $path, bool $public = false): Markup |
||||||
| 109 | { |
||||||
| 110 | return Template::raw( |
||||||
| 111 | $this->viteService->asset($path, $public) |
||||||
| 112 | ); |
||||||
| 113 | } |
||||||
| 114 | |||||||
| 115 | /** |
||||||
| 116 | * Inline the contents of a local file (via path) or remote file (via URL) in your templates. |
||||||
| 117 | * Yii2 aliases and/or environment variables may be used |
||||||
| 118 | * |
||||||
| 119 | * @param string $pathOrUrl |
||||||
|
0 ignored issues
–
show
|
|||||||
| 120 | * |
||||||
| 121 | * @return Markup |
||||||
| 122 | */ |
||||||
| 123 | public function inline(string $pathOrUrl): Markup |
||||||
| 124 | { |
||||||
| 125 | $file = $this->viteService->fetch($pathOrUrl); |
||||||
| 126 | if ($file === null) { |
||||||
| 127 | $file = ''; |
||||||
| 128 | } |
||||||
| 129 | |||||||
| 130 | return Template::raw($file); |
||||||
|
0 ignored issues
–
show
It seems like
$file can also be of type array; however, parameter $value of craft\helpers\Template::raw() does only seem to accept string, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 131 | } |
||||||
| 132 | |||||||
| 133 | /** |
||||||
| 134 | * Determine whether the Vite dev server is running |
||||||
| 135 | * |
||||||
| 136 | * @return bool |
||||||
| 137 | */ |
||||||
| 138 | public function devServerRunning(): bool |
||||||
| 139 | { |
||||||
| 140 | return $this->viteService->devServerRunning(); |
||||||
| 141 | } |
||||||
| 142 | } |
||||||
| 143 |