Skip to content

Commit 526e7b8

Browse files
authored
Upgrade Hyperf to 3.0 (#45)
1 parent e1fb622 commit 526e7b8

13 files changed

+110
-94
lines changed

.github/workflows/test.yml

+11-4
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,14 @@ jobs:
1414
strategy:
1515
matrix:
1616
os: [ ubuntu-latest ]
17-
php-version: [ '7.2', '7.3', '7.4', '8.0' ]
17+
php-version: [ '8.0', '8.1' ]
1818
engine: [ 'swoole' ]
19-
swoole-version: [ 'v4.5.11', 'v4.6.7', 'v4.7.0', 'v4.8.10' ]
19+
swoole-version: [ 'v4.6.7', 'v4.7.1', 'v4.8.10', 'master' ]
20+
exclude:
21+
- php-version: '8.1'
22+
swoole-version: 'v4.6.7'
23+
- php-version: '8.1'
24+
swoole-version: 'v4.7.1'
2025
max-parallel: 12
2126
steps:
2227
- name: Checkout
@@ -26,7 +31,7 @@ jobs:
2631
with:
2732
php-version: ${{ matrix.php-version }}
2833
tools: phpize
29-
ini-values: opcache.enable_cli=1, swoole.use_shortname='Off'
34+
ini-values: opcache.enable_cli=0, swoole.use_shortname='Off'
3035
coverage: none
3136
- name: Setup Swoole
3237
if: ${{ matrix.engine == 'swoole' }}
@@ -70,7 +75,9 @@ jobs:
7075
run: |
7176
php example/index.php start
7277
sleep 5
73-
- name: Run Test Cases
78+
- name: Run Analyse
7479
run: |
7580
composer analyse src
81+
- name: Run Test Cases
82+
run: |
7683
composer test

composer.json

+16-15
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,23 @@
1919
}
2020
},
2121
"require": {
22-
"php": ">=7.2",
22+
"php": ">=8.0",
2323
"ext-swoole": ">=4.5",
24-
"hyperf/command": "^2.0.0",
25-
"hyperf/config": "^2.0.0",
26-
"hyperf/di": "^2.0.0",
27-
"hyperf/framework": "^2.0.0",
28-
"hyperf/http-server": "^2.0.0",
29-
"hyperf/utils": "^2.0.0"
24+
"hyperf/command": "^3.0.0",
25+
"hyperf/config": "^3.0.0",
26+
"hyperf/di": "^3.0.0",
27+
"hyperf/framework": "^3.0.0",
28+
"hyperf/http-server": "^3.0.0",
29+
"hyperf/utils": "^3.0.0"
3030
},
3131
"require-dev": {
3232
"friendsofphp/php-cs-fixer": "^3.0",
33-
"hyperf/crontab": "^2.0.0",
34-
"hyperf/db": "^2.0.0",
35-
"hyperf/guzzle": "^2.0.0",
36-
"hyperf/process": "^2.0.0",
37-
"hyperf/testing": "^2.0.0",
38-
"phpstan/phpstan": "^0.12",
33+
"hyperf/crontab": "^3.0.0",
34+
"hyperf/db": "^3.0.0",
35+
"hyperf/guzzle": "^3.0.0",
36+
"hyperf/process": "^3.0.0",
37+
"hyperf/testing": "^3.0.0",
38+
"phpstan/phpstan": "^1.0",
3939
"phpunit/phpunit": ">=7.0",
4040
"swoole/ide-helper": "^4.5.2",
4141
"symfony/finder": "^4.0|^5.0"
@@ -44,17 +44,18 @@
4444
"hyperf/crontab": "Required to use closure crontab",
4545
"hyperf/process": "Required to use closure process"
4646
},
47+
"minimum-stability": "dev",
4748
"config": {
4849
"sort-packages": true
4950
},
5051
"scripts": {
5152
"test": "co-phpunit -c phpunit.xml --colors=always",
52-
"analyse": "phpstan analyse --memory-limit 4096M -l 5 -c phpstan.neon",
53+
"analyse": "phpstan analyse --memory-limit=-1 -l 5 -c phpstan.neon",
5354
"cs-fix": "php-cs-fixer fix $1"
5455
},
5556
"extra": {
5657
"branch-alias": {
57-
"dev-master": "1.0-dev"
58+
"dev-master": "2.0-dev"
5859
},
5960
"hyperf": {
6061
"config": "Hyperf\\Nano\\ConfigProvider"

phpstan.neon

+6
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@
44
# vendor/bin/phpstan analyse app --memory-limit 200M -l 0
55
#
66
parameters:
7+
bootstrapFiles:
8+
- "vendor/autoload.php"
9+
inferPrivatePropertyTypeFromConstructor: true
10+
treatPhpDocTypesAsCertain: true
711
reportUnmatchedIgnoredErrors: false
812
ignoreErrors:
913
- '#Static call to instance method Hyperf\\HttpServer\\Router\\Router::[a-zA-Z0-9\\_]+\(\)#'
1014
- '#Static call to instance method Hyperf\\DbConnection\\Db::[a-zA-Z0-9\\_]+\(\)#'
15+
- '#Property Hyperf\\Nano\\ContainerProxy::\$(request|response) is never read, only written.#'
16+
- '#Constant BASE_PATH not found.#'

rector.php

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\CodeQuality\Rector\Class_\InlineConstructorDefaultToPropertyRector;
6+
use Rector\Config\RectorConfig;
7+
use Rector\Set\ValueObject\LevelSetList;
8+
9+
return static function (RectorConfig $rectorConfig): void {
10+
$rectorConfig->paths([
11+
__DIR__ . '/src'
12+
]);
13+
14+
// register a single rule
15+
$rectorConfig->rule(InlineConstructorDefaultToPropertyRector::class);
16+
17+
// define sets of rules
18+
$rectorConfig->sets([
19+
LevelSetList::UP_TO_PHP_80
20+
]);
21+
};

src/App.php

+10-17
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,6 @@
3636
*/
3737
class App
3838
{
39-
/**
40-
* @var ContainerInterface
41-
*/
42-
protected $container;
43-
4439
/**
4540
* @var ConfigInterface
4641
*/
@@ -56,11 +51,10 @@ class App
5651
*/
5752
protected $bound;
5853

59-
private $serverName = 'http';
54+
private string $serverName = 'http';
6055

61-
public function __construct(ContainerInterface $container)
56+
public function __construct(protected ContainerInterface $container)
6257
{
63-
$this->container = $container;
6458
$this->config = $this->container->get(ConfigInterface::class);
6559
$this->dispatcherFactory = $this->container->get(DispatcherFactory::class);
6660
$this->bound = $this->container->has(BoundInterface::class)
@@ -108,7 +102,7 @@ public function getContainer(): ContainerInterface
108102
* Add a middleware globally.
109103
* @param callable|MiddlewareInterface|string $middleware
110104
*/
111-
public function addMiddleware($middleware)
105+
public function addMiddleware(callable|MiddlewareInterface|string $middleware)
112106
{
113107
if ($middleware instanceof MiddlewareInterface || is_string($middleware)) {
114108
$this->appendConfig('middlewares.' . $this->serverName, $middleware);
@@ -127,7 +121,7 @@ public function addMiddleware($middleware)
127121
* Add an exception handler globally.
128122
* @param callable|string $exceptionHandler
129123
*/
130-
public function addExceptionHandler($exceptionHandler)
124+
public function addExceptionHandler(callable|string $exceptionHandler)
131125
{
132126
if (is_string($exceptionHandler)) {
133127
$this->appendConfig('exceptions.handler.' . $this->serverName, $exceptionHandler);
@@ -168,15 +162,14 @@ public function addListener(string $event, $listener = null, int $priority = 1)
168162

169163
/**
170164
* Add a route group.
171-
* @param array|string $prefix
172165
*/
173-
public function addGroup($prefix, callable $callback, array $options = [])
166+
public function addGroup(array|string $prefix, callable $callback, array $options = []): void
174167
{
175168
$router = $this->dispatcherFactory->getRouter($this->serverName);
176169
if (isset($options['middleware'])) {
177170
$this->convertClosureToMiddleware($options['middleware']);
178171
}
179-
return $router->addGroup($prefix, $callback, $options);
172+
$router->addGroup($prefix, $callback, $options);
180173
}
181174

182175
/**
@@ -216,7 +209,7 @@ function ($handler) {
216209
* Add a new crontab.
217210
* @param callable|string $crontab
218211
*/
219-
public function addCrontab(string $rule, $crontab): Crontab
212+
public function addCrontab(string $rule, callable|string $crontab): Crontab
220213
{
221214
$this->config->set('crontab.enable', true);
222215
$this->ensureConfigHasValue('processes', CrontabDispatcherProcess::class);
@@ -251,7 +244,7 @@ function ($crontab) {
251244
* Add a new process.
252245
* @param callable|string $process
253246
*/
254-
public function addProcess($process)
247+
public function addProcess(callable|string $process)
255248
{
256249
if (is_string($process)) {
257250
$this->appendConfig('processes', $process);
@@ -275,7 +268,7 @@ public function addProcess($process)
275268
* @param mixed $httpMethod
276269
* @param mixed $handler
277270
*/
278-
public function addRoute($httpMethod, string $route, $handler, array $options = [])
271+
public function addRoute($httpMethod, string $route, $handler, array $options = []): void
279272
{
280273
$router = $this->dispatcherFactory->getRouter($this->serverName);
281274
if (isset($options['middleware'])) {
@@ -284,7 +277,7 @@ public function addRoute($httpMethod, string $route, $handler, array $options =
284277
if ($handler instanceof \Closure) {
285278
$handler = $handler->bindTo($this->bound, $this->bound);
286279
}
287-
return $router->addRoute($httpMethod, $route, $handler, $options);
280+
$router->addRoute($httpMethod, $route, $handler, $options);
288281
}
289282

290283
/**

src/ContainerProxy.php

+9-21
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,12 @@
1717

1818
class ContainerProxy implements BoundInterface, ContainerInterface
1919
{
20-
/**
21-
* @var ContainerInterface
22-
*/
23-
private $container;
20+
private ?RequestInterface $request;
2421

25-
/**
26-
* @var RequestInterface
27-
*/
28-
private $request;
22+
private ?ResponseInterface $response;
2923

30-
/**
31-
* @var ResponseInterface
32-
*/
33-
private $response;
34-
35-
public function __construct(ContainerInterface $container)
24+
public function __construct(private ContainerInterface $container)
3625
{
37-
$this->container = $container;
3826
$this->request = $container->get(RequestInterface::class);
3927
$this->response = $container->get(ResponseInterface::class);
4028
}
@@ -49,9 +37,9 @@ public function get($id)
4937
return $this->container->get($id);
5038
}
5139

52-
public function define(string $name, $definition)
40+
public function define(string $name, $definition): void
5341
{
54-
return $this->container->define($name, $definition);
42+
$this->container->define($name, $definition);
5543
}
5644

5745
public function has($id): bool
@@ -64,14 +52,14 @@ public function make(string $name, array $parameters = [])
6452
return $this->container->make($name, $parameters);
6553
}
6654

67-
public function set(string $name, $entry)
55+
public function set(string $name, $entry): void
6856
{
69-
return $this->container->set($name, $entry);
57+
$this->container->set($name, $entry);
7058
}
7159

72-
public function unbind(string $name)
60+
public function unbind(string $name): void
7361
{
7462
/* @phpstan-ignore-next-line */
75-
return $this->container->unbind($name);
63+
$this->container->unbind($name);
7664
}
7765
}

src/Factory/AppFactory.php

+21
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
*/
1212
namespace Hyperf\Nano\Factory;
1313

14+
use Dotenv\Dotenv;
15+
use Dotenv\Repository\Adapter\PutenvAdapter;
16+
use Dotenv\Repository\RepositoryBuilder;
1417
use Hyperf\Config\Config;
1518
use Hyperf\Config\ProviderConfig;
1619
use Hyperf\Contract\ConfigInterface;
@@ -67,6 +70,11 @@ public static function createApp(array $dependencies = []): App
6770
// Setting ini and flags
6871
self::prepareFlags();
6972

73+
// Load envs
74+
if (file_exists(BASE_PATH . '/.env')) {
75+
self::loadDotenv();
76+
}
77+
7078
// Prepare container
7179
$container = self::prepareContainer($dependencies);
7280

@@ -111,4 +119,17 @@ protected static function prepareFlags(int $hookFlags = SWOOLE_HOOK_ALL)
111119
! defined('BASE_PATH') && define('BASE_PATH', $projectRootPath);
112120
! defined('SWOOLE_HOOK_FLAGS') && define('SWOOLE_HOOK_FLAGS', $hookFlags);
113121
}
122+
123+
/**
124+
* Setup envs.
125+
*/
126+
protected static function loadDotenv(): void
127+
{
128+
$repository = RepositoryBuilder::createWithNoAdapters()
129+
->addAdapter(PutenvAdapter::class)
130+
->immutable()
131+
->make();
132+
133+
Dotenv::create($repository, [BASE_PATH])->load();
134+
}
114135
}

src/Factory/CommandFactory.php

+1-4
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,9 @@ class CommandFactory
1818
public function create(string $name, \Closure $closure): Command
1919
{
2020
return new class($name, $closure) extends Command {
21-
private $closure;
22-
23-
public function __construct(string $name, \Closure $closure)
21+
public function __construct(string $name, private \Closure $closure)
2422
{
2523
parent::__construct($name);
26-
$this->closure = $closure;
2724
}
2825

2926
public function handle()

src/Factory/CronFactory.php

+1-7
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,8 @@
1515

1616
class CronFactory
1717
{
18-
/**
19-
* @var ContainerInterface
20-
*/
21-
private $container;
22-
23-
public function __construct(ContainerInterface $container)
18+
public function __construct(private ContainerInterface $container)
2419
{
25-
$this->container = $container;
2620
}
2721

2822
public function execute($name)

src/Factory/ProcessFactory.php

+1-4
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,9 @@ public function create(\Closure $closure): AbstractProcess
2121
{
2222
$container = ApplicationContext::getContainer();
2323
return new class($container, $closure) extends AbstractProcess {
24-
private $closure;
25-
26-
public function __construct(ContainerInterface $container, \Closure $closure)
24+
public function __construct(ContainerInterface $container, private \Closure $closure)
2725
{
2826
parent::__construct($container);
29-
$this->closure = $closure;
3027
}
3128

3229
public function handle(): void

0 commit comments

Comments
 (0)