Skip to content

Commit

Permalink
Upgrade Hyperf to 3.0 (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
huangdijia authored Jul 6, 2022
1 parent e1fb622 commit 526e7b8
Show file tree
Hide file tree
Showing 13 changed files with 110 additions and 94 deletions.
15 changes: 11 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,14 @@ jobs:
strategy:
matrix:
os: [ ubuntu-latest ]
php-version: [ '7.2', '7.3', '7.4', '8.0' ]
php-version: [ '8.0', '8.1' ]
engine: [ 'swoole' ]
swoole-version: [ 'v4.5.11', 'v4.6.7', 'v4.7.0', 'v4.8.10' ]
swoole-version: [ 'v4.6.7', 'v4.7.1', 'v4.8.10', 'master' ]
exclude:
- php-version: '8.1'
swoole-version: 'v4.6.7'
- php-version: '8.1'
swoole-version: 'v4.7.1'
max-parallel: 12
steps:
- name: Checkout
Expand All @@ -26,7 +31,7 @@ jobs:
with:
php-version: ${{ matrix.php-version }}
tools: phpize
ini-values: opcache.enable_cli=1, swoole.use_shortname='Off'
ini-values: opcache.enable_cli=0, swoole.use_shortname='Off'
coverage: none
- name: Setup Swoole
if: ${{ matrix.engine == 'swoole' }}
Expand Down Expand Up @@ -70,7 +75,9 @@ jobs:
run: |
php example/index.php start
sleep 5
- name: Run Test Cases
- name: Run Analyse
run: |
composer analyse src
- name: Run Test Cases
run: |
composer test
31 changes: 16 additions & 15 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,23 @@
}
},
"require": {
"php": ">=7.2",
"php": ">=8.0",
"ext-swoole": ">=4.5",
"hyperf/command": "^2.0.0",
"hyperf/config": "^2.0.0",
"hyperf/di": "^2.0.0",
"hyperf/framework": "^2.0.0",
"hyperf/http-server": "^2.0.0",
"hyperf/utils": "^2.0.0"
"hyperf/command": "^3.0.0",
"hyperf/config": "^3.0.0",
"hyperf/di": "^3.0.0",
"hyperf/framework": "^3.0.0",
"hyperf/http-server": "^3.0.0",
"hyperf/utils": "^3.0.0"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.0",
"hyperf/crontab": "^2.0.0",
"hyperf/db": "^2.0.0",
"hyperf/guzzle": "^2.0.0",
"hyperf/process": "^2.0.0",
"hyperf/testing": "^2.0.0",
"phpstan/phpstan": "^0.12",
"hyperf/crontab": "^3.0.0",
"hyperf/db": "^3.0.0",
"hyperf/guzzle": "^3.0.0",
"hyperf/process": "^3.0.0",
"hyperf/testing": "^3.0.0",
"phpstan/phpstan": "^1.0",
"phpunit/phpunit": ">=7.0",
"swoole/ide-helper": "^4.5.2",
"symfony/finder": "^4.0|^5.0"
Expand All @@ -44,17 +44,18 @@
"hyperf/crontab": "Required to use closure crontab",
"hyperf/process": "Required to use closure process"
},
"minimum-stability": "dev",
"config": {
"sort-packages": true
},
"scripts": {
"test": "co-phpunit -c phpunit.xml --colors=always",
"analyse": "phpstan analyse --memory-limit 4096M -l 5 -c phpstan.neon",
"analyse": "phpstan analyse --memory-limit=-1 -l 5 -c phpstan.neon",
"cs-fix": "php-cs-fixer fix $1"
},
"extra": {
"branch-alias": {
"dev-master": "1.0-dev"
"dev-master": "2.0-dev"
},
"hyperf": {
"config": "Hyperf\\Nano\\ConfigProvider"
Expand Down
6 changes: 6 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@
# vendor/bin/phpstan analyse app --memory-limit 200M -l 0
#
parameters:
bootstrapFiles:
- "vendor/autoload.php"
inferPrivatePropertyTypeFromConstructor: true
treatPhpDocTypesAsCertain: true
reportUnmatchedIgnoredErrors: false
ignoreErrors:
- '#Static call to instance method Hyperf\\HttpServer\\Router\\Router::[a-zA-Z0-9\\_]+\(\)#'
- '#Static call to instance method Hyperf\\DbConnection\\Db::[a-zA-Z0-9\\_]+\(\)#'
- '#Property Hyperf\\Nano\\ContainerProxy::\$(request|response) is never read, only written.#'
- '#Constant BASE_PATH not found.#'
21 changes: 21 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

use Rector\CodeQuality\Rector\Class_\InlineConstructorDefaultToPropertyRector;
use Rector\Config\RectorConfig;
use Rector\Set\ValueObject\LevelSetList;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->paths([
__DIR__ . '/src'
]);

// register a single rule
$rectorConfig->rule(InlineConstructorDefaultToPropertyRector::class);

// define sets of rules
$rectorConfig->sets([
LevelSetList::UP_TO_PHP_80
]);
};
27 changes: 10 additions & 17 deletions src/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,6 @@
*/
class App
{
/**
* @var ContainerInterface
*/
protected $container;

/**
* @var ConfigInterface
*/
Expand All @@ -56,11 +51,10 @@ class App
*/
protected $bound;

private $serverName = 'http';
private string $serverName = 'http';

public function __construct(ContainerInterface $container)
public function __construct(protected ContainerInterface $container)
{
$this->container = $container;
$this->config = $this->container->get(ConfigInterface::class);
$this->dispatcherFactory = $this->container->get(DispatcherFactory::class);
$this->bound = $this->container->has(BoundInterface::class)
Expand Down Expand Up @@ -108,7 +102,7 @@ public function getContainer(): ContainerInterface
* Add a middleware globally.
* @param callable|MiddlewareInterface|string $middleware
*/
public function addMiddleware($middleware)
public function addMiddleware(callable|MiddlewareInterface|string $middleware)
{
if ($middleware instanceof MiddlewareInterface || is_string($middleware)) {
$this->appendConfig('middlewares.' . $this->serverName, $middleware);
Expand All @@ -127,7 +121,7 @@ public function addMiddleware($middleware)
* Add an exception handler globally.
* @param callable|string $exceptionHandler
*/
public function addExceptionHandler($exceptionHandler)
public function addExceptionHandler(callable|string $exceptionHandler)
{
if (is_string($exceptionHandler)) {
$this->appendConfig('exceptions.handler.' . $this->serverName, $exceptionHandler);
Expand Down Expand Up @@ -168,15 +162,14 @@ public function addListener(string $event, $listener = null, int $priority = 1)

/**
* Add a route group.
* @param array|string $prefix
*/
public function addGroup($prefix, callable $callback, array $options = [])
public function addGroup(array|string $prefix, callable $callback, array $options = []): void
{
$router = $this->dispatcherFactory->getRouter($this->serverName);
if (isset($options['middleware'])) {
$this->convertClosureToMiddleware($options['middleware']);
}
return $router->addGroup($prefix, $callback, $options);
$router->addGroup($prefix, $callback, $options);
}

/**
Expand Down Expand Up @@ -216,7 +209,7 @@ function ($handler) {
* Add a new crontab.
* @param callable|string $crontab
*/
public function addCrontab(string $rule, $crontab): Crontab
public function addCrontab(string $rule, callable|string $crontab): Crontab
{
$this->config->set('crontab.enable', true);
$this->ensureConfigHasValue('processes', CrontabDispatcherProcess::class);
Expand Down Expand Up @@ -251,7 +244,7 @@ function ($crontab) {
* Add a new process.
* @param callable|string $process
*/
public function addProcess($process)
public function addProcess(callable|string $process)
{
if (is_string($process)) {
$this->appendConfig('processes', $process);
Expand All @@ -275,7 +268,7 @@ public function addProcess($process)
* @param mixed $httpMethod
* @param mixed $handler
*/
public function addRoute($httpMethod, string $route, $handler, array $options = [])
public function addRoute($httpMethod, string $route, $handler, array $options = []): void
{
$router = $this->dispatcherFactory->getRouter($this->serverName);
if (isset($options['middleware'])) {
Expand All @@ -284,7 +277,7 @@ public function addRoute($httpMethod, string $route, $handler, array $options =
if ($handler instanceof \Closure) {
$handler = $handler->bindTo($this->bound, $this->bound);
}
return $router->addRoute($httpMethod, $route, $handler, $options);
$router->addRoute($httpMethod, $route, $handler, $options);
}

/**
Expand Down
30 changes: 9 additions & 21 deletions src/ContainerProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,12 @@

class ContainerProxy implements BoundInterface, ContainerInterface
{
/**
* @var ContainerInterface
*/
private $container;
private ?RequestInterface $request;

/**
* @var RequestInterface
*/
private $request;
private ?ResponseInterface $response;

/**
* @var ResponseInterface
*/
private $response;

public function __construct(ContainerInterface $container)
public function __construct(private ContainerInterface $container)
{
$this->container = $container;
$this->request = $container->get(RequestInterface::class);
$this->response = $container->get(ResponseInterface::class);
}
Expand All @@ -49,9 +37,9 @@ public function get($id)
return $this->container->get($id);
}

public function define(string $name, $definition)
public function define(string $name, $definition): void
{
return $this->container->define($name, $definition);
$this->container->define($name, $definition);
}

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

public function set(string $name, $entry)
public function set(string $name, $entry): void
{
return $this->container->set($name, $entry);
$this->container->set($name, $entry);
}

public function unbind(string $name)
public function unbind(string $name): void
{
/* @phpstan-ignore-next-line */
return $this->container->unbind($name);
$this->container->unbind($name);
}
}
21 changes: 21 additions & 0 deletions src/Factory/AppFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
*/
namespace Hyperf\Nano\Factory;

use Dotenv\Dotenv;
use Dotenv\Repository\Adapter\PutenvAdapter;
use Dotenv\Repository\RepositoryBuilder;
use Hyperf\Config\Config;
use Hyperf\Config\ProviderConfig;
use Hyperf\Contract\ConfigInterface;
Expand Down Expand Up @@ -67,6 +70,11 @@ public static function createApp(array $dependencies = []): App
// Setting ini and flags
self::prepareFlags();

// Load envs
if (file_exists(BASE_PATH . '/.env')) {
self::loadDotenv();
}

// Prepare container
$container = self::prepareContainer($dependencies);

Expand Down Expand Up @@ -111,4 +119,17 @@ protected static function prepareFlags(int $hookFlags = SWOOLE_HOOK_ALL)
! defined('BASE_PATH') && define('BASE_PATH', $projectRootPath);
! defined('SWOOLE_HOOK_FLAGS') && define('SWOOLE_HOOK_FLAGS', $hookFlags);
}

/**
* Setup envs.
*/
protected static function loadDotenv(): void
{
$repository = RepositoryBuilder::createWithNoAdapters()
->addAdapter(PutenvAdapter::class)
->immutable()
->make();

Dotenv::create($repository, [BASE_PATH])->load();
}
}
5 changes: 1 addition & 4 deletions src/Factory/CommandFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,9 @@ class CommandFactory
public function create(string $name, \Closure $closure): Command
{
return new class($name, $closure) extends Command {
private $closure;

public function __construct(string $name, \Closure $closure)
public function __construct(string $name, private \Closure $closure)
{
parent::__construct($name);
$this->closure = $closure;
}

public function handle()
Expand Down
8 changes: 1 addition & 7 deletions src/Factory/CronFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,8 @@

class CronFactory
{
/**
* @var ContainerInterface
*/
private $container;

public function __construct(ContainerInterface $container)
public function __construct(private ContainerInterface $container)
{
$this->container = $container;
}

public function execute($name)
Expand Down
5 changes: 1 addition & 4 deletions src/Factory/ProcessFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,9 @@ public function create(\Closure $closure): AbstractProcess
{
$container = ApplicationContext::getContainer();
return new class($container, $closure) extends AbstractProcess {
private $closure;

public function __construct(ContainerInterface $container, \Closure $closure)
public function __construct(ContainerInterface $container, private \Closure $closure)
{
parent::__construct($container);
$this->closure = $closure;
}

public function handle(): void
Expand Down
Loading

0 comments on commit 526e7b8

Please sign in to comment.