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' => [ // diff --git a/src/Console/Commands/ProEnable.php b/src/Console/Commands/ProEnable.php new file mode 100644 index 00000000000..8d7f291a8ac --- /dev/null +++ b/src/Console/Commands/ProEnable.php @@ -0,0 +1,138 @@ +setProInEnvironmentFile()) { + return; + } + + $this->laravel['config']['statamic.editions.pro'] = true; + + $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)"); + } + } + + /** + * 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()); + } + + /** + * 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)); + } +} 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()