Skip to content

Commit

Permalink
Create command to update the config file if it exists
Browse files Browse the repository at this point in the history
  • Loading branch information
afonic committed Nov 2, 2024
1 parent 4ddf8fa commit 2b8492a
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
66 changes: 66 additions & 0 deletions src/Console/Commands/UpdateLivewireFilters.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace Reach\StatamicLivewireFilters\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\File;
use Statamic\Console\RunsInPlease;

class UpdateLivewireFilters extends Command
{
use RunsInPlease;

protected $signature = 'statamic-livewire-filters:update';

protected $description = 'Needed upgrades for Livewire Filters';

public function handle()
{
$this->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.');
}
}
4 changes: 4 additions & 0 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
];
Expand Down

0 comments on commit 2b8492a

Please sign in to comment.