-
-
Notifications
You must be signed in to change notification settings - Fork 35
[WIP] Add support for generating env specific config caches #219
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,12 +2,129 @@ | |
|
|
||
| namespace Winter\Storm\Foundation\Console; | ||
|
|
||
| use Exception; | ||
| use Illuminate\Contracts\Console\Kernel as ConsoleKernelContract; | ||
| use Illuminate\Foundation\Console\ConfigCacheCommand as BaseCommand; | ||
| use LogicException; | ||
| use Throwable; | ||
|
|
||
| class ConfigCacheCommand extends BaseCommand | ||
| { | ||
| /** | ||
| * @var string The console command signature. | ||
| */ | ||
| protected $signature = 'config:cache | ||
| {env? : Which environment should be cached?} | ||
| '; | ||
|
|
||
| /** | ||
| * Execute the console command. | ||
| * | ||
| * @return void | ||
| * | ||
| * @throws \LogicException | ||
| */ | ||
| public function handle() | ||
| { | ||
| $this->components->warn('Caching configuration files is not supported in Winter CMS. See https://github.com/wintercms/winter/issues/1297#issuecomment-2624578966'); | ||
| $args = []; | ||
| if ($this->argument('env')) { | ||
| $args['env'] = $this->argument('env'); | ||
| } | ||
|
|
||
| // This is the only change to the parent, it allows us to only clear the requested config | ||
| $this->callSilent('config:clear', $args); | ||
|
|
||
| $config = $this->getFreshConfiguration(); | ||
|
|
||
| $configPath = $this->laravel->getCachedConfigPath(); | ||
|
|
||
| $this->files->put( | ||
| $configPath, | ||
| '<?php return ' . var_export($config, true) . ';' . PHP_EOL | ||
| ); | ||
|
|
||
| try { | ||
| require $configPath; | ||
| } catch (Throwable $e) { | ||
| $this->files->delete($configPath); | ||
|
|
||
| throw new LogicException('Your configuration files are not serializable.', 0, $e); | ||
| } | ||
|
|
||
| $this->components->info('Configuration cached successfully.'); | ||
| } | ||
|
|
||
| /** | ||
| * Boot a fresh copy of the application configuration. | ||
| * | ||
| * @return array | ||
| */ | ||
| protected function getFreshConfiguration() | ||
| { | ||
| // This allows us to detect and override the "env by subdomain" feature of Winter | ||
| if ($this->argument('env') && ($environment = $this->getEnvironmentConfiguration())) { | ||
| // Grab hosts as env => domain | ||
| $hosts = isset($environment['hosts']) | ||
| ? array_flip($environment['hosts']) | ||
| : []; | ||
|
|
||
| // if we have env, set the host to the domain to "trick" the system into registering the correct config | ||
| if (isset($hosts[$this->argument('env')])) { | ||
| $_SERVER['HTTP_HOST'] = $hosts[$this->argument('env')]; | ||
| } | ||
| } | ||
|
|
||
| $app = require $this->laravel->bootstrapPath() . '/app.php'; | ||
|
|
||
| // This allows us to inform the LoadConfiguration class to not load from cache on fresh load | ||
| $app['disableConfigCacheLoading'] = true; | ||
|
|
||
| // This overrides the new app and existing app's env | ||
| if ($this->argument('env')) { | ||
| $this->laravel->detectEnvironment(fn() => $this->argument('env') ?? $app['env']); | ||
| $app->detectEnvironment(fn() => $this->argument('env') ?? $app['env']); | ||
| } | ||
|
|
||
| // Stolen stuff from the Laravel command | ||
| $app->useStoragePath($this->laravel->storagePath()); | ||
| $app->make(ConsoleKernelContract::class)->bootstrap(); | ||
|
|
||
| // Force preload all registered configs | ||
| foreach ($app['config']->getNamespaces() as $namespace => $path) { | ||
| foreach (glob($path . DIRECTORY_SEPARATOR . '*.php') as $file) { | ||
| $app['config']->get($namespace . '::' . pathinfo($file, PATHINFO_FILENAME)); | ||
| } | ||
| } | ||
|
|
||
| return $app['config']->all(); | ||
| } | ||
|
|
||
| /** | ||
| * Load the environment configuration. | ||
| * @TODO: This is copied from LoadConfiguration, should be exposed somewhere... | ||
| * @see storm/src/Foundation/Bootstrap/LoadConfiguration.php | ||
| */ | ||
| protected function getEnvironmentConfiguration(): array | ||
| { | ||
| $config = []; | ||
| $environment = env('APP_ENV'); | ||
| if ($environment && file_exists($configPath = base_path() . '/config/' . $environment . '/environment.php')) { | ||
| try { | ||
| $config = require $configPath; | ||
| } | ||
| catch (Exception $ex) { | ||
| // | ||
| } | ||
| } | ||
| elseif (file_exists($configPath = base_path() . '/config/environment.php')) { | ||
| try { | ||
| $config = require $configPath; | ||
| } | ||
| catch (Exception $ex) { | ||
| // | ||
| } | ||
|
Comment on lines
+123
to
+125
|
||
| } | ||
|
|
||
| return $config; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -6,8 +6,32 @@ | |||||||||||||||||||||
|
|
||||||||||||||||||||||
| class ConfigClearCommand extends BaseCommand | ||||||||||||||||||||||
| { | ||||||||||||||||||||||
| public function handle() | ||||||||||||||||||||||
| /** | ||||||||||||||||||||||
| * @var string The console command signature. | ||||||||||||||||||||||
| */ | ||||||||||||||||||||||
| protected $signature = 'config:clear | ||||||||||||||||||||||
| {env? : Which environment should be cleared?} | ||||||||||||||||||||||
| '; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| /** | ||||||||||||||||||||||
| * Execute the console command. | ||||||||||||||||||||||
| * | ||||||||||||||||||||||
| * @return void | ||||||||||||||||||||||
| */ | ||||||||||||||||||||||
| public function handle(): void | ||||||||||||||||||||||
| { | ||||||||||||||||||||||
| $this->components->warn('Caching configuration files is not supported in Winter CMS. See https://github.com/wintercms/winter/issues/1297#issuecomment-2624578966'); | ||||||||||||||||||||||
| $configPath = $this->laravel->getCachedConfigPath(); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| if ($this->argument('env')) { | ||||||||||||||||||||||
| $configPath = realpath( | ||||||||||||||||||||||
| dirname($this->laravel->getCachedConfigPath()) | ||||||||||||||||||||||
| . DIRECTORY_SEPARATOR | ||||||||||||||||||||||
| . $this->argument('env') | ||||||||||||||||||||||
| . '.config.php' | ||||||||||||||||||||||
| ); | ||||||||||||||||||||||
|
Comment on lines
+26
to
+31
|
||||||||||||||||||||||
| $configPath = realpath( | |
| dirname($this->laravel->getCachedConfigPath()) | |
| . DIRECTORY_SEPARATOR | |
| . $this->argument('env') | |
| . '.config.php' | |
| ); | |
| $configPath = dirname($this->laravel->getCachedConfigPath()) | |
| . DIRECTORY_SEPARATOR | |
| . $this->argument('env') | |
| . '.config.php'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Empty catch blocks make debugging difficult. Consider logging the exception or adding a comment explaining why the exception is being silently ignored.