From 6a216a36167ce4a34c0b2277138981ce610b86f3 Mon Sep 17 00:00:00 2001 From: Jesse Leite Date: Wed, 31 Jan 2024 13:16:54 -0500 Subject: [PATCH 1/4] Add .env var for easy enabling of pro without publishing config. --- config/editions.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/editions.php b/config/editions.php index e4839d4c3ea..1dbe5994a1a 100644 --- a/config/editions.php +++ b/config/editions.php @@ -2,7 +2,7 @@ return [ - 'pro' => false, + 'pro' => env('STATAMIC_PRO_ENABLED', false), 'addons' => [ // From cd78aa4937972cc3f5ef38f3fd99bf610b640550 Mon Sep 17 00:00:00 2001 From: Jesse Leite Date: Wed, 31 Jan 2024 13:23:04 -0500 Subject: [PATCH 2/4] Add `please pro:enable` command. --- src/Console/Commands/ProEnable.php | 118 +++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 src/Console/Commands/ProEnable.php diff --git a/src/Console/Commands/ProEnable.php b/src/Console/Commands/ProEnable.php new file mode 100644 index 00000000000..1e26ec897d7 --- /dev/null +++ b/src/Console/Commands/ProEnable.php @@ -0,0 +1,118 @@ +setProInEnvironmentFile()) { + return; + } + + $this->laravel['config']['statamic.editions.pro'] = true; + + $this->checkInfo('Statamic Pro successfully enabled.'); + } + + /** + * Set to pro in the environment file. + * + * @return bool + */ + protected function setProInEnvironmentFile() + { + if (! $this->confirmToProceed()) { + return false; + } + + if ($this->proEnvVarExists()) { + $this->ensureProInEnv(); + } else { + $this->appendProToEnv(); + } + + return true; + } + + /** + * Check whether the pro env var already exists. + * + * @return bool + */ + protected function proEnvVarExists() + { + return preg_match('/^STATAMIC_PRO_ENABLED=/m', $this->envContents()); + } + + /** + * Ensure pro in .env file. + * + * @return void + */ + protected function ensureProInEnv() + { + file_put_contents($this->envPath(), preg_replace( + '/^STATAMIC_PRO_ENABLED=.*$/m', + 'STATAMIC_PRO_ENABLED=true', + $this->envContents() + )); + } + + /** + * Append pro to end of .env file. + * + * @return void + */ + protected function appendProToEnv() + { + file_put_contents($this->envPath(), $this->envContents()."\nSTATAMIC_PRO_ENABLED=true"); + } + + /** + * Get app .env path. + * + * @return string + */ + protected function envPath() + { + return $this->laravel->environmentFilePath(); + } + + /** + * Get app .env contents. + * + * @return string + */ + protected function envContents() + { + return file_get_contents($this->envPath()); + } +} From c02d7674bb81778fda7e5928674857b25edd7baa Mon Sep 17 00:00:00 2001 From: Jesse Leite Date: Wed, 31 Jan 2024 13:24:23 -0500 Subject: [PATCH 3/4] Register command. --- src/Providers/ConsoleServiceProvider.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Providers/ConsoleServiceProvider.php b/src/Providers/ConsoleServiceProvider.php index 9353ccb1ae4..7f78a5c6ebb 100644 --- a/src/Providers/ConsoleServiceProvider.php +++ b/src/Providers/ConsoleServiceProvider.php @@ -46,6 +46,7 @@ class ConsoleServiceProvider extends ServiceProvider Commands\ImportGroups::class, Commands\ImportRoles::class, Commands\ImportUsers::class, + Commands\ProEnable::class, ]; public function boot() From b570a91f574fef17761c63ecb14c959c8ae62b3b Mon Sep 17 00:00:00 2001 From: Jesse Leite Date: Wed, 31 Jan 2024 13:53:12 -0500 Subject: [PATCH 4/4] If editions config is not referencing the new .env var, instruct user. --- src/Console/Commands/ProEnable.php | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/Console/Commands/ProEnable.php b/src/Console/Commands/ProEnable.php index 1e26ec897d7..8d7f291a8ac 100644 --- a/src/Console/Commands/ProEnable.php +++ b/src/Console/Commands/ProEnable.php @@ -39,7 +39,13 @@ public function handle() $this->laravel['config']['statamic.editions.pro'] = true; - $this->checkInfo('Statamic Pro successfully enabled.'); + $this->checkInfo('Statamic Pro successfully enabled in .env file!'); + + if ($this->configNotReferencingEnv()) { + $this->crossLine('Statamic editions config not currently referencing .env file.'); + $this->comment(PHP_EOL.'For this setting to take effect, please modify your [config/statamic/editions.php] as follows:'); + $this->line("'pro' => env('STATAMIC_PRO_ENABLED', false)"); + } } /** @@ -115,4 +121,18 @@ protected function envContents() { return file_get_contents($this->envPath()); } + + /** + * Check whether the editions config is referencing the .env var. + * + * @return bool + */ + protected function configNotReferencingEnv() + { + if (! file_exists($configPath = config_path('statamic/editions.php'))) { + return false; + } + + return ! preg_match('/[\'"]pro[\'"]\s*=>\s*env\([\'"]STATAMIC_PRO_ENABLED[\'"]/m', file_get_contents($configPath)); + } }