nystudio107 /
craft-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\vite; |
||||
| 12 | |||||
| 13 | use Craft; |
||||
| 14 | use craft\base\Model; |
||||
| 15 | use craft\base\Plugin; |
||||
| 16 | use craft\cloud\cli\controllers\UpController; |
||||
| 17 | use craft\events\CancelableEvent; |
||||
| 18 | use craft\events\RegisterCacheOptionsEvent; |
||||
| 19 | use craft\events\TemplateEvent; |
||||
| 20 | use craft\utilities\ClearCaches; |
||||
| 21 | use craft\web\twig\variables\CraftVariable; |
||||
| 22 | use craft\web\View; |
||||
| 23 | use nystudio107\vite\models\Settings; |
||||
| 24 | use nystudio107\vite\services\ServicesTrait; |
||||
| 25 | use nystudio107\vite\variables\ViteVariable; |
||||
| 26 | use yii\base\Event; |
||||
| 27 | |||||
| 28 | /** |
||||
| 29 | * Class Vite |
||||
| 30 | * |
||||
| 31 | * @author nystudio107 |
||||
|
0 ignored issues
–
show
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
|
|||||
| 32 | * @package Vite |
||||
|
0 ignored issues
–
show
|
|||||
| 33 | * @since 1.0.0 |
||||
|
0 ignored issues
–
show
|
|||||
| 34 | */ |
||||
|
0 ignored issues
–
show
|
|||||
| 35 | class Vite extends Plugin |
||||
| 36 | { |
||||
| 37 | // Traits |
||||
| 38 | // ========================================================================= |
||||
| 39 | |||||
| 40 | use ServicesTrait; |
||||
| 41 | |||||
| 42 | // Static Properties |
||||
| 43 | // ========================================================================= |
||||
| 44 | |||||
| 45 | /** |
||||
|
0 ignored issues
–
show
|
|||||
| 46 | * @var Vite |
||||
| 47 | */ |
||||
| 48 | public static Plugin $plugin; |
||||
| 49 | |||||
| 50 | /** |
||||
|
0 ignored issues
–
show
|
|||||
| 51 | * @var string |
||||
| 52 | */ |
||||
| 53 | public static string $templateName; |
||||
| 54 | |||||
| 55 | // Public Properties |
||||
| 56 | // ========================================================================= |
||||
| 57 | |||||
| 58 | /** |
||||
|
0 ignored issues
–
show
|
|||||
| 59 | * @var string |
||||
| 60 | */ |
||||
| 61 | public string $schemaVersion = '1.0.0'; |
||||
| 62 | |||||
| 63 | /** |
||||
|
0 ignored issues
–
show
|
|||||
| 64 | * @var bool |
||||
| 65 | */ |
||||
| 66 | public bool $hasCpSettings = false; |
||||
| 67 | |||||
| 68 | /** |
||||
|
0 ignored issues
–
show
|
|||||
| 69 | * @var bool |
||||
| 70 | */ |
||||
| 71 | public bool $hasCpSection = false; |
||||
| 72 | |||||
| 73 | // Public Methods |
||||
| 74 | // ========================================================================= |
||||
| 75 | |||||
| 76 | /** |
||||
|
0 ignored issues
–
show
|
|||||
| 77 | * @inheritdoc |
||||
| 78 | */ |
||||
|
0 ignored issues
–
show
|
|||||
| 79 | public function init(): void |
||||
| 80 | { |
||||
| 81 | parent::init(); |
||||
| 82 | self::$plugin = $this; |
||||
| 83 | // Add our event listeners |
||||
| 84 | $this->installEventListeners(); |
||||
| 85 | // Log that the plugin has loaded |
||||
| 86 | Craft::info( |
||||
| 87 | Craft::t( |
||||
| 88 | 'vite', |
||||
| 89 | '{name} plugin loaded', |
||||
| 90 | ['name' => $this->name] |
||||
| 91 | ), |
||||
| 92 | __METHOD__ |
||||
| 93 | ); |
||||
| 94 | } |
||||
| 95 | |||||
| 96 | /** |
||||
| 97 | * Clear all the caches! |
||||
| 98 | */ |
||||
|
0 ignored issues
–
show
|
|||||
| 99 | public function clearAllCaches(): void |
||||
| 100 | { |
||||
| 101 | // Clear all of Vite's caches |
||||
| 102 | $this->vite->invalidateCaches(); |
||||
| 103 | } |
||||
| 104 | |||||
| 105 | // Protected Methods |
||||
| 106 | // ========================================================================= |
||||
| 107 | |||||
| 108 | /** |
||||
| 109 | * Install our event listeners. |
||||
| 110 | */ |
||||
|
0 ignored issues
–
show
|
|||||
| 111 | protected function installEventListeners(): void |
||||
| 112 | { |
||||
| 113 | // Register our variable |
||||
| 114 | Event::on( |
||||
| 115 | CraftVariable::class, |
||||
| 116 | CraftVariable::EVENT_INIT, |
||||
| 117 | function(Event $event) { |
||||
|
0 ignored issues
–
show
|
|||||
| 118 | /** @var CraftVariable $variable */ |
||||
|
0 ignored issues
–
show
|
|||||
| 119 | $variable = $event->sender; |
||||
| 120 | $variable->set('vite', [ |
||||
|
0 ignored issues
–
show
|
|||||
| 121 | 'class' => ViteVariable::class, |
||||
| 122 | 'viteService' => $this->vite, |
||||
| 123 | ]); |
||||
|
0 ignored issues
–
show
For multi-line function calls, the closing parenthesis should be on a new line.
If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line: someFunctionCall(
$firstArgument,
$secondArgument,
$thirdArgument
); // Closing parenthesis on a new line.
Loading history...
|
|||||
| 124 | } |
||||
| 125 | ); |
||||
| 126 | // Handler: ClearCaches::EVENT_REGISTER_CACHE_OPTIONS |
||||
| 127 | Event::on( |
||||
| 128 | ClearCaches::class, |
||||
| 129 | ClearCaches::EVENT_REGISTER_CACHE_OPTIONS, |
||||
| 130 | function(RegisterCacheOptionsEvent $event) { |
||||
|
0 ignored issues
–
show
|
|||||
| 131 | Craft::debug( |
||||
| 132 | 'ClearCaches::EVENT_REGISTER_CACHE_OPTIONS', |
||||
| 133 | __METHOD__ |
||||
| 134 | ); |
||||
| 135 | // Register our caches for the Clear Cache Utility |
||||
| 136 | $event->options = array_merge( |
||||
| 137 | $event->options, |
||||
| 138 | $this->customAdminCpCacheOptions() |
||||
| 139 | ); |
||||
| 140 | } |
||||
| 141 | ); |
||||
| 142 | // Remember the name of the currently rendering template |
||||
| 143 | // Handler: View::EVENT_BEFORE_RENDER_PAGE_TEMPLATE |
||||
| 144 | Event::on( |
||||
| 145 | View::class, |
||||
| 146 | View::EVENT_BEFORE_RENDER_PAGE_TEMPLATE, |
||||
| 147 | static function(TemplateEvent $event) { |
||||
|
0 ignored issues
–
show
|
|||||
| 148 | self::$templateName = $event->template; |
||||
| 149 | } |
||||
| 150 | ); |
||||
| 151 | // Clears cache after craft cloud/up is run, which Craft Cloud runs on deploy |
||||
| 152 | // Handler: UpController::EVENT_AFTER_UP |
||||
| 153 | if (class_exists(UpController::class)) { |
||||
| 154 | Event::on( |
||||
| 155 | UpController::class, |
||||
| 156 | UpController::EVENT_AFTER_UP, |
||||
| 157 | function(CancelableEvent $event) { |
||||
|
0 ignored issues
–
show
The parameter
$event is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. Loading history...
|
|||||
| 158 | $this->clearAllCaches(); |
||||
| 159 | } |
||||
| 160 | ); |
||||
| 161 | } |
||||
| 162 | } |
||||
| 163 | |||||
| 164 | /** |
||||
| 165 | * Returns the custom Control Panel cache options. |
||||
| 166 | * |
||||
| 167 | * @return array |
||||
| 168 | */ |
||||
| 169 | protected function customAdminCpCacheOptions(): array |
||||
| 170 | { |
||||
| 171 | return [ |
||||
| 172 | // Manifest cache |
||||
| 173 | [ |
||||
| 174 | 'key' => 'vite-file-cache', |
||||
| 175 | 'label' => Craft::t('vite', 'Vite Cache'), |
||||
| 176 | 'action' => [$this, 'clearAllCaches'], |
||||
| 177 | ], |
||||
| 178 | ]; |
||||
| 179 | } |
||||
| 180 | |||||
| 181 | /** |
||||
|
0 ignored issues
–
show
|
|||||
| 182 | * @inheritdoc |
||||
| 183 | */ |
||||
|
0 ignored issues
–
show
|
|||||
| 184 | protected function createSettingsModel(): ?Model |
||||
| 185 | { |
||||
| 186 | return new Settings(); |
||||
| 187 | } |
||||
| 188 | } |
||||
| 189 |