Skip to content

Commit 51945b0

Browse files
committed
feat(custom-executable): ability to specify preferred PHP executable file
1 parent 01088a8 commit 51945b0

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

README.md

+15-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ A small library to help run PHP servers easily and quickly.
55
## Installation
66

77
```
8-
composer require ahmard/php-server
8+
composer require ahmard/php-server --dev
99
```
1010

1111
## Usage
@@ -55,3 +55,17 @@ Server::create('127.0.0.1', '9900')
5555
->onRequest(fn() => var_dump('Request Received'))
5656
->start();
5757
```
58+
59+
- Use preferred php version/executable
60+
61+
```php
62+
use PHPServer\BuiltIn\Server;
63+
64+
Server::create('127.0.0.1', '9900')
65+
->setWorkers(2)
66+
->setPHPExecutable('/usr/bin/php8.0')
67+
->onRequest(fn() => var_dump('Request Received'))
68+
->start();
69+
```
70+
71+
Enjoy 😎

src/BuiltIn/Server.php

+14-1
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@ class Server extends AbstractServer
1212
{
1313
protected string|null $documentRoot = null;
1414
protected string|null $routerScript = null;
15+
protected string $PHPExecutable = 'php';
1516
protected Closure|null $requestCallback = null;
1617
protected int|null $workers = null;
1718

1819

1920
public function getCommand(): ServerCommand
2021
{
2122
$config = $this->getInfo();
22-
$command = "php -S {$config->getHost()}:{$config->getPort()}";
23+
$command = "$this->PHPExecutable -S {$config->getHost()}:{$config->getPort()}";
2324

2425
if (null != $this->workers) {
2526
$command = "PHP_CLI_SERVER_WORKERS=$this->workers $command";
@@ -68,4 +69,16 @@ public function setWorkers(int $num): static
6869
$this->workers = $num;
6970
return $this;
7071
}
72+
73+
/**
74+
* Use custom/preferred php version/executable
75+
*
76+
* @param string $path
77+
* @return $this
78+
*/
79+
public function setPHPExecutable(string $path): static
80+
{
81+
$this->PHPExecutable = $path;
82+
return $this;
83+
}
7184
}

test-builtin.php

+1
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@
77
Server::create('0.0.0.0', 9904)
88
->onRequest(fn() => var_dump('Request Received'))
99
->setWorkers(3)
10+
->setPHPExecutable('/usr/bin/php8.0')
1011
->start()
1112
->logOutputToConsole();

0 commit comments

Comments
 (0)