Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
WyattCast44 committed Jul 17, 2022
1 parent c7c58b9 commit e3f497c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 25 deletions.
18 changes: 12 additions & 6 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@ You can configure the several options with the server, such as the host, the por

### Passing configuration via the constructor or Server::new()

You may pass most configuration options via the constructor as shown below where we set the `host`, `port` and `root` options.
You may pass most configuration options via the constructor. For example we are setting the `host`, `port` and `root` options in the code below.

```PHP
use Statix\Server\Server;

Server::new([
'host' => 'localhost',
'port' => 8000,
Expand All @@ -50,7 +52,11 @@ new Server([
'port' => 8000,
'root' => __DIR__ . '/content'
]);
```

The complete list of configuration items that can be passed via the constructor can be found below.

```PHP
$optionsSettableViaContructor = [
'host' => 'string', // default: localhost
'port' => 'string|int', // default: 8000
Expand All @@ -65,8 +71,10 @@ $optionsSettableViaContructor = [

### Setting configuration via the named methods

You also have the option of calling named methods to set the configuration options as shown below.

```PHP
use Statix\Server;
use Statix\Server\Server;

Server::new()
->usePHP('path')
Expand All @@ -75,10 +83,8 @@ Server::new()
->root('./content')
->useRouter('./router.php')
->withoutEnvVars([
//
])->filterEnvVars(function($value, $key) {
//
})
'APP_KEY',
]);
```

### Capturing the output from the server process
Expand Down
38 changes: 19 additions & 19 deletions src/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,29 @@ class Server
{
/**
* The user passed configuration array
*
* @param array $baseConfiguration
*
* @param array $baseConfiguration
*/
protected $baseConfiguration;

/**
* The default and user merged configuration array
*
* @param array $configuration
*
* @param array $configuration
*/
protected $configuration;

/**
* The output handler to pass process output to
*
* @param callable $outputHandler
*
* @param callable $outputHandler
*/
protected $outputHandler;

/**
* The env vars which will be passed to the server process
*
* @param array $envVarsToPass
*
* @param array $envVarsToPass
*/
protected $envVarsToPass;

Expand All @@ -46,11 +46,11 @@ public function __construct(array $configuration = [])
$this->baseConfiguration = $configuration;

$this->configuration = array_merge($defaults = [
'host' => 'localhost',
'port' => 8000,
'root' => getcwd(),
'host' => 'localhost',
'port' => 8000,
'root' => getcwd(),
'executable' => null,
'router' => null,
'router' => null,
'withoutEnvVars' => [],
], $configuration);

Expand Down Expand Up @@ -116,11 +116,11 @@ public function useRouter(string $path): self
public function filterEnvVars(callable $callback): self
{
$this->envVarsToPass = array_filter(
$this->envVarsToPass,
$callback,
$this->envVarsToPass,
$callback,
ARRAY_FILTER_USE_BOTH
);

return $this;
}

Expand All @@ -129,7 +129,7 @@ private function findExecutable(): string
if ($this->configuration['executable'] != null) {
return $this->configuration['executable'];
}

return (new PhpExecutableFinder)->find(false);
}

Expand All @@ -147,7 +147,7 @@ private function buildServeCommand(): array

private function buildPassingEnvVarArray(): array
{
return array_filter($this->envVarsToPass, function($key) {
return array_filter($this->envVarsToPass, function ($key) {
return in_array($key, $this->configuration['withoutEnvVars']);
}, ARRAY_FILTER_USE_KEY);
}
Expand All @@ -163,7 +163,7 @@ private function initProcess(): Process
);

$process->start(function ($type, $buffer) {
if($this->outputHandler != null) {
if ($this->outputHandler != null) {
($this->outputHandler)($buffer);
}
});
Expand All @@ -188,4 +188,4 @@ public function runInBackground(): int|null

return $process->getExitCode();
}
}
}

0 comments on commit e3f497c

Please sign in to comment.