From d7d05b66a9be0540e14a942e6096c67741cf572d Mon Sep 17 00:00:00 2001 From: chengyao <64066545+topyao@users.noreply.github.com> Date: Mon, 8 Aug 2022 09:42:19 +0800 Subject: [PATCH 1/3] =?UTF-8?q?Fix:=20=E4=BF=AE=E5=A4=8D=E5=8F=82=E6=95=B0?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/watcher/src/Watcher.php | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/watcher/src/Watcher.php b/src/watcher/src/Watcher.php index 20e84971..96cca442 100644 --- a/src/watcher/src/Watcher.php +++ b/src/watcher/src/Watcher.php @@ -15,7 +15,10 @@ class Watcher { - public function watch(array $dirs, string $pattern, callable $callback, float $interval = 1) + /** + * @param int $interval 微秒 + */ + public function watch(array $dirs, string $pattern, callable $callback, int $interval = 1000000): void { $original = []; $files = Finder::create()->in($dirs)->name($pattern)->files(); @@ -26,14 +29,14 @@ public function watch(array $dirs, string $pattern, callable $callback, float $i echo 'Watching changed files.' . PHP_EOL; while (true) { - sleep($interval); + usleep($interval); clearstatcache(); $modified = []; $files = Finder::create()->in($dirs)->name($pattern)->files(); foreach ($files as $file) { $realPath = $file->getRealPath(); $fileMTime = $file->getMTime(); - if (! isset($original[$realPath])) { + if (!isset($original[$realPath])) { $original[$realPath] = $fileMTime; $modified[] = $realPath; } else if ($original[$realPath] != $fileMTime) { @@ -41,7 +44,7 @@ public function watch(array $dirs, string $pattern, callable $callback, float $i $modified[] = $realPath; } } - if (! empty($modified)) { + if (!empty($modified)) { $callback($modified); } } From df6b14af23fd00c24d7b4a40a223f694a9de7117 Mon Sep 17 00:00:00 2001 From: chengyao <64066545+topyao@users.noreply.github.com> Date: Mon, 8 Aug 2022 13:28:49 +0800 Subject: [PATCH 2/3] =?UTF-8?q?Fix:=20=E8=B7=AF=E7=94=B1=E6=B3=A8=E5=86=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/http-server/src/Kernel.php | 10 ++++---- .../src/Middlewares/AllowCrossDomain.php | 2 +- .../ResponseEmitter/SwooleResponseEmitter.php | 2 +- .../WorkerManResponseEmitter.php | 2 +- src/http-server/src/RouteCollector.php | 23 +++++++++++-------- .../src/Annotations/AutoController.php | 3 ++- src/routing/src/Annotations/Controller.php | 3 ++- .../src/Contracts/ControllerInterface.php | 16 +++++++++++++ 8 files changed, 41 insertions(+), 20 deletions(-) create mode 100644 src/routing/src/Contracts/ControllerInterface.php diff --git a/src/http-server/src/Kernel.php b/src/http-server/src/Kernel.php index c94a4357..d3cad43b 100644 --- a/src/http-server/src/Kernel.php +++ b/src/http-server/src/Kernel.php @@ -57,17 +57,17 @@ public function through(ServerRequestInterface $request): ResponseInterface } /** - * 路由注册. + * @param array|string[] $middlewares */ - protected function map(Router $router): void + public function setMiddlewares(array $middlewares): void { + $this->middlewares = $middlewares; } /** - * @param array|string[] $middlewares + * 路由注册. */ - public function setMiddlewares(array $middlewares): void + protected function map(Router $router): void { - $this->middlewares = $middlewares; } } diff --git a/src/http-server/src/Middlewares/AllowCrossDomain.php b/src/http-server/src/Middlewares/AllowCrossDomain.php index 0637b31a..3e372871 100644 --- a/src/http-server/src/Middlewares/AllowCrossDomain.php +++ b/src/http-server/src/Middlewares/AllowCrossDomain.php @@ -20,7 +20,7 @@ class AllowCrossDomain implements MiddlewareInterface { /** @var array 允许域,全部可以使用`*` */ - protected array $allowOrigin = []; + protected array $allowOrigin = ['*']; /** @var array 附加的响应头 */ protected array $addedHeaders = [ diff --git a/src/http-server/src/ResponseEmitter/SwooleResponseEmitter.php b/src/http-server/src/ResponseEmitter/SwooleResponseEmitter.php index 83923e69..654f8882 100644 --- a/src/http-server/src/ResponseEmitter/SwooleResponseEmitter.php +++ b/src/http-server/src/ResponseEmitter/SwooleResponseEmitter.php @@ -53,7 +53,7 @@ protected function sendCookie(Cookie $cookie, Response $response): void $cookie->getDomain(), $cookie->isSecure(), $cookie->isHttponly(), - $cookie->getSamesite() + $cookie->getSameSite() ); } } diff --git a/src/http-server/src/ResponseEmitter/WorkerManResponseEmitter.php b/src/http-server/src/ResponseEmitter/WorkerManResponseEmitter.php index d0b99503..4686aac8 100644 --- a/src/http-server/src/ResponseEmitter/WorkerManResponseEmitter.php +++ b/src/http-server/src/ResponseEmitter/WorkerManResponseEmitter.php @@ -46,7 +46,7 @@ public function emit(ResponseInterface $psrResponse, $sender = null) $cookie->getDomain(), $cookie->isSecure(), $cookie->isHttponly(), - $cookie->getSamesite() + $cookie->getSameSite() ); } $sender->send($response->withBody((string) $body?->getContents())); diff --git a/src/http-server/src/RouteCollector.php b/src/http-server/src/RouteCollector.php index 8633689f..bf6f0760 100644 --- a/src/http-server/src/RouteCollector.php +++ b/src/http-server/src/RouteCollector.php @@ -17,6 +17,7 @@ use Max\Di\Reflection; use Max\Routing\Annotations\AutoController; use Max\Routing\Annotations\Controller; +use Max\Routing\Contracts\ControllerInterface; use Max\Routing\Contracts\MappingInterface; use Max\Routing\Router; use Max\Utils\Str; @@ -64,16 +65,18 @@ class RouteCollector extends AbstractCollector */ public static function collectClass(string $class, object $attribute): void { - $routeCollector = Context::getContainer()->make(\Max\Routing\RouteCollector::class); - $router = new Router($attribute->prefix, $attribute->patterns, middlewares: $attribute->middlewares, routeCollector: $routeCollector); - if ($attribute instanceof Controller) { - self::$router = $router; - self::$class = $class; - } elseif ($attribute instanceof AutoController) { - foreach (Reflection::class($class)->getMethods() as $reflectionMethod) { - $methodName = $reflectionMethod->getName(); - if (! self::isIgnoredMethod($methodName) && $reflectionMethod->isPublic() && ! $reflectionMethod->isAbstract()) { - $router->request($attribute->prefix . Str::snake($methodName, '-'), [$class, $methodName], $attribute->methods); + if ($attribute instanceof ControllerInterface) { + $routeCollector = Context::getContainer()->make(\Max\Routing\RouteCollector::class); + $router = new Router($attribute->prefix, $attribute->patterns, middlewares: $attribute->middlewares, routeCollector: $routeCollector); + if ($attribute instanceof Controller) { + self::$router = $router; + self::$class = $class; + } elseif ($attribute instanceof AutoController) { + foreach (Reflection::class($class)->getMethods() as $reflectionMethod) { + $methodName = $reflectionMethod->getName(); + if (! self::isIgnoredMethod($methodName) && $reflectionMethod->isPublic() && ! $reflectionMethod->isAbstract()) { + $router->request($attribute->prefix . Str::snake($methodName, '-'), [$class, $methodName], $attribute->methods); + } } } } diff --git a/src/routing/src/Annotations/AutoController.php b/src/routing/src/Annotations/AutoController.php index 9c46f9d9..0b8564e3 100644 --- a/src/routing/src/Annotations/AutoController.php +++ b/src/routing/src/Annotations/AutoController.php @@ -12,9 +12,10 @@ namespace Max\Routing\Annotations; use Attribute; +use Max\Routing\Contracts\ControllerInterface; #[Attribute(Attribute::TARGET_CLASS)] -class AutoController +class AutoController implements ControllerInterface { /** * @param string $prefix 前缀 diff --git a/src/routing/src/Annotations/Controller.php b/src/routing/src/Annotations/Controller.php index 8617fb97..57e225c6 100644 --- a/src/routing/src/Annotations/Controller.php +++ b/src/routing/src/Annotations/Controller.php @@ -12,9 +12,10 @@ namespace Max\Routing\Annotations; use Attribute; +use Max\Routing\Contracts\ControllerInterface; #[Attribute(Attribute::TARGET_CLASS)] -class Controller +class Controller implements ControllerInterface { /** * @param string $prefix 前缀 diff --git a/src/routing/src/Contracts/ControllerInterface.php b/src/routing/src/Contracts/ControllerInterface.php new file mode 100644 index 00000000..488d171a --- /dev/null +++ b/src/routing/src/Contracts/ControllerInterface.php @@ -0,0 +1,16 @@ + Date: Mon, 8 Aug 2022 13:32:58 +0800 Subject: [PATCH 3/3] =?UTF-8?q?Fix:=20=E6=9B=B4=E6=94=B9=E6=96=B9=E6=B3=95?= =?UTF-8?q?=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/http-message/src/Cookie.php | 113 ++++++++++++++++++++++++++++---- 1 file changed, 99 insertions(+), 14 deletions(-) diff --git a/src/http-message/src/Cookie.php b/src/http-message/src/Cookie.php index 9591bd51..a79015e0 100644 --- a/src/http-message/src/Cookie.php +++ b/src/http-message/src/Cookie.php @@ -15,6 +15,16 @@ class Cookie { + /** + * @param string $name + * @param string $value + * @param int $expires + * @param string $path + * @param string $domain + * @param bool $secure + * @param bool $httponly + * @param string $sameSite + */ public function __construct( protected string $name, protected string $value, @@ -23,13 +33,16 @@ public function __construct( protected string $domain = '', protected bool $secure = false, protected bool $httponly = false, - protected string $samesite = '', + protected string $sameSite = '', ) { - if ($this->samesite && ! in_array(strtolower($samesite), ['lax', 'none', 'strict'])) { + if ($this->sameSite && !in_array(strtolower($sameSite), ['lax', 'none', 'strict'])) { throw new InvalidArgumentException('The "sameSite" parameter value is not valid.'); } } + /** + * @return string + */ public function __toString(): string { $str = $this->name . '='; @@ -53,57 +66,105 @@ public function __toString(): string if ($this->httponly) { $str .= '; httponly'; } - if ($this->samesite) { - $str .= '; samesite=' . $this->samesite; + if ($this->sameSite) { + $str .= '; samesite=' . $this->sameSite; } return $str; } + /** + * @param string $name + * + * @return void + */ public function setName(string $name): void { $this->name = $name; } + /** + * @param string $value + * + * @return void + */ public function setValue(string $value): void { $this->value = $value; } + /** + * @param int $expires + * + * @return void + */ public function setExpires(int $expires): void { $this->expires = $expires; } + /** + * @param string $path + * + * @return void + */ public function setPath(string $path): void { $this->path = $path; } + /** + * @param string $domain + * + * @return void + */ public function setDomain(string $domain): void { $this->domain = $domain; } + /** + * @param bool $secure + * + * @return void + */ public function setSecure(bool $secure): void { $this->secure = $secure; } + /** + * @param bool $httponly + * + * @return void + */ public function setHttponly(bool $httponly): void { $this->httponly = $httponly; } - public function setSamesite(?string $samesite): void + /** + * @param string|null $sameSite + * + * @return void + */ + public function setSameSite(?string $sameSite): void { - $this->samesite = $samesite; + $this->sameSite = $sameSite; } + /** + * @return int + */ public function getMaxAge(): int { return $this->expires !== 0 ? $this->expires - time() : 0; } + /** + * @param string $str + * + * @return Cookie + */ public static function parse(string $str): Cookie { $parts = [ @@ -115,16 +176,16 @@ public static function parse(string $str): Cookie 'samesite' => '', ]; foreach (explode(';', $str) as $part) { - if (! str_contains($part, '=')) { + if (!str_contains($part, '=')) { $key = $part; $value = true; } else { [$key, $value] = explode('=', trim($part), 2); - $value = trim($value); + $value = trim($value); } switch ($key = trim(strtolower($key))) { case 'max-age': - $parts['expires'] = time() + (int) $value; + $parts['expires'] = time() + (int)$value; break; default: if (array_key_exists($key, $parts)) { @@ -137,49 +198,73 @@ public static function parse(string $str): Cookie } return new static( $parts['name'], $parts['value'], - (int) $parts['expires'], $parts['path'], - $parts['domain'], (bool) $parts['secure'], - (bool) $parts['httponly'], $parts['samesite'] + (int)$parts['expires'], $parts['path'], + $parts['domain'], (bool)$parts['secure'], + (bool)$parts['httponly'], $parts['samesite'] ); } + /** + * @return string + */ public function getName(): string { return $this->name; } + /** + * @return string + */ public function getValue(): string { return $this->value; } + /** + * @return int + */ public function getExpires(): int { return $this->expires; } + /** + * @return string + */ public function getPath(): string { return $this->path; } + /** + * @return string + */ public function getDomain(): string { return $this->domain; } + /** + * @return bool + */ public function isSecure(): bool { return $this->secure; } + /** + * @return bool + */ public function isHttponly(): bool { return $this->httponly; } - public function getSamesite(): ?string + /** + * @return string|null + */ + public function getSameSite(): ?string { - return $this->samesite; + return $this->sameSite; } }