Hyperf\Utils\Resource will be deprecated in v2.3. (#3636)

* `Hyperf\Utils\Resource` will be deprecated in v2.3, please use `Hyperf\Utils\ResourceGenerator` instead.

* Update composer.json
This commit is contained in:
李铭昕 2021-05-29 23:26:08 +08:00 committed by GitHub
parent 90623860ab
commit 7af798f8fa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 48 additions and 6 deletions

View File

@ -15,6 +15,10 @@
- Changed the default priority of aspect to 0. - Changed the default priority of aspect to 0.
- Changed the consumer tag of amqp to empty string. - Changed the consumer tag of amqp to empty string.
## Deprecated
- `Hyperf\Utils\Resource` will be deprecated in v2.3, please use `Hyperf\Utils\ResourceGenerator` instead.
## Added ## Added
- [#3589](https://github.com/hyperf/hyperf/pull/3589) Added DAG component. - [#3589](https://github.com/hyperf/hyperf/pull/3589) Added DAG component.

View File

@ -16,7 +16,7 @@ use GuzzleHttp\Ring\Exception\RingException;
use GuzzleHttp\Ring\Future\CompletedFutureArray; use GuzzleHttp\Ring\Future\CompletedFutureArray;
use Hyperf\Engine\Http\Client; use Hyperf\Engine\Http\Client;
use Hyperf\Engine\Http\RawResponse; use Hyperf\Engine\Http\RawResponse;
use Hyperf\Utils\Resource; use Hyperf\Utils\ResourceGenerator;
/** /**
* Http handler that uses Swoole Coroutine as a transport layer. * Http handler that uses Swoole Coroutine as a transport layer.
@ -147,7 +147,7 @@ class CoroutineHandler
'effective_url' => $effectiveUrl, 'effective_url' => $effectiveUrl,
'headers' => $response->headers, 'headers' => $response->headers,
'status' => $response->statusCode, 'status' => $response->statusCode,
'body' => Resource::from($response->body), 'body' => ResourceGenerator::from($response->body),
]); ]);
} }
} }

View File

@ -26,7 +26,8 @@
"symfony/var-dumper": "Required to use the dd function (^5.0).", "symfony/var-dumper": "Required to use the dd function (^5.0).",
"symfony/serializer": "Required to use SymfonyNormalizer (^5.0)", "symfony/serializer": "Required to use SymfonyNormalizer (^5.0)",
"symfony/property-access": "Required to use SymfonyNormalizer (^5.0)", "symfony/property-access": "Required to use SymfonyNormalizer (^5.0)",
"hyperf/di": "Required to use ExceptionNormalizer" "hyperf/di": "Required to use ExceptionNormalizer",
"nikic/php-parser": "Required to use PhpParser. (^4.0)"
}, },
"autoload": { "autoload": {
"files": [ "files": [

View File

@ -11,6 +11,9 @@ declare(strict_types=1);
*/ */
namespace Hyperf\Utils; namespace Hyperf\Utils;
/**
* @deprecated v2.3, please use ResourceGenerator instead.
*/
class Resource class Resource
{ {
/** /**

View File

@ -0,0 +1,34 @@
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace Hyperf\Utils;
class ResourceGenerator
{
/**
* TODO: Swoole file hook does not support `php://temp` and `php://memory`.
*/
public static function from(string $body, string $filename = 'php://temp')
{
$resource = fopen($filename, 'r+');
if ($body !== '') {
fwrite($resource, $body);
fseek($resource, 0);
}
return $resource;
}
public static function fromMemory(string $body)
{
return static::from($body, 'php://memory');
}
}

View File

@ -11,7 +11,7 @@ declare(strict_types=1);
*/ */
namespace HyperfTest\Utils; namespace HyperfTest\Utils;
use Hyperf\Utils\Resource; use Hyperf\Utils\ResourceGenerator;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
/** /**
@ -23,7 +23,7 @@ class ResourceTest extends TestCase
public function testFrom() public function testFrom()
{ {
$data = '123123'; $data = '123123';
$resource = Resource::from($data); $resource = ResourceGenerator::from($data);
$this->assertSame('1', fread($resource, 1)); $this->assertSame('1', fread($resource, 1));
$this->assertSame('23', fread($resource, 2)); $this->assertSame('23', fread($resource, 2));
$this->assertSame('123', fread($resource, 10)); $this->assertSame('123', fread($resource, 10));
@ -34,7 +34,7 @@ class ResourceTest extends TestCase
$data = str_repeat('1', 1024 * 1024); $data = str_repeat('1', 1024 * 1024);
$memory = memory_get_usage(true); $memory = memory_get_usage(true);
for ($i = 0; $i < 100; ++$i) { for ($i = 0; $i < 100; ++$i) {
Resource::fromMemory($data); ResourceGenerator::fromMemory($data);
$current = memory_get_usage(true); $current = memory_get_usage(true);
$leak = $current - $memory; $leak = $current - $memory;
$memory = $current; $memory = $current;