Skip to content

Commit

Permalink
Create a build-in PHP server with --server-host and --server-root
Browse files Browse the repository at this point in the history
  • Loading branch information
nohponex committed Feb 21, 2016
1 parent b9f2527 commit 5449ddd
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 12 deletions.
10 changes: 10 additions & 0 deletions public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>Hello world</h1>
</body>
</html>
42 changes: 30 additions & 12 deletions src/Binary.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ class Binary
*/
protected $arguments;

protected $server = null;

/**
* Get argument specifications
* @return OptionCollection
Expand Down Expand Up @@ -70,6 +72,9 @@ public static function getArgumentSpecifications()
$specs->add('no-colors', 'No colors')->defaultValue(false);
$specs->add('i|immediate', 'Show error output immediately as it appears')->defaultValue(false);

$specs->add('server-host?', 'Server host')->defaultValue(null);
$specs->add('server-root', 'Server root path, default is ./public')->defaultValue('./public');

return $specs;
}

Expand Down Expand Up @@ -102,10 +107,20 @@ public function __construct($argv)
*/
public function invoke()
{
echo 'testphase v' . Testphase::getVersion() . PHP_EOL;

$arguments = $this->arguments;

if (($serverHost = $arguments->{'server-host'}) !== null) {
$this->server = new Server($serverHost, $arguments->{'server-root'});
$this->server->start();
}

//Include bootstrap file if set
if (($bootstrapFile = $arguments->bootstrap)) {
require $bootstrapFile;
}

echo 'testphase v' . Testphase::getVersion() . PHP_EOL;

if ($arguments->help) {
echo 'Help:' . PHP_EOL;
$printer = new ConsoleOptionPrinter;
Expand All @@ -119,16 +134,11 @@ public function invoke()
}
}

//Include bootstrap file if set
if (($bootstrapFile = $arguments->bootstrap)) {
require $bootstrapFile;
}

try {
$testParserCollection = $this->getTestParserCollection();
} catch (\Exception $e) {
echo $e->getMessage();
return 1;
return $this->stop(1);
}

//Statistics object
Expand Down Expand Up @@ -216,7 +226,7 @@ public function invoke()
PHP_EOL,
$e->getMessage()
) . PHP_EOL;
return 1;
return $this->stop(1);
}

$testphaseCollection = $test->getTest();
Expand Down Expand Up @@ -394,13 +404,21 @@ public function invoke()
echo 'Elapsed time: ' . (time() - $_SERVER['REQUEST_TIME']) . ' s' . PHP_EOL;

if ($stats->error > 0) {
return 1;
return $this->stop(1);
}
if ($stats->failure > 0) {
return 2;
return $this->stop(2);
}

return $this->stop(0);
}

protected function stop($returnCode) {
if ($this->server !== null) {
$this->server->stop();
}

return 0;
return $returnCode;
}

/**
Expand Down
84 changes: 84 additions & 0 deletions src/Server.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php
/**
* Copyright 2015 - 2016 Xenofon Spafaridis
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Phramework\Testphase;


/**
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache-2.0
* @author Xenofon Spafaridis <nohponex@gmail.com>
*/
class Server
{
/**
* @var string
*/
protected $pid;
/**
* @var string
*/
protected $host;
/**
* @var string
*/
protected $root;

/**
* Server constructor.
* @param string $host
* @param string $root
*/
public function __construct($host, $root = './public')
{
$this->host = $host;
$this->root = $root;
}

/**
* Start server
*/
public function start()
{
$command = sprintf(
'php -S %s -t %s',
$this->host,
$this->root
);

echo sprintf(
'Start server %s at root %s...',
$this->host,
$this->root
) . PHP_EOL;

$this->pid = exec("$command > /dev/null 2>&1 & echo $!; ", $output);
}

/**
* Stop server
*/
public function stop()
{
echo 'Stop server...' . PHP_EOL;

$command = sprintf(
'kill -9 %s',
$this->pid
);

exec($command);
}
}

0 comments on commit 5449ddd

Please sign in to comment.