Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

路由注册相关修复 #26

Merged
merged 3 commits into from
Aug 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 99 additions & 14 deletions src/http-message/src/Cookie.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 . '=';
Expand All @@ -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 = [
Expand All @@ -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)) {
Expand All @@ -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;
}
}
10 changes: 5 additions & 5 deletions src/http-server/src/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
2 changes: 1 addition & 1 deletion src/http-server/src/Middlewares/AllowCrossDomain.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
class AllowCrossDomain implements MiddlewareInterface
{
/** @var array 允许域,全部可以使用`*` */
protected array $allowOrigin = [];
protected array $allowOrigin = ['*'];

/** @var array 附加的响应头 */
protected array $addedHeaders = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ protected function sendCookie(Cookie $cookie, Response $response): void
$cookie->getDomain(),
$cookie->isSecure(),
$cookie->isHttponly(),
$cookie->getSamesite()
$cookie->getSameSite()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
Expand Down
23 changes: 13 additions & 10 deletions src/http-server/src/RouteCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/routing/src/Annotations/AutoController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 前缀
Expand Down
3 changes: 2 additions & 1 deletion src/routing/src/Annotations/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 前缀
Expand Down
16 changes: 16 additions & 0 deletions src/routing/src/Contracts/ControllerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

/**
* This file is part of MaxPHP.
*
* @link https://github.com/marxphp
* @license https://github.com/marxphp/max/blob/master/LICENSE
*/

namespace Max\Routing\Contracts;

interface ControllerInterface
{
}
Loading