-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.php
More file actions
executable file
·144 lines (128 loc) · 4.28 KB
/
server.php
File metadata and controls
executable file
·144 lines (128 loc) · 4.28 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
class Server
{
public $os, $config, $web_root_path;
public $port = "8080";
public $php = "8.1";
public $worker = 20;
public $routes = "";
public $debug = false;
public $phpini = true; // false if to use default php.ini or path to php.ini
public $live_site = ""; // live site link, used load live site images to local
public $configPath = "";
function __construct()
{
$this->initalSetup();
// get project root directory path
$this->web_root_path = dirname(__FILE__);
$this->getOs();
$this->getRoutes();
$this->server();
}
public function initalSetup(){
$this->configPath = dirname(__FILE__).'/.server';
if(!is_dir($this->configPath)){
mkdir($this->configPath,0755, true);
}
if($this->phpini != false){
$this->phpini = $this->configPath.'/php.ini';
if(!file_exists($this->phpini)){
file_put_contents($this->phpini,'');
}
}
}
public function getOs(){
if (PHP_OS == "Darwin"): // is windows
$this->os = 'mac';
elseif (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN'):
$this->os = 'win';
else:
$this->os = 'linux';
endif;
}
public function getRoutes(){
$route_content = '<?php
if (php_sapi_name() !== "cli-server") {
die("this is only for the php development server");
}
elseif (preg_match("/\.(?:png|jpg|jpeg|gif)$/", $_SERVER["REQUEST_URI"])) {';
if($this->live_site != ''){
$route_content.='header("Location: '.$this->live_site.'".$_SERVER["REQUEST_URI"], true, 301);
return false;';
}
$route_content.='
}
// if needed, fix also "PATH_INFO" and "PHP_SELF" variables here...
// require the entry point
require "index.php";
?>';
$this->routes = $this->configPath.'/routes.php';
file_put_contents($this->routes,$route_content);
}
public function startWpServer(){
$command = "PHP_CLI_SERVER_WORKERS=".$this->worker;
$command .=" ENV=local";
$php = exec('which php'.$this->php);
$wp = exec('which wp');
if(!$wp){
exec("curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar");
rename('wp-cli.phar',$this->configPath.'/wp-cli.phar');
exec("chmod +x ".$this->configPath.'/wp-cli.phar');
$wp = $this->configPath.'/wp-cli.phar';
}
if($this->debug == true){
$debug = "-dxdebug.mode=debug -dxdebug.start_with_request=yes";
}else{
$debug = "";
}
$command = $command ." ".$php." ".$debug." ".$wp." --port=".$this->port;
$command.=" --color";
$command.=" --debug=bootstrap";
$command.=" --skip-plugins";
$command.=" --skip-themes";
$command.=" server";
// $command.= " server.php";
// exec($command.' server.php');
if($this->phpini != false){
$command.= " --config=".$this->phpini;
}
// $command.= " --docroot= '".__DIR__."'";
exec($command);
}
public function startServer(){
$command = "PHP_CLI_SERVER_WORKERS=".$this->worker;
$command.=" ENV=local";
$php = exec('which php'.$this->php);
$command = $command ." ".$php." -S localhost:".$this->port;
if($this->phpini != false){
$command = $command." -c ".$this->phpini;
}
//exec($command.' '.$this->routes);
exec($command);
}
/***
* @return void
* config file for running in linux
*
*/
public function runServer(){
if(file_exists(__DIR__.'/wp-load.php')){
// THIS IS WP
$this->startWpServer();
}else{
// THIS IS NOT WP
$this->startServer();
}
}
public function server(){
// check if apachectl is installed or not
$output = exec("which php".$this->php);
if($output){ //installed
$this->runServer();
}else{ // not installed
echo "Install php version ".$this->php."\n";
}
exit;
}
}
$server = new Server();