hyperf/docs/en/upgrade/2.1.md
2022-10-31 22:43:05 +07:00

4.3 KiB

2.1 Upgrade Guide

  • Version 2.1 mainly adds the underlying driver hyperf/engine, allowing the Hyperf framework to run on top of Swoole or Swow.
  • Minimum PHP version changed from 7.2 to 7.3

Swow is temporarily a preview version, please use it with caution.

Modify Hyperf component version

Simply change hyperf/* in composer.json to 2.1.*.

{
    "require": {
        "php": ">=7.3",
        "ext-json": "*",
        "ext-openssl": "*",
        "ext-pdo": "*",
        "ext-pdo_mysql": "*",
        "ext-redis": "*",
        "ext-swoole": ">=4.5",
        "hyperf/async-queue": "2.1.*",
        "hyperf/cache": "2.1.*",
        "hyperf/command": "2.1.*",
        "hyperf/config": "2.1.*",
        "hyperf/constants": "2.1.*",
        "hyperf/contract": "2.1.*",
        "hyperf/database": "2.1.*",
        "hyperf/db-connection": "2.1.*",
        "hyperf/di": "2.1.*",
        "hyperf/dispatcher": "2.1.*",
        "hyperf/event": "2.1.*",
        "hyperf/exception-handler": "2.1.*",
        "hyperf/framework": "2.1.*",
        "hyperf/guzzle": "2.1.*",
        "hyperf/http-server": "2.1.*",
        "hyperf/logger": "2.1.*",
        "hyperf/model-cache": "2.1.*",
        "hyperf/pool": "2.1.*",
        "hyperf/process": "2.1.*",
        "hyperf/redis": "2.1.*",
        "hyperf/server": "2.1.*",
        "hyperf/utils": "2.1.*"
    },
    "require-dev": {
        "friendsofphp/php-cs-fixer": "^2.14",
        "hyperf/devtool": "2.1.*",
        "hyperf/testing": "2.1.*",
        "mockery/mockery": "^1.0",
        "phpstan/phpstan": "^0.12.18",
        "swoole/ide-helper": "dev-master",
        "symfony/var-dumper": "^5.1"
    }
}

After that, you only need to execute composer update -o, and the upgrade can be completed normally.

Add optimize-autoloader configuration

Because Hyperf needs to use class_map generated by composer since 2.0, which requires users to use -o for optimization every time they update dependencies, but many users never have this habit. Therefore, we recommend adding corresponding configuration to composer.json to meet this need.

{
    "config": {
        "optimize-autoloader": true,
        "sort-packages": true
    }
}

Modify SwooleEvent

The class Hyperf\Server\SwooleEvent has been renamed to Hyperf\Server\Event in 2.1, so we need to modify the corresponding code in the server.php configuration.

SwooleEvent will be officially removed in 3.0, please change it to Event as soon as possible

<?php

declare(strict_types=1);

use Hyperf\Server\Event;
use Hyperf\Server\Server;

return [
    'mode' => SWOOLE_BASE,
    'servers' => [
        [
            'name' => 'http',
            'type' => Server::SERVER_HTTP,
            'host' => '0.0.0.0',
            'port' => 9501,
            'sock_type' => SWOOLE_SOCK_TCP,
            'callbacks' => [
                Event::ON_REQUEST => [Hyperf\HttpServer\Server::class, 'onRequest'],
            ],
        ],
    ],
    'callbacks' => [
        Event::ON_BEFORE_START => [Hyperf\Framework\Bootstrap\ServerStartCallback::class, 'beforeStart'],
        Event::ON_WORKER_START => [Hyperf\Framework\Bootstrap\WorkerStartCallback::class, 'onWorkerStart'],
        Event::ON_PIPE_MESSAGE => [Hyperf\Framework\Bootstrap\PipeMessageCallback::class, 'onPipeMessage'],
        Event::ON_WORKER_EXIT => [Hyperf\Framework\Bootstrap\WorkerExitCallback::class, 'onWorkerExit'],
    ],
];

Paginator

Because the component hyperf/paginator has been removed from the hyperf/database dependency. Therefore, students who use the pager in the database also need to introduce the hyperf/paginator component.

Modify DBAL version

If you use the doctrine/dbal component, you need to upgrade to the ^3.0 version.

remove component doctrine/common

The doctrine/common component has a dependency conflict with hyperf/utils. So this component needs to be removed from composer.json.

# remove component
composer remove doctrine/common
# update
composer update "hyperf/*" -o

Precautions

  • Try not to change the engine of the old project to Swow, if you want to use Swow, please try it in the new project. Because Swow is not a substitute for Swoole, not all Swoole scenarios can find corresponding alternatives in Swow.