Skip to content

Commit 944efca

Browse files
committed
new way to build commands. add support for --with-watch. use processSpawner
1 parent 3d23644 commit 944efca

File tree

1 file changed

+37
-12
lines changed

1 file changed

+37
-12
lines changed

src/PatternLab/Console/Commands/ServerCommand.php

Lines changed: 37 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@
1313
use \PatternLab\Config;
1414
use \PatternLab\Console;
1515
use \PatternLab\Console\Command;
16+
use \PatternLab\Console\Commands\WatchCommand;
17+
use \PatternLab\Console\ProcessSpawner;
1618
use \PatternLab\Timer;
1719

20+
1821
class ServerCommand extends Command {
1922

2023
public function __construct() {
@@ -27,6 +30,7 @@ public function __construct() {
2730
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>");
2831
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>");
2932
Console::setCommandOption($this->command,"quiet","Turn on quiet mode for the server.","To turn on quiet mode:");
33+
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:");
3034
Console::setCommandSample($this->command,"To provide both a custom hostname and port:","--host <host> --port <port>");
3135

3236
}
@@ -43,29 +47,50 @@ public function run() {
4347
$publicDir = Config::getOption("publicDir");
4448
$coreDir = Config::getOption("coreDir");
4549

46-
$host = Console::findCommandOptionValue("host");
47-
$host = $host ? $host : "localhost";
50+
$host = Console::findCommandOptionValue("host");
51+
$host = $host ? $host : "localhost";
4852

49-
$port = Console::findCommandOptionValue("port");
50-
$host = $port ? $host.":".$port : $host.":8080";
53+
$port = Console::findCommandOptionValue("port");
54+
$host = $port ? $host.":".$port : $host.":8080";
5155

52-
$null = Console::findCommandOption("quiet");
53-
$null = $null ? " >& /dev/null" : "";
56+
$quiet = Console::findCommandOption("quiet");
5457

55-
$php = isset($_SERVER["_"]) ? $_SERVER["_"] : Config::getOption("phpBin");
58+
// set-up the base command
59+
$command = $this->pathPHP." -S ".$host." ".$coreDir."/server/router.php";
60+
$commands = array();
61+
$commands[] = array("command" => $command, "cwd" => $publicDir, "timeout" => null, "idle" => 600);
5662

57-
if (!$php) {
58-
$configPath = Console::getHumanReadablePath(Config::getOption("configPath"));
59-
Console::writeError("please add the option `phpBin` with the path to PHP to <path>".$configPath."</path> before running the server...");
63+
// get the watch command info
64+
if (Console::findCommandOption("with-watch")) {
65+
$watchCommand = new WatchCommand;
66+
$commands[] = array("command" => $watchCommand->build()." --noprocs", "timeout" => null, "idle" => 600);
6067
}
6168

62-
// start-up the server with the router
6369
Console::writeInfo("server started on http://".$host." - use ctrl+c to exit...");
6470

65-
passthru("cd ".$publicDir." && ".$php." -S ".$host." ".$coreDir."/server/router.php".$null);
71+
$processSpawner = new ProcessSpawner;
72+
$processSpawner->spawn($commands, $quiet);
6673

6774
}
6875

6976
}
7077

78+
public function build() {
79+
80+
$command = $this->pathPHP." ".$this->pathConsole." --".$this->command;
81+
82+
$host = Console::findCommandOptionValue("host");
83+
$port = Console::findCommandOptionValue("port");
84+
85+
if ($host) {
86+
$command .= " --host ".$host;
87+
}
88+
if ($port) {
89+
$command .= " --port ".$port;
90+
}
91+
92+
return $command;
93+
94+
}
95+
7196
}

0 commit comments

Comments
 (0)