forked from pattern-lab/patternlab-php-core
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathServerCommand.php
More file actions
96 lines (65 loc) · 2.98 KB
/
ServerCommand.php
File metadata and controls
96 lines (65 loc) · 2.98 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
<?php
/*!
* Console Server Command Class
*
* Copyright (c) 2014 Dave Olsen, http://dmolsen.com
* Licensed under the MIT license
*
*/
namespace PatternLab\Console\Commands;
use \PatternLab\Config;
use \PatternLab\Console;
use \PatternLab\Console\Command;
use \PatternLab\Console\Commands\WatchCommand;
use \PatternLab\Console\ProcessSpawner;
use \PatternLab\Timer;
class ServerCommand extends Command {
public function __construct() {
parent::__construct();
$this->command = "server";
Console::setCommand($this->command,"Start the PHP-based server","The server command will start PHP's web server for you.","s");
Console::setCommandOption($this->command,"host:","Provide a custom hostname. Default value is <path>localhost</path>.","To use a custom hostname and the default port:","","<host>");
Console::setCommandOption($this->command,"port:","Provide a custom port. Default value is <path>8080</path>.","To use a custom port and the default hostname:","","<port>");
Console::setCommandOption($this->command,"quiet","Turn on quiet mode for the server.","To turn on quiet mode:");
Console::setCommandOption($this->command,"with-watch","Start watching ./source when starting the server. Takes the same arguments as --watch.","To turn on with-watch mode:");
Console::setCommandSample($this->command,"To provide both a custom hostname and port:","--host <host> --port <port>");
}
public function run() {
if (version_compare(phpversion(), '5.4.0', '<')) {
Console::writeWarning("you must have PHP 5.4.0 or greater to use this feature. you are using PHP ".phpversion()."...");
} else {
// set-up defaults
$publicDir = Config::getOption("publicDir");
$coreDir = Config::getOption("coreDir");
$host = Console::findCommandOptionValue("host");
$host = $host ? $host : "localhost";
$port = Console::findCommandOptionValue("port");
$host = $port ? $host.":".$port : $host.":8080";
$quiet = Console::findCommandOption("quiet");
// set-up the base command
$command = $this->pathPHP." -S ".$host." ".$coreDir."/server/router.php";
$commands = array();
$commands[] = array("command" => $command, "cwd" => $publicDir, "timeout" => null, "idle" => 1800);
// get the watch command info
if (Console::findCommandOption("with-watch")) {
$watchCommand = new WatchCommand;
$commands[] = array("command" => $watchCommand->build()." --no-procs", "timeout" => null, "idle" => 1800);
}
Console::writeInfo("server started on http://".$host." - use ctrl+c to exit...");
$processSpawner = new ProcessSpawner;
$processSpawner->spawn($commands, $quiet);
}
}
public function build() {
$command = $this->pathPHP." ".$this->pathConsole." --".$this->command;
$host = Console::findCommandOptionValue("host");
$port = Console::findCommandOptionValue("port");
if ($host) {
$command .= " --host ".$host;
}
if ($port) {
$command .= " --port ".$port;
}
return $command;
}
}