From 7602012b78b8235b64c78c7e7c1d40a419f24ce3 Mon Sep 17 00:00:00 2001 From: chengyao <987861463@qq.com> Date: Sun, 14 Jan 2024 10:58:37 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=85=E4=BB=85=E6=94=AF=E6=8C=81php8.2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- composer.json | 34 ++++++++++---- src/aop/composer.json | 44 +++++++++---------- src/aop/src/AstManager.php | 5 ++- src/aop/src/PropertyHandlerVisitor.php | 2 +- src/cache/composer.json | 2 +- src/config/composer.json | 2 +- src/database/composer.json | 2 +- src/di/composer.json | 2 +- src/event/composer.json | 2 +- src/http-message/composer.json | 5 ++- src/http-message/src/Message.php | 20 ++++----- src/http-message/src/Stream/FileStream.php | 34 +++++++------- .../src/Stream/StandardStream.php | 20 ++++----- src/http-message/src/UploadedFile.php | 29 ++++++------ src/http-message/src/Uri.php | 17 +++---- src/http-server/composer.json | 2 +- src/routing/composer.json | 2 +- src/session/composer.json | 2 +- src/utils/composer.json | 2 +- src/utils/src/helpers.php | 2 + 20 files changed, 123 insertions(+), 107 deletions(-) diff --git a/composer.json b/composer.json index 83a76dea..55f48455 100644 --- a/composer.json +++ b/composer.json @@ -56,24 +56,42 @@ "next/var-dumper": "*" }, "require": { - "php": "^8.0", - "psr/container": "^1.0|^2.0", + "php": "^8.2", + "next/aop": "*", + "next/cache": "*", + "next/config": "*", + "next/database": "*", + "next/di": "*", + "next/event": "*", + "next/http-message": "*", + "next/http-server": "*", + "next/routing": "*", + "next/session": "*", + "next/var-dumper": "*", + "next/utils": "*", + "psr/container": "^2.0", "psr/simple-cache": "^1.0", "psr/event-dispatcher": "^1.0", - "psr/http-message": "^1.0", + "psr/http-message": "^2.0", "psr/http-server-middleware": "^1.0", "psr/http-server-handler": "^1.0", - "psr/log": "^1.0" + "psr/log": "^3.0", + "symfony/var-dumper": "^7.0", + "voku/portable-ascii": "^2.0", + "workerman/workerman": "^4.1", + "hyperf/exception-handler": "^3.1", + "league/commonmark": "^2.4", + "ramsey/uuid": "^4.7" }, "require-dev": { "friendsofphp/php-cs-fixer": "*", - "phpunit/phpunit": "^9.0", + "phpunit/phpunit": "*", "phpstan/phpstan": "*", "doctrine/inflector": "*" }, "scripts": { - "test": "phpunit --prepend tests/bootstrap.php -c phpunit.xml --colors=always", - "cs-fix": "php-cs-fixer fix $1", - "analyse": "phpstan analyse --memory-limit 300M -l 0 -c phpstan.neon $1" + "test": "@php ./vendor/bin/phpunit --prepend tests/bootstrap.php -c phpunit.xml --colors=always", + "cs-fix": "@php ./vendor/bin/php-cs-fixer fix $1", + "analyse": "@php ./vendor/bin/phpstan analyse --memory-limit 300M -l 0 -c phpstan.neon" } } diff --git a/src/aop/composer.json b/src/aop/composer.json index 5162e83a..412a22d7 100644 --- a/src/aop/composer.json +++ b/src/aop/composer.json @@ -1,25 +1,25 @@ { - "name": "next/aop", - "description": "An AOP package that is resident in in-memory PHP frameworks, such as nextphp, WebMan.", - "homepage": "https://github.com/next-laboratory/aop", - "license": "Apache-2.0", - "autoload": { - "psr-4": { - "Next\\Aop\\": "src/" - } - }, - "authors": [ - { - "name": "chengyao", - "email": "987861463@qq.com" - } - ], - "require": { - "php": "^8.0", - "ext-pcntl": "*", - "next/utils": "dev-master", - "next/di": "dev-master", - "nikic/php-parser": "^4.13", - "symfony/finder": "*" + "name": "next/aop", + "description": "An AOP package that is resident in in-memory PHP frameworks, such as nextphp, WebMan.", + "homepage": "https://github.com/next-laboratory/aop", + "license": "Apache-2.0", + "autoload": { + "psr-4": { + "Next\\Aop\\": "src/" } + }, + "authors": [ + { + "name": "chengyao", + "email": "987861463@qq.com" + } + ], + "require": { + "php": "^8.2", + "ext-pcntl": "*", + "next/utils": "dev-master", + "next/di": "dev-master", + "nikic/php-parser": "^5.0", + "symfony/finder": "*" + } } diff --git a/src/aop/src/AstManager.php b/src/aop/src/AstManager.php index 46774c61..685d9fe7 100644 --- a/src/aop/src/AstManager.php +++ b/src/aop/src/AstManager.php @@ -15,16 +15,17 @@ use PhpParser\Node\Stmt\Namespace_; use PhpParser\Parser; use PhpParser\ParserFactory; +use PhpParser\PhpVersion; class AstManager { protected Parser $parser; - protected array $container = []; + protected array $container = []; public function __construct() { - $this->parser = (new ParserFactory())->create(ParserFactory::ONLY_PHP7); + $this->parser = (new ParserFactory())->createForHostVersion(); } public function getNodes(string $realpath) diff --git a/src/aop/src/PropertyHandlerVisitor.php b/src/aop/src/PropertyHandlerVisitor.php index 01840eec..33987f88 100644 --- a/src/aop/src/PropertyHandlerVisitor.php +++ b/src/aop/src/PropertyHandlerVisitor.php @@ -60,7 +60,7 @@ public function leaveNode(Node $node) if ($reflectionConstructor = $reflectionClass->getConstructor()) { $declaringClass = $reflectionConstructor->getDeclaringClass()->getName(); if ($classPath = Composer::getClassLoader()->findFile($declaringClass)) { - $parser = (new ParserFactory())->create(ParserFactory::PREFER_PHP7); + $parser = (new ParserFactory())->createForHostVersion(); $ast = $parser->parse(file_get_contents($classPath)); foreach ($ast as $stmt) { if ($stmt instanceof Node\Stmt\Namespace_) { diff --git a/src/cache/composer.json b/src/cache/composer.json index 76b2ae9d..3521ec58 100644 --- a/src/cache/composer.json +++ b/src/cache/composer.json @@ -26,7 +26,7 @@ } }, "require": { - "php": "^8.0", + "php": "^8.2", "next/utils": "~0.1", "psr/simple-cache": "^1.0" }, diff --git a/src/config/composer.json b/src/config/composer.json index c2e8526b..3ed60f4b 100644 --- a/src/config/composer.json +++ b/src/config/composer.json @@ -18,7 +18,7 @@ } }, "require": { - "php": "^8.0", + "php": "^8.2", "next/utils": "~0.1" }, "extra": { diff --git a/src/database/composer.json b/src/database/composer.json index e022f974..b6a7d1e9 100644 --- a/src/database/composer.json +++ b/src/database/composer.json @@ -15,7 +15,7 @@ } }, "require": { - "php": "^8.0|^8.1|^8.2", + "php": "^8.2", "ext-pdo": "*" } } diff --git a/src/di/composer.json b/src/di/composer.json index 06e73d16..313bd4e7 100644 --- a/src/di/composer.json +++ b/src/di/composer.json @@ -28,7 +28,7 @@ } }, "require": { - "php": "^8.0|^8.1", + "php": "^8.2", "psr/container": "^1.0|^2.0" }, "extra": { diff --git a/src/event/composer.json b/src/event/composer.json index 34a6a343..fc6318f6 100644 --- a/src/event/composer.json +++ b/src/event/composer.json @@ -24,7 +24,7 @@ } }, "require": { - "php": "^8.0||^8.1||^8.2", + "php": "^8.2", "psr/event-dispatcher": "^1.0" }, "extra": { diff --git a/src/http-message/composer.json b/src/http-message/composer.json index c9301666..dffc7387 100644 --- a/src/http-message/composer.json +++ b/src/http-message/composer.json @@ -17,8 +17,9 @@ } ], "require": { - "php": "^8.0|^8.1|^8.2", - "psr/http-message": "^1.0" + "php": "^8.2", + "ext-fileinfo": "*", + "psr/http-message": "^2.0" }, "extra": { "branch-alias": { diff --git a/src/http-message/src/Message.php b/src/http-message/src/Message.php index f3bd0cf4..32ae175d 100644 --- a/src/http-message/src/Message.php +++ b/src/http-message/src/Message.php @@ -22,7 +22,7 @@ class Message implements MessageInterface protected HeaderBag $headers; - protected ?StreamInterface $body = null; + protected StreamInterface $body; /** * {@inheritDoc} @@ -35,7 +35,7 @@ public function getProtocolVersion(): string /** * {@inheritDoc} */ - public function withProtocolVersion($version) + public function withProtocolVersion($version): MessageInterface { if ($this->protocolVersion === $version) { return $this; @@ -61,7 +61,7 @@ public function getHeaders(): array /** * {@inheritDoc} */ - public function hasHeader($name): ?bool + public function hasHeader($name): bool { return $this->headers->has($name); } @@ -69,7 +69,7 @@ public function hasHeader($name): ?bool /** * {@inheritDoc} */ - public function getHeader($name) + public function getHeader($name): array { return $this->headers->get($name); } @@ -88,7 +88,7 @@ public function getHeaderLine($name): string /** * {@inheritDoc} */ - public function withHeader($name, $value) + public function withHeader($name, $value): MessageInterface { $new = clone $this; $new->headers = clone $this->headers; @@ -104,7 +104,7 @@ public function setHeader($name, $value) /** * {@inheritDoc} */ - public function withAddedHeader($name, $value) + public function withAddedHeader($name, $value): MessageInterface { $new = clone $this; $new->headers = clone $this->headers; @@ -120,7 +120,7 @@ public function setAddedHeader($name, $value) /** * {@inheritDoc} */ - public function withoutHeader($name) + public function withoutHeader($name): MessageInterface { $new = clone $this; $new->headers = clone $this->headers; @@ -132,7 +132,7 @@ public function withoutHeader($name) /** * {@inheritDoc} */ - public function getBody(): ?StreamInterface + public function getBody(): StreamInterface { return $this->body; } @@ -140,7 +140,7 @@ public function getBody(): ?StreamInterface /** * {@inheritDoc} */ - public function withBody(StreamInterface $body) + public function withBody(StreamInterface $body): MessageInterface { $new = clone $this; return $new->setBody($body); @@ -154,6 +154,6 @@ public function setBody(StreamInterface $body) protected function formatBody(string|StreamInterface|null $body) { - $this->body = $body ? StandardStream::create($body) : null; + $this->body = StandardStream::create($body); } } diff --git a/src/http-message/src/Stream/FileStream.php b/src/http-message/src/Stream/FileStream.php index d2ec90c7..530da86f 100644 --- a/src/http-message/src/Stream/FileStream.php +++ b/src/http-message/src/Stream/FileStream.php @@ -29,9 +29,10 @@ class FileStream implements StreamInterface */ public function __construct( SplFileInfo|string $file, - protected int $offset = 0, - protected int $length = 0, - ) { + protected int $offset = 0, + protected int $length = 0, + ) + { if (!$file instanceof SplFileInfo) { $file = new SplFileInfo($file); } @@ -50,9 +51,8 @@ public function __construct( * string casting operations. * * @see http://php.net/manual/en/language.oop5.magic.php#object.tostring - * @return string */ - public function __toString() + public function __toString(): string { try { return $this->getContents(); @@ -69,7 +69,7 @@ public function __destruct() /** * Closes the stream and any underlying resources. */ - public function close() + public function close(): void { if (isset($this->resource)) { if (is_resource($this->resource)) { @@ -94,7 +94,7 @@ public function detach() * * @return null|int returns the size in bytes if known, or null if unknown */ - public function getSize() + public function getSize(): ?int { if (!$this->size) { $this->size = filesize($this->getContents()); @@ -108,7 +108,7 @@ public function getSize() * @return int Position of the file pointer * @throws RuntimeException on error */ - public function tell() + public function tell(): int { throw new BadMethodCallException('Not implemented'); } @@ -118,7 +118,7 @@ public function tell() * * @return bool */ - public function eof() + public function eof(): bool { throw new BadMethodCallException('Not implemented'); } @@ -128,7 +128,7 @@ public function eof() * * @return bool */ - public function isSeekable() + public function isSeekable(): bool { throw new BadMethodCallException('Not implemented'); } @@ -147,7 +147,7 @@ public function isSeekable() * * @throws RuntimeException on failure */ - public function seek($offset, $whence = SEEK_SET) + public function seek($offset, $whence = SEEK_SET): void { throw new BadMethodCallException('Not implemented'); } @@ -161,7 +161,7 @@ public function seek($offset, $whence = SEEK_SET) * @see http://www.php.net/manual/en/function.fseek.php * @see seek() */ - public function rewind() + public function rewind(): void { throw new BadMethodCallException('Not implemented'); } @@ -171,7 +171,7 @@ public function rewind() * * @return bool */ - public function isWritable() + public function isWritable(): bool { return false; } @@ -184,7 +184,7 @@ public function isWritable() * @return int returns the number of bytes written to the stream * @throws RuntimeException on failure */ - public function write($string) + public function write($string): int { throw new BadMethodCallException('Not implemented'); } @@ -194,7 +194,7 @@ public function write($string) * * @return bool */ - public function isReadable() + public function isReadable(): bool { return true; } @@ -210,7 +210,7 @@ public function isReadable() * if no bytes are available * @throws RuntimeException if an error occurs */ - public function read($length) + public function read($length): string { throw new BadMethodCallException('Not implemented'); } @@ -222,7 +222,7 @@ public function read($length) * @throws RuntimeException if unable to read or an error occurs while * reading */ - public function getContents() + public function getContents(): string { if (false === $contents = stream_get_contents($this->getResource(), $this->getLength() ?: null, $this->getOffset() ?: -1)) { throw new RuntimeException('Unable to read stream contents'); diff --git a/src/http-message/src/Stream/StandardStream.php b/src/http-message/src/Stream/StandardStream.php index 05dfc5d7..d5129909 100644 --- a/src/http-message/src/Stream/StandardStream.php +++ b/src/http-message/src/Stream/StandardStream.php @@ -111,9 +111,9 @@ public static function create(mixed $body = ''): StreamInterface return $body; } - if (is_string($body)) { + if (is_null($body) || is_string($body)) { $resource = fopen('php://temp', 'rw+'); - fwrite($resource, $body); + fwrite($resource, (string)$body); rewind($resource); $body = $resource; } @@ -151,7 +151,7 @@ public function close(): void */ public function detach() { - if (! isset($this->stream)) { + if (!isset($this->stream)) { return null; } @@ -172,7 +172,7 @@ public function getSize(): ?int return $this->size; } - if (! isset($this->stream)) { + if (!isset($this->stream)) { return null; } @@ -208,7 +208,7 @@ public function tell(): int */ public function eof(): bool { - return ! $this->stream || feof($this->stream); + return !$this->stream || feof($this->stream); } /** @@ -224,7 +224,7 @@ public function isSeekable(): bool */ public function seek($offset, $whence = SEEK_SET): void { - if (! $this->seekable) { + if (!$this->seekable) { throw new RuntimeException('Stream is not seekable'); } @@ -254,7 +254,7 @@ public function isWritable(): bool */ public function write($string): int { - if (! $this->writable) { + if (!$this->writable) { throw new RuntimeException('Cannot write to a non-writable stream'); } @@ -281,7 +281,7 @@ public function isReadable(): bool */ public function read($length): string { - if (! $this->readable) { + if (!$this->readable) { throw new RuntimeException('Cannot read from non-readable stream'); } @@ -293,7 +293,7 @@ public function read($length): string */ public function getContents(): string { - if (! isset($this->stream)) { + if (!isset($this->stream)) { throw new RuntimeException('Unable to read stream contents'); } @@ -309,7 +309,7 @@ public function getContents(): string */ public function getMetadata($key = null) { - if (! isset($this->stream)) { + if (!isset($this->stream)) { return $key ? null : []; } diff --git a/src/http-message/src/UploadedFile.php b/src/http-message/src/UploadedFile.php index 9d45516e..61253982 100644 --- a/src/http-message/src/UploadedFile.php +++ b/src/http-message/src/UploadedFile.php @@ -16,10 +16,8 @@ use Psr\Http\Message\UploadedFileInterface; use RuntimeException; use SplFileInfo; - -use const UPLOAD_ERR_OK; - use function mime_content_type; +use const UPLOAD_ERR_OK; class UploadedFile extends SplFileInfo implements UploadedFileInterface { @@ -44,12 +42,13 @@ class UploadedFile extends SplFileInfo implements UploadedFileInterface * @param int $error 错误码 */ public function __construct( - protected string $tmpFilename, + protected string $tmpFilename, protected int|false $size = 0, - protected string $clientFilename = '', - protected string $clientMediaType = '', - protected int $error = UPLOAD_ERR_OK, - ) { + protected string $clientFilename = '', + protected string $clientMediaType = '', + protected int $error = UPLOAD_ERR_OK, + ) + { parent::__construct($this->tmpFilename); } @@ -67,7 +66,7 @@ public function getStream(): StreamInterface /** * {@inheritDoc} */ - public function moveTo($targetPath): SplFileInfo + public function moveTo($targetPath): void { if (($error = $this->getError()) > 0) { throw new RuntimeException(static::ERROR_MESSAGES[$error], $error); @@ -75,22 +74,20 @@ public function moveTo($targetPath): SplFileInfo $path = pathinfo($targetPath, PATHINFO_DIRNAME); !is_dir($path) && mkdir($path, 0755, true); if (is_uploaded_file($this->tmpFilename)) { - $moved = move_uploaded_file($this->tmpFilename, $targetPath); + $this->moved = move_uploaded_file($this->tmpFilename, $targetPath); } else { // 兼容WorkerMan - $moved = rename($this->tmpFilename, $targetPath); + $this->moved = rename($this->tmpFilename, $targetPath); } - if ($moved) { - $this->moved = true; - return new SplFileInfo($targetPath); + if (!$this->moved) { + throw new RuntimeException('Failed to upload file. Check directory permission.'); } - throw new RuntimeException('Failed to upload file. Check directory permission.'); } /** * {@inheritDoc} */ - public function getSize(): int|false + public function getSize(): ?int { return $this->size; } diff --git a/src/http-message/src/Uri.php b/src/http-message/src/Uri.php index 546e8ad1..6f710740 100644 --- a/src/http-message/src/Uri.php +++ b/src/http-message/src/Uri.php @@ -30,7 +30,7 @@ class Uri implements UriInterface protected string $host = 'localhost'; - protected int|string $port = 80; + protected ?int $port = 80; protected string $query = ''; @@ -62,7 +62,7 @@ public function __construct(string $uri = '') if (isset($parts['host'])) { $this->host = $parts['host']; } - $this->port = $parts['port'] ?? $this->getDefaultPort(); + $this->port = (int)($parts['port'] ?? $this->getDefaultPort()); if (isset($parts['path'])) { $this->path = '/' . trim($parts['path'], '/'); } @@ -79,10 +79,7 @@ public function __construct(string $uri = '') } } - /** - * @return string - */ - public function __toString() + public function __toString(): string { return sprintf( '%s://%s%s%s%s%s', @@ -130,9 +127,9 @@ public function getHost(): string } /** - * @return int|string|null + * @return int|null */ - public function getPort(): int|string|null + public function getPort(): ?int { return $this->port; } @@ -227,7 +224,7 @@ public function withPath($path): UriInterface /** * {@inheritDoc} */ - public function withQuery($query):UriInterface + public function withQuery($query): UriInterface { if ($query === $this->query) { return $this; @@ -240,7 +237,7 @@ public function withQuery($query):UriInterface /** * {@inheritDoc} */ - public function withFragment($fragment):UriInterface + public function withFragment($fragment): UriInterface { if ($fragment === $this->fragment) { return $this; diff --git a/src/http-server/composer.json b/src/http-server/composer.json index 8e1ebe2d..7c3234ae 100644 --- a/src/http-server/composer.json +++ b/src/http-server/composer.json @@ -14,7 +14,7 @@ } ], "require": { - "php": "^8.0|^8.1|^8.2", + "php": "^8.2", "next/http-message": "~0.1", "next/utils": "~0.1", "next/routing": "~0.1", diff --git a/src/routing/composer.json b/src/routing/composer.json index 8ba5c028..3f267531 100644 --- a/src/routing/composer.json +++ b/src/routing/composer.json @@ -15,7 +15,7 @@ } }, "require": { - "php": "^8.0|^8.1|^8.2", + "php": "^8.2", "next/http-message": "~0.1" }, "extra": { diff --git a/src/session/composer.json b/src/session/composer.json index 582f3628..2abfb6dc 100644 --- a/src/session/composer.json +++ b/src/session/composer.json @@ -14,7 +14,7 @@ } }, "require": { - "php": "^8.0|^8.1|^8.2" + "php": "^8.2" }, "extra": { "branch-alias": { diff --git a/src/utils/composer.json b/src/utils/composer.json index aad499f6..34034145 100644 --- a/src/utils/composer.json +++ b/src/utils/composer.json @@ -23,7 +23,7 @@ } }, "require": { - "php": "^8.0|^8.1|^8.2", + "php": "^8.2", "symfony/finder": "*", "ramsey/uuid": "*", "symfony/mime": "*", diff --git a/src/utils/src/helpers.php b/src/utils/src/helpers.php index be62ff67..860ed8a0 100644 --- a/src/utils/src/helpers.php +++ b/src/utils/src/helpers.php @@ -330,6 +330,8 @@ function optional(mixed $value = null, callable $callback = null): mixed if (! is_null($value)) { return $callback($value); } + + return null; } /**