From 2b8492a468e659ea255b1fa1c0179550225e2b70 Mon Sep 17 00:00:00 2001 From: Iosif Chatzimichail Date: Sat, 2 Nov 2024 12:53:24 +0200 Subject: [PATCH] Create command to update the config file if it exists --- .../Commands/UpdateLivewireFilters.php | 66 +++++++++++++++++++ src/ServiceProvider.php | 4 ++ 2 files changed, 70 insertions(+) create mode 100644 src/Console/Commands/UpdateLivewireFilters.php diff --git a/src/Console/Commands/UpdateLivewireFilters.php b/src/Console/Commands/UpdateLivewireFilters.php new file mode 100644 index 0000000..ba5c1b1 --- /dev/null +++ b/src/Console/Commands/UpdateLivewireFilters.php @@ -0,0 +1,66 @@ +updateConfigForCustomQueryString(); + } + + protected function updateConfigForCustomQueryString() + { + $configPath = config_path('statamic-livewire-filters.php'); + + if (! File::exists($configPath)) { + return; + } + + // Read file content as string + $contents = File::get($configPath); + + // Check if options already exist + $hasCustomQueryString = preg_match("/['\"']custom_query_string['\"']\s*=>/", $contents); + $hasAliases = preg_match("/['\"']custom_query_string_aliases['\"']\s*=>/", $contents); + + if ($hasCustomQueryString && $hasAliases) { + return; + } + + // New options using nowdoc for exact formatting + $newOptions = <<<'EOT' + + // Enable custom query string + 'custom_query_string' => false, + + // Set the aliases for each custom query string parameter + 'custom_query_string_aliases' => [ + // + ], + + EOT; + + // Find position of last closing bracket + $pos = strrpos($contents, '];'); + + if ($pos !== false) { + // Insert new options before closing bracket + $contents = substr_replace($contents, $newOptions, $pos, 0); + } + + File::put($configPath, $contents); + + $this->info('Statamic Livewire Filters config file updated successfully.'); + } +} diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index 95836ad..69d6dc7 100644 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -32,6 +32,10 @@ class ServiceProvider extends AddonServiceProvider \Reach\StatamicLivewireFilters\Scopes\Multiselect::class, ]; + protected $commands = [ + \Reach\StatamicLivewireFilters\Console\Commands\UpdateLivewireFilters::class, + ]; + protected $publishables = [ __DIR__.'/../resources/build' => 'build', ];