-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathWordpressStarter.php
102 lines (92 loc) · 2.75 KB
/
WordpressStarter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php
use WpStarter\Http\Request;
final class WordpressStarter
{
/**
* @var \WpStarter\Foundation\Application
*/
protected $app;
/**
* @var WpStarter\Wordpress\Kernel | WpStarter\Wordpress\Console\Kernel
*/
protected $kernel;
static protected $instance;
public static function make()
{
if (!self::$instance) {
self::$instance = new self();
}
return self::$instance;
}
function __construct()
{
$this->loadApp();
}
function loadApp(){
if(!$this->app) {
require __DIR__ . '/bootstrap/autoload.php';
return $this->app = require_once __DIR__ . '/bootstrap/app.php';
}
return $this->app;
}
public function app(){
return $this->app;
}
public function kernel(){
return $this->kernel;
}
function run(){
if ($this->isRunningInConsole()) {
$this->runCli();
} else {
$this->runWeb();
}
if(!did_action('mu_plugin_loaded')) {
add_action('mu_plugin_loaded', [$this->kernel, 'earlyBootstrap'], 1);
}else{
add_action('ws_loaded',[$this->kernel,'earlyBootstrap'],1);
}
add_action('plugins_loaded',[$this->kernel,'bootstrap'],1);
do_action('ws_loaded',$this);
}
protected function isRunningInConsole(){
if(defined('WS_CLI') && WS_CLI){
return true;
}
if(defined('WP_CLI') && WP_CLI){
return true;
}
if (isset($_ENV['APP_RUNNING_IN_CONSOLE'])) {
return $_ENV['APP_RUNNING_IN_CONSOLE'] === 'true';
}
return php_sapi_name() === 'cli' || php_sapi_name() === 'phpdbg';
}
protected function runCli()
{
$this->kernel = $this->app->make(WpStarter\Contracts\Console\Kernel::class);
}
protected function runWeb()
{
$this->checkMaintenance();
$this->kernel = $kernel = $this->app->make(WpStarter\Contracts\Http\Kernel::class);
$request = Request::capture();
$this->app->instance('request', $request);
add_action('init', function ()use($kernel,$request) {
$response = $kernel->handle(
$request
);
}, 1);
}
/**
* Check If The Application Is Under Maintenance
* If the application is in maintenance / demo mode via the "down" command
* we will load this file so that any pre-rendered content can be shown
* instead of starting the framework, which could cause an exception.
* @return void
*/
protected function checkMaintenance(){
if (file_exists($maintenance = __DIR__.'/storage/framework/maintenance.php')) {
require $maintenance;
}
}
}