Skip to content

Commit

Permalink
Merge pull request #20 from aledeg/add-params-to-server
Browse files Browse the repository at this point in the history
Add parameters to Server object
  • Loading branch information
ddelnano committed May 7, 2016
2 parents 5431561 + 53f99b1 commit 9319578
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 18 deletions.
18 changes: 16 additions & 2 deletions bin/dredd-hooks-php
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,25 @@ if (!$loaded) {
);
}

// Get options from the command line
$options = getopt('', [
'host:',
'port:',
'force',
]);

Hooks::loadHooks($argv);

$server = new Server();
$host = array_key_exists('host', $options)
? $options['host']
: '127.0.0.1';
$port = array_key_exists('port', $options)
? $options['port']
: 61321;

$server = new Server($host, $port);

fprintf(STDOUT, "Starting server\n");
flush();

$server->run();
$server->run(array_key_exists('force', $options));
17 changes: 8 additions & 9 deletions src/Hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,22 +197,21 @@ public static function getCallbacksForName($propertyName, $transaction)
}

/**
* @param $files
* @param $arguments
*/
public static function loadHooks($files)
public static function loadHooks($arguments)
{
$paths = $files;
// iterate through the files passed from stdin
array_walk($paths, function ($file) {
// iterate through the arguments passed from stdin
array_walk($arguments, function ($argument) {

// iterate through the files passed back from expanding globs
$globs = glob($file);
array_walk($globs, function ($path) {
$files = glob($argument);
array_walk($files, function ($file) {

// require all files except dredd-hooks-php
if (basename($path) != 'dredd-hooks-php') {
if (basename($file) != 'dredd-hooks-php') {

require_once $path;
require_once $file;
}
});
});
Expand Down
20 changes: 13 additions & 7 deletions src/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,28 @@
class Server
{

const SOCKET = "tcp://127.0.0.1:61321";

const RECV_LENGTH= 10;

const MESSAGE_END = "\n";

public function __construct()
private $host;
private $port;

public function __construct($host, $port)
{
$this->runner = new Runner;
$this->host = $host;
$this->port = $port;
}

public function run()
public function run($force = false)
{
// $this->killProgramsOnDreddPort();
if ($force) {
$this->killProgramsOnDreddPort();
}

$server = stream_socket_server(self::SOCKET, $errno, $errorMessage);
$socket = sprintf('tcp://%s:%s', $this->host, $this->port);
$server = stream_socket_server($socket, $errno, $errorMessage);

if ($server === false) {

Expand Down Expand Up @@ -117,7 +123,7 @@ private function killProgramsOnDreddPort()
{
foreach ([SIGTERM, SIGINT, SIGHUP, SIGKILL] as $signal) {
// get any programs running on the dredd port
if ($string = shell_exec("lsof -i tcp:61321")) {
if ($string = shell_exec(sprintf("lsof -i tcp:%s", $this->port))) {
$regex = '/(?:php\s*)(\d*)/';

// get matches if any programs are returned
Expand Down

0 comments on commit 9319578

Please sign in to comment.