From c7bd9144cba29e72e4fdeec92abbbc05d652cbd7 Mon Sep 17 00:00:00 2001 From: alt Date: Wed, 19 Mar 2025 07:35:30 +0700 Subject: [PATCH 1/2] Better early bootstrap --- src/WpStarter/Wordpress/Application.php | 42 ++++++++++++------- .../Bootstrap/HasEarlyBootstrapers.php | 33 +++++++++++++++ src/WpStarter/Wordpress/Console/Kernel.php | 17 ++------ src/WpStarter/Wordpress/Kernel.php | 16 +------ .../Setting/SettingServiceProvider.php | 20 +++++---- 5 files changed, 78 insertions(+), 50 deletions(-) create mode 100644 src/WpStarter/Wordpress/Bootstrap/HasEarlyBootstrapers.php diff --git a/src/WpStarter/Wordpress/Application.php b/src/WpStarter/Wordpress/Application.php index 8e6c564..cb13a34 100644 --- a/src/WpStarter/Wordpress/Application.php +++ b/src/WpStarter/Wordpress/Application.php @@ -11,9 +11,13 @@ class Application extends \WpStarter\Foundation\Application * * @var string */ - const VERSION = '1.9.9'; - - protected $bootstrappedList = []; + const VERSION = '1.9.10'; + /** + * Indicates if the application has been early bootstrapped before. + * + * @var bool + */ + protected $hasBeenEarlyBootstrapped = false; protected function registerBaseServiceProviders() { @@ -30,22 +34,30 @@ public function registerCoreContainerAliases() $this->alias('app',self::class); } - function bootstrapWith(array $bootstrappers) + /** + * Run the given array of bootstrap classes. + * + * @param string[] $bootstrappers + * @return void + */ + public function earlyBootstrapWith(array $bootstrappers) { - $this->hasBeenBootstrapped = true; - + $this->hasBeenEarlyBootstrapped = true; foreach ($bootstrappers as $bootstrapper) { - $this->bootstrapOne($bootstrapper); - } - } + $this['events']->dispatch('bootstrapping: '.$bootstrapper, [$this]); - function bootstrapOne($bootstrapper) - { - if (!isset($this->bootstrappedList[$bootstrapper])) { - $this->bootstrappedList[$bootstrapper] = true; - $this['events']->dispatch('bootstrapping: ' . $bootstrapper, [$this]); $this->make($bootstrapper)->bootstrap($this); - $this['events']->dispatch('bootstrapped: ' . $bootstrapper, [$this]); + + $this['events']->dispatch('bootstrapped: '.$bootstrapper, [$this]); } } + /** + * Determine if the application has been early bootstrapped before. + * + * @return bool + */ + public function hasBeenEarlyBootstrapped() + { + return $this->hasBeenEarlyBootstrapped; + } } diff --git a/src/WpStarter/Wordpress/Bootstrap/HasEarlyBootstrapers.php b/src/WpStarter/Wordpress/Bootstrap/HasEarlyBootstrapers.php new file mode 100644 index 0000000..36e08f3 --- /dev/null +++ b/src/WpStarter/Wordpress/Bootstrap/HasEarlyBootstrapers.php @@ -0,0 +1,33 @@ +app->hasBeenEarlyBootstrapped()) { + $this->app->earlyBootstrapWith($this->earlyBootstrappers()); + } + } + protected function earlyBootstrappers() + { + return $this->earlyBootstrappers; + } + protected function bootstrappers() + { + $bootstrappers = parent::bootstrappers(); + if($this->app->hasBeenEarlyBootstrapped()) { + //Remove early bootstrapper from bootstrapper list + $bootstrappers=array_diff($bootstrappers, $this->earlyBootstrappers); + } + return $bootstrappers; + } +} diff --git a/src/WpStarter/Wordpress/Console/Kernel.php b/src/WpStarter/Wordpress/Console/Kernel.php index 2545d91..1bfd529 100644 --- a/src/WpStarter/Wordpress/Console/Kernel.php +++ b/src/WpStarter/Wordpress/Console/Kernel.php @@ -3,15 +3,11 @@ namespace WpStarter\Wordpress\Console; use WpStarter\Foundation\Console\Kernel as ConsoleKernel; +use WpStarter\Wordpress\Bootstrap\HasEarlyBootstrapers; class Kernel extends ConsoleKernel { - protected $earlyBootstrapers = [ - \WpStarter\Foundation\Bootstrap\LoadEnvironmentVariables::class, - \WpStarter\Foundation\Bootstrap\LoadConfiguration::class, - \WpStarter\Wordpress\Bootstrap\HandleExceptions::class, - \WpStarter\Foundation\Bootstrap\RegisterFacades::class, - ]; + use HasEarlyBootstrapers; /** * The bootstrap classes for the application. * @@ -26,11 +22,4 @@ class Kernel extends ConsoleKernel \WpStarter\Foundation\Bootstrap\RegisterProviders::class, \WpStarter\Foundation\Bootstrap\BootProviders::class, ]; - - function earlyBootstrap() - { - foreach ($this->earlyBootstrapers as $bootstraper) { - $this->app->bootstrapOne($bootstraper); - } - } -} \ No newline at end of file +} diff --git a/src/WpStarter/Wordpress/Kernel.php b/src/WpStarter/Wordpress/Kernel.php index 89f0fc3..9d16dbc 100644 --- a/src/WpStarter/Wordpress/Kernel.php +++ b/src/WpStarter/Wordpress/Kernel.php @@ -8,21 +8,17 @@ use WpStarter\Routing\Pipeline; use WpStarter\Routing\Router; use WpStarter\Support\Facades\Facade; +use WpStarter\Wordpress\Bootstrap\HasEarlyBootstrapers; use WpStarter\Wordpress\Routing\Router as ShortcodeRouter; class Kernel extends HttpKernel { + use HasEarlyBootstrapers; protected $wpHandleHook=['template_redirect',1]; /** * @var \WpStarter\Wordpress\Application */ protected $app; - protected $earlyBootstrapers = [ - \WpStarter\Foundation\Bootstrap\LoadEnvironmentVariables::class, - \WpStarter\Foundation\Bootstrap\LoadConfiguration::class, - \WpStarter\Wordpress\Bootstrap\HandleExceptions::class, - \WpStarter\Foundation\Bootstrap\RegisterFacades::class, - ]; /** * The bootstrap classes for the application. * @@ -164,12 +160,4 @@ protected function syncMiddlewareToRouter() } } - - - function earlyBootstrap() - { - foreach ($this->earlyBootstrapers as $bootstraper) { - $this->app->bootstrapOne($bootstraper); - } - } } diff --git a/src/WpStarter/Wordpress/Setting/SettingServiceProvider.php b/src/WpStarter/Wordpress/Setting/SettingServiceProvider.php index 5230b12..d98756c 100644 --- a/src/WpStarter/Wordpress/Setting/SettingServiceProvider.php +++ b/src/WpStarter/Wordpress/Setting/SettingServiceProvider.php @@ -19,15 +19,21 @@ function register() public function boot(){ if($this->autoRestartQueue) { - add_action('update_option_' . $this->getOptionKey(), function () { - $this->app['setting']->reload(); - Artisan::call('queue:restart'); - }); + add_action('update_option_' . $this->getOptionKey(), [$this,'reloadSettings']); } if($this->autoSave) { - add_action('shutdown', function () { - $this->app['setting']->save(); - }); + add_action('shutdown', [$this,'saveSettings']); + } + } + public function saveSettings(){ + if($this->app->bound('setting')) { + $this->app['setting']->save(); + } + } + public function reloadSettings(){ + if($this->app->bound('setting')) { + $this->app['setting']->reload(); + Artisan::call('queue:restart'); } } From 8833336520142a1b3091f016329dffab8f9e0953 Mon Sep 17 00:00:00 2001 From: alt Date: Wed, 19 Mar 2025 07:36:11 +0700 Subject: [PATCH 2/2] Better early bootstrap --- .../{HasEarlyBootstrapers.php => HasEarlyBootstrappers.php} | 2 +- src/WpStarter/Wordpress/Console/Kernel.php | 4 ++-- src/WpStarter/Wordpress/Kernel.php | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) rename src/WpStarter/Wordpress/Bootstrap/{HasEarlyBootstrapers.php => HasEarlyBootstrappers.php} (97%) diff --git a/src/WpStarter/Wordpress/Bootstrap/HasEarlyBootstrapers.php b/src/WpStarter/Wordpress/Bootstrap/HasEarlyBootstrappers.php similarity index 97% rename from src/WpStarter/Wordpress/Bootstrap/HasEarlyBootstrapers.php rename to src/WpStarter/Wordpress/Bootstrap/HasEarlyBootstrappers.php index 36e08f3..ef6d145 100644 --- a/src/WpStarter/Wordpress/Bootstrap/HasEarlyBootstrapers.php +++ b/src/WpStarter/Wordpress/Bootstrap/HasEarlyBootstrappers.php @@ -2,7 +2,7 @@ namespace WpStarter\Wordpress\Bootstrap; -trait HasEarlyBootstrapers +trait HasEarlyBootstrappers { protected $earlyBootstrappers = [ \WpStarter\Foundation\Bootstrap\LoadEnvironmentVariables::class, diff --git a/src/WpStarter/Wordpress/Console/Kernel.php b/src/WpStarter/Wordpress/Console/Kernel.php index 1bfd529..8f81f8e 100644 --- a/src/WpStarter/Wordpress/Console/Kernel.php +++ b/src/WpStarter/Wordpress/Console/Kernel.php @@ -3,11 +3,11 @@ namespace WpStarter\Wordpress\Console; use WpStarter\Foundation\Console\Kernel as ConsoleKernel; -use WpStarter\Wordpress\Bootstrap\HasEarlyBootstrapers; +use WpStarter\Wordpress\Bootstrap\HasEarlyBootstrappers; class Kernel extends ConsoleKernel { - use HasEarlyBootstrapers; + use HasEarlyBootstrappers; /** * The bootstrap classes for the application. * diff --git a/src/WpStarter/Wordpress/Kernel.php b/src/WpStarter/Wordpress/Kernel.php index 9d16dbc..7531bd3 100644 --- a/src/WpStarter/Wordpress/Kernel.php +++ b/src/WpStarter/Wordpress/Kernel.php @@ -8,12 +8,12 @@ use WpStarter\Routing\Pipeline; use WpStarter\Routing\Router; use WpStarter\Support\Facades\Facade; -use WpStarter\Wordpress\Bootstrap\HasEarlyBootstrapers; +use WpStarter\Wordpress\Bootstrap\HasEarlyBootstrappers; use WpStarter\Wordpress\Routing\Router as ShortcodeRouter; class Kernel extends HttpKernel { - use HasEarlyBootstrapers; + use HasEarlyBootstrappers; protected $wpHandleHook=['template_redirect',1]; /** * @var \WpStarter\Wordpress\Application