Skip to content

Commit

Permalink
Register script only on non-console requests
Browse files Browse the repository at this point in the history
  • Loading branch information
bencroker committed Dec 20, 2024
1 parent 886e622 commit 595f864
Showing 1 changed file with 22 additions and 19 deletions.
41 changes: 22 additions & 19 deletions src/Datastar.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Datastar extends Module
/**
* The module settings.
*/
private ?SettingsModel $_settings = null;
private ?SettingsModel $settingsInternal = null;

/**
* The bootstrap process creates an instance of the module.
Expand Down Expand Up @@ -73,8 +73,11 @@ public function init(): void

$this->registerComponents();
$this->registerTwigExtension();
$this->registerScript();
$this->registerAutocompleteEvent();

if (!Craft::$app->getRequest()->getIsConsoleRequest()) {
$this->registerScript();
}
}

/**
Expand All @@ -92,11 +95,11 @@ public function expose(): void

public function getSettings(): SettingsModel
{
if ($this->_settings === null) {
$this->_settings = new SettingsModel(Craft::$app->getConfig()->getConfigFromFile('datastar'));
if ($this->settingsInternal === null) {
$this->settingsInternal = new SettingsModel(Craft::$app->getConfig()->getConfigFromFile('datastar'));
}

return $this->_settings;
return $this->settingsInternal;
}

private function registerComponents(): void
Expand All @@ -111,6 +114,20 @@ private function registerTwigExtension(): void
Craft::$app->getView()->registerTwigExtension(new DatastarTwigExtension());
}

private function registerAutocompleteEvent(): void
{
if (!class_exists('nystudio107\autocomplete\generators\AutocompleteTwigExtensionGenerator')) {
return;
}

Event::on(AutocompleteTwigExtensionGenerator::class,
AutocompleteTwigExtensionGenerator::EVENT_BEFORE_GENERATE,
function(DefineGeneratorValuesEvent $event) {
$event->values[$this->settings->signalsVariableName] = 'new \\' . SignalsModel::class . '()';
}
);
}

private function registerScript(): void
{
if (!$this->settings->registerScript) {
Expand All @@ -125,18 +142,4 @@ private function registerScript(): void

$this->exposeScriptUrl = $url;
}

private function registerAutocompleteEvent(): void
{
if (!class_exists('nystudio107\autocomplete\generators\AutocompleteTwigExtensionGenerator')) {
return;
}

Event::on(AutocompleteTwigExtensionGenerator::class,
AutocompleteTwigExtensionGenerator::EVENT_BEFORE_GENERATE,
function(DefineGeneratorValuesEvent $event) {
$event->values[$this->settings->signalsVariableName] = 'new \\' . SignalsModel::class . '()';
}
);
}
}

0 comments on commit 595f864

Please sign in to comment.