hyperf/docs/en/upgrade/1.1.md
2022-10-31 00:47:12 +07:00

6.7 KiB

1.1 Upgrade Guide

Version 1.1 has added a lot of functions, but some changes also involve the adjustment of the Skeleton skeleton and the structural adjustment of the configuration items. If you have already invested in a project for business use and created a 1.0 application project based on the officially provided Skeleton project , then you can adjust your skeleton project according to the following content points. If you are a new project, follow the documentation to create a new project with the composer create-project hyperf/hyperf-skeleton command to use the new skeleton structure.

Upgrade Swoole to 4.4+

Version 1.1 raised the minimum Swoole version requirement from 4.3+ to 4.4+. There are some usage details between these two versions. Hyperf has already been adapted in earlier versions, so there is no need for Hyperf users. Ignoring the differences, we have increased the minimum Swoole version requirements mainly to reduce our historical burden, and Swoole 4.4 as Swoole's LTS (Long Term Support Version) also means more stable and reliable.

Hyperf will perform Swoole version detection at startup, but in order to better unify the dependencies on Swoole versions everywhere, we recommend that you change the dependency on Swoole in composer.json to "ext-swoole": " >=4.4".

Add SWOOLE_HOOK_FLAGS constant

Add a line of constant definitions to the entry file bin/hyperf.php of the application and the entry file test/bootstrap.php of the unit test as follows:

! defined('SWOOLE_HOOK_FLAGS') && define('SWOOLE_HOOK_FLAGS', SWOOLE_HOOK_ALL);

Reference: Entry file reference [Single test entry file reference](https://github.com/ hyperf/hyperf-skeleton/blob/70062b7bbf29e23cda2f30680e02aa3b26ebd6f7/test/bootstrap.php#L20)

Move the config/dependencies.php file and adjust the file structure

Move config/dependencies.phpconfig/autoload/dependencies.php, and remove the first level of dependencies in the configuration file, as follows:

1.0 file structure:

<?php
// config/dependencies.php document

return [
    'dependencies' => [
        FooInterface::class => Foo::class
    ],
];

1.1 The file structure of:

<?php
// config/autoload/dependencies.php document

return [
    FooInterface::class => Foo::class
];

Adjust the content of the config/container.php file

Since version 1.1 adjusted the location and structure of the dependencies.php file, we also need to adjust the config/container.php file so that the dependency injection container can run correctly. /container.phpprovides a simpler way of writing,DefinitionSourceFactoryaggregates many default behaviors, you just need to replace the content of theconfig/container.php` file with the following content:

The annotation scanning cache function is enabled by default. You can modify the first parameter of the DefinitionSourceFactory parameter to disable this function

<?php
/**
 * Initial a dependency injection container that implemented PSR-11 and return the container.
 */
declare(strict_types=1);

use Hyperf\Di\Container;
use Hyperf\Di\Definition\DefinitionSourceFactory;
use Hyperf\Utils\ApplicationContext;
use Psr\Container\ContainerInterface;

$container = new Container((new DefinitionSourceFactory(true))());
if (! $container instanceof ContainerInterface) {
    throw new RuntimeException('The dependency injection container is invalid.');
}
return ApplicationContext::setContainer($container);

Adjust the WebSocket controller

Since version 1.1 adjusted the input constraints of onMessage and onOpen, you need to manually modify it to Swoole\WebSocket\Server, the specific code is as follows

<?php
declare(strict_types=1);

namespace App\Controller;

use Hyperf\Contract\OnMessageInterface;
use Hyperf\Contract\OnOpenInterface;
use Swoole\Http\Request;
use Swoole\Websocket\Frame;
use Swoole\WebSocket\Server as WebSocketServer;

class WebSocketController implements OnMessageInterface, OnOpenInterface
{
    public function onMessage(WebSocketServer $server, Frame $frame): void
    {
    }

    public function onOpen(WebSocketServer $server, Request $request): void
    {
    }
}

Adjust the ConfigProvider of the custom component

In version 1.0, scan.path was adjusted to annotations.scan.path in version 1.1. You need to modify the ConfigProvider class of all custom components to adapt to this change. For example, your custom components do not involve annotation scanning. function configuration, this adjustment can be ignored, as follows:

ConfigProvider file structure for 1.0:

class ConfigProvider
{

    public function __invoke(): array
    {
        return [
            'scan' => [
                'paths' => [
                    __DIR__,
                ],
            ],
        ];
    }
}

ConfigProvider file structure for 1.1:

class ConfigProvider
{

    public function __invoke(): array
    {
        return [
            'annotations' => [
                'scan' => [
                    'paths' => [
                        __DIR__,
                    ],
                ],
            ],
        ];
    }
}

Adjust the default localization language

If you have used the hyperf/translation component before, then you need to check the locale configuration item in the config/autoload/translation.php file, such as If it is zh-CN, it needs to be changed to zh_CN. In version 1.1, we unified the value of this configuration.

Adjust the dependencies of composer.json

Since we want to upgrade to the 1.1 version of the component, and the original skeleton project depends on the 1.0.x version of the component by default, we need to make some adjustments to the constraints of the dependency, and change the dependencies of all the original Hyperf components to ~1.0.0 is modified to ~1.1.0. After modification, you need to run composer update to upgrade the dependencies to version 1.1.

All Hyperf dependencies must be upgraded to version 1.1 to be available, because 1.1 adjusts the ConfigProvider mechanism for component adaptation.

complete the upgrade

At this point, the 1.1 upgrade has been completed, but since each underlying file of Hyperf can be rewritten through DI, if you rewrite some internal files of the framework adjusted in this upgrade, you still need to rewrite it according to your Make some adjustments to the actual situation.

If you encounter any problems during or after the upgrade, please go to Github Issue to submit your issue, explain the problem you have encountered, and we will try to fix it as soon as possible help you out.