Skip to content

Commit 80ede13

Browse files
committed
add an new tool class
1 parent df9164c commit 80ede13

File tree

1 file changed

+215
-0
lines changed

1 file changed

+215
-0
lines changed

src/Util/PhpDevServe.php

Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Inhere\Console\Util;
4+
5+
use Toolkit\Cli\Color;
6+
use Toolkit\Sys\Sys;
7+
use function implode;
8+
use function sprintf;
9+
use const PHP_EOL;
10+
11+
/**
12+
* Class PhpDevServe
13+
*
14+
* @package Inhere\Kite\Common
15+
*/
16+
class PhpDevServe
17+
{
18+
/**
19+
* @var string
20+
*/
21+
private $phpBin;
22+
23+
/**
24+
* @var string
25+
*/
26+
private $serveAddr;
27+
28+
/**
29+
* @var string
30+
*/
31+
private $entryFile;
32+
33+
/**
34+
* @var string
35+
*/
36+
private $documentRoot;
37+
38+
/**
39+
* @param string $serveAddr
40+
* @param string $documentRoot
41+
* @param string $entryFile
42+
*
43+
* @return static
44+
*/
45+
public static function new(string $serveAddr, string $documentRoot, string $entryFile = ''): self
46+
{
47+
return new self($serveAddr, $documentRoot, $entryFile);
48+
}
49+
50+
/**
51+
* Class constructor.
52+
*
53+
* @param string $serveAddr
54+
* @param string $documentRoot
55+
* @param string $entryFile
56+
*/
57+
public function __construct(string $serveAddr, string $documentRoot, string $entryFile = '')
58+
{
59+
$this->documentRoot = $documentRoot;
60+
61+
$this->entryFile = $entryFile;
62+
$this->serveAddr = $serveAddr ?: '127.0.0.1:8080';
63+
}
64+
65+
/**
66+
* @param bool $withColorTag
67+
*/
68+
public function showTipsMessage(bool $withColorTag = true): void
69+
{
70+
$msg = $this->getTipsMessage($withColorTag);
71+
72+
if ($withColorTag) {
73+
echo Color::parseTag($msg), PHP_EOL;
74+
} else {
75+
echo $msg, PHP_EOL;
76+
}
77+
}
78+
79+
/**
80+
* @param bool $withColorTag
81+
*
82+
* @return string
83+
*/
84+
public function getTipsMessage(bool $withColorTag = true): string
85+
{
86+
$version = PHP_VERSION;
87+
88+
$addr = $this->serveAddr;
89+
$root = $this->documentRoot;
90+
$stop = 'CTRL + C';
91+
92+
if ($withColorTag) {
93+
$addr = "<info>{$this->serveAddr}</info>";
94+
$root = "<comment>{$this->documentRoot}</comment>";
95+
$stop = "<comment>CTRL + C</comment>";
96+
}
97+
98+
$nodes = [
99+
sprintf("PHP $version Development Server started\nServer listening on http://%s", $addr),
100+
sprintf("Document root is %s\nYou can use %s to stop run.", $root, $stop),
101+
];
102+
103+
return implode("\n", $nodes);
104+
}
105+
106+
public function start(): void
107+
{
108+
// php -S {$serveAddr} -t public public/index.php
109+
$phpBin = $this->phpBin ?: 'php';
110+
$command = "$phpBin -S {$this->serveAddr}";
111+
112+
if ($this->documentRoot) {
113+
$command .= " -t {$this->documentRoot}";
114+
}
115+
116+
if ($entryFile = $this->entryFile) {
117+
$command .= " $entryFile";
118+
}
119+
120+
echo Color::parseTag("<comment>></comment> <info>$command</info>"), PHP_EOL;
121+
Sys::execute($command);
122+
}
123+
124+
/**
125+
* @return string
126+
*/
127+
public function findPhpBinFile(): string
128+
{
129+
$phpBin = 'php';
130+
131+
// TODO use `type php` check and find. return: 'php is /usr/local/bin/php'
132+
[$ok, $ret,] = Sys::run('which php');
133+
if ($ok === 0) {
134+
$phpBin = trim($ret);
135+
}
136+
137+
return $phpBin;
138+
}
139+
140+
/**
141+
* @return string
142+
*/
143+
public function getPhpBin(): string
144+
{
145+
return $this->phpBin;
146+
}
147+
148+
/**
149+
* @param string $phpBin
150+
*
151+
* @return PhpDevServe
152+
*/
153+
public function setPhpBin(string $phpBin): self
154+
{
155+
$this->phpBin = $phpBin;
156+
return $this;
157+
}
158+
159+
/**
160+
* @return string
161+
*/
162+
public function getServeAddr(): string
163+
{
164+
return $this->serveAddr;
165+
}
166+
167+
/**
168+
* @param string $serveAddr
169+
*
170+
* @return PhpDevServe
171+
*/
172+
public function setServeAddr(string $serveAddr): self
173+
{
174+
$this->serveAddr = $serveAddr;
175+
return $this;
176+
}
177+
178+
/**
179+
* @return string
180+
*/
181+
public function getDocumentRoot(): string
182+
{
183+
return $this->documentRoot;
184+
}
185+
186+
/**
187+
* @param string $documentRoot
188+
*
189+
* @return PhpDevServe
190+
*/
191+
public function setDocumentRoot(string $documentRoot): self
192+
{
193+
$this->documentRoot = $documentRoot;
194+
return $this;
195+
}
196+
197+
/**
198+
* @return string
199+
*/
200+
public function getEntryFile(): string
201+
{
202+
return $this->entryFile;
203+
}
204+
205+
/**
206+
* @param string $entryFile
207+
*
208+
* @return PhpDevServe
209+
*/
210+
public function setEntryFile(string $entryFile): self
211+
{
212+
$this->entryFile = $entryFile;
213+
return $this;
214+
}
215+
}

0 commit comments

Comments
 (0)