Skip to content

Commit 399da8b

Browse files
authored
Merge pull request #117 from wpstarter/dev
Better bootstrapers
2 parents cf51f11 + 8833336 commit 399da8b

5 files changed

Lines changed: 78 additions & 50 deletions

File tree

src/WpStarter/Wordpress/Application.php

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,13 @@ class Application extends \WpStarter\Foundation\Application
1111
*
1212
* @var string
1313
*/
14-
const VERSION = '1.9.9';
15-
16-
protected $bootstrappedList = [];
14+
const VERSION = '1.9.10';
15+
/**
16+
* Indicates if the application has been early bootstrapped before.
17+
*
18+
* @var bool
19+
*/
20+
protected $hasBeenEarlyBootstrapped = false;
1721

1822
protected function registerBaseServiceProviders()
1923
{
@@ -30,22 +34,30 @@ public function registerCoreContainerAliases()
3034
$this->alias('app',self::class);
3135
}
3236

33-
function bootstrapWith(array $bootstrappers)
37+
/**
38+
* Run the given array of bootstrap classes.
39+
*
40+
* @param string[] $bootstrappers
41+
* @return void
42+
*/
43+
public function earlyBootstrapWith(array $bootstrappers)
3444
{
35-
$this->hasBeenBootstrapped = true;
36-
45+
$this->hasBeenEarlyBootstrapped = true;
3746
foreach ($bootstrappers as $bootstrapper) {
38-
$this->bootstrapOne($bootstrapper);
39-
}
40-
}
47+
$this['events']->dispatch('bootstrapping: '.$bootstrapper, [$this]);
4148

42-
function bootstrapOne($bootstrapper)
43-
{
44-
if (!isset($this->bootstrappedList[$bootstrapper])) {
45-
$this->bootstrappedList[$bootstrapper] = true;
46-
$this['events']->dispatch('bootstrapping: ' . $bootstrapper, [$this]);
4749
$this->make($bootstrapper)->bootstrap($this);
48-
$this['events']->dispatch('bootstrapped: ' . $bootstrapper, [$this]);
50+
51+
$this['events']->dispatch('bootstrapped: '.$bootstrapper, [$this]);
4952
}
5053
}
54+
/**
55+
* Determine if the application has been early bootstrapped before.
56+
*
57+
* @return bool
58+
*/
59+
public function hasBeenEarlyBootstrapped()
60+
{
61+
return $this->hasBeenEarlyBootstrapped;
62+
}
5163
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace WpStarter\Wordpress\Bootstrap;
4+
5+
trait HasEarlyBootstrappers
6+
{
7+
protected $earlyBootstrappers = [
8+
\WpStarter\Foundation\Bootstrap\LoadEnvironmentVariables::class,
9+
\WpStarter\Foundation\Bootstrap\LoadConfiguration::class,
10+
\WpStarter\Wordpress\Bootstrap\HandleExceptions::class,
11+
\WpStarter\Foundation\Bootstrap\RegisterFacades::class,
12+
];
13+
14+
function earlyBootstrap()
15+
{
16+
if(!$this->app->hasBeenEarlyBootstrapped()) {
17+
$this->app->earlyBootstrapWith($this->earlyBootstrappers());
18+
}
19+
}
20+
protected function earlyBootstrappers()
21+
{
22+
return $this->earlyBootstrappers;
23+
}
24+
protected function bootstrappers()
25+
{
26+
$bootstrappers = parent::bootstrappers();
27+
if($this->app->hasBeenEarlyBootstrapped()) {
28+
//Remove early bootstrapper from bootstrapper list
29+
$bootstrappers=array_diff($bootstrappers, $this->earlyBootstrappers);
30+
}
31+
return $bootstrappers;
32+
}
33+
}

src/WpStarter/Wordpress/Console/Kernel.php

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,11 @@
33
namespace WpStarter\Wordpress\Console;
44

55
use WpStarter\Foundation\Console\Kernel as ConsoleKernel;
6+
use WpStarter\Wordpress\Bootstrap\HasEarlyBootstrappers;
67

78
class Kernel extends ConsoleKernel
89
{
9-
protected $earlyBootstrapers = [
10-
\WpStarter\Foundation\Bootstrap\LoadEnvironmentVariables::class,
11-
\WpStarter\Foundation\Bootstrap\LoadConfiguration::class,
12-
\WpStarter\Wordpress\Bootstrap\HandleExceptions::class,
13-
\WpStarter\Foundation\Bootstrap\RegisterFacades::class,
14-
];
10+
use HasEarlyBootstrappers;
1511
/**
1612
* The bootstrap classes for the application.
1713
*
@@ -26,11 +22,4 @@ class Kernel extends ConsoleKernel
2622
\WpStarter\Foundation\Bootstrap\RegisterProviders::class,
2723
\WpStarter\Foundation\Bootstrap\BootProviders::class,
2824
];
29-
30-
function earlyBootstrap()
31-
{
32-
foreach ($this->earlyBootstrapers as $bootstraper) {
33-
$this->app->bootstrapOne($bootstraper);
34-
}
35-
}
36-
}
25+
}

src/WpStarter/Wordpress/Kernel.php

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,17 @@
88
use WpStarter\Routing\Pipeline;
99
use WpStarter\Routing\Router;
1010
use WpStarter\Support\Facades\Facade;
11+
use WpStarter\Wordpress\Bootstrap\HasEarlyBootstrappers;
1112
use WpStarter\Wordpress\Routing\Router as ShortcodeRouter;
1213

1314
class Kernel extends HttpKernel
1415
{
16+
use HasEarlyBootstrappers;
1517
protected $wpHandleHook=['template_redirect',1];
1618
/**
1719
* @var \WpStarter\Wordpress\Application
1820
*/
1921
protected $app;
20-
protected $earlyBootstrapers = [
21-
\WpStarter\Foundation\Bootstrap\LoadEnvironmentVariables::class,
22-
\WpStarter\Foundation\Bootstrap\LoadConfiguration::class,
23-
\WpStarter\Wordpress\Bootstrap\HandleExceptions::class,
24-
\WpStarter\Foundation\Bootstrap\RegisterFacades::class,
25-
];
2622
/**
2723
* The bootstrap classes for the application.
2824
*
@@ -164,12 +160,4 @@ protected function syncMiddlewareToRouter()
164160
}
165161

166162
}
167-
168-
169-
function earlyBootstrap()
170-
{
171-
foreach ($this->earlyBootstrapers as $bootstraper) {
172-
$this->app->bootstrapOne($bootstraper);
173-
}
174-
}
175163
}

src/WpStarter/Wordpress/Setting/SettingServiceProvider.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,21 @@ function register()
1919

2020
public function boot(){
2121
if($this->autoRestartQueue) {
22-
add_action('update_option_' . $this->getOptionKey(), function () {
23-
$this->app['setting']->reload();
24-
Artisan::call('queue:restart');
25-
});
22+
add_action('update_option_' . $this->getOptionKey(), [$this,'reloadSettings']);
2623
}
2724
if($this->autoSave) {
28-
add_action('shutdown', function () {
29-
$this->app['setting']->save();
30-
});
25+
add_action('shutdown', [$this,'saveSettings']);
26+
}
27+
}
28+
public function saveSettings(){
29+
if($this->app->bound('setting')) {
30+
$this->app['setting']->save();
31+
}
32+
}
33+
public function reloadSettings(){
34+
if($this->app->bound('setting')) {
35+
$this->app['setting']->reload();
36+
Artisan::call('queue:restart');
3137
}
3238
}
3339

0 commit comments

Comments
 (0)