ConfigProvider Explanation β
How to configure ConfigProvider and publish your application's own configuration files
Mechanism Overview β
This mechanism is derived from Hyperf's ConfigProvider mechanism
The ConfigProvider
mechanism is crucial for the modularization of Hyperf
. Features such as decoupling between components
, component independence
, and component reusability
are all achieved through this mechanism.
In simple terms, each component provides a ConfigProvider
, typically as a class located in the component's root directory. The ConfigProvider
supplies all configuration information for the corresponding component. This information is loaded by the Hyperf
framework during startup, and the configurations within ConfigProvider
are eventually merged into the implementation class of Hyperf\Contract\ConfigInterface
. This ensures that each component is properly initialized when used within the Hyperf
framework.
The ConfigProvider
itself has no dependencies, does not inherit any abstract classes, and does not require the implementation of any interfaces. It only needs to provide an __invoke
method that returns an array with the corresponding configuration structure.
Publishing Your Own Configuration Files β
To publish your configuration files, simply define the publish
item in the array structure and set the following parameters. When installing the MineAdmin
application, these configuration files will automatically be published to the config/autoload
directory.
Refer to the example code below for specifics:
id
description
source
destination
TIP
The merging of configuration files is not physical but occurs in memory during system startup via Hyperf. You can verify this by printing the results of configuration retrieval functions.
ConfigProvider Example β
Below is an example:
<?php
declare(strict_types=1);
/**
* This file is part of MineAdmin.
*
* @link https://www.mineadmin.com
* @document https://doc.mineadmin.com
* @contact root@imoi.cn
* @license https://github.com/mineadmin/MineAdmin/blob/master/LICENSE
*/
namespace Plugin\MineAdmin\AppStore;
class ConfigProvider
{
public function __invoke()
{
return [
// Merge into config/autoload/annotations.php
'annotations' => [
'scan' => [
'paths' => [
__DIR__,
],
],
],
// Merge into config/autoload/dependencies.php
'dependencies' => [],
// Default Command definitions, analogous to config/autoload/commands.php
'commands' => [],
// Similar to commands
'listeners' => [],
// Default component configuration files.
// When executing the command, the file specified in 'source' will be copied to the 'destination' path.
'publish' => [
[
'id' => 'config',
'description' => 'description of this config file.', // Description
// Recommended to place default configurations in the publish folder, with filenames matching the component name.
'source' => __DIR__ . '/../publish/appstore.php', // Path to the configuration file
'destination' => BASE_PATH . '/config/autoload/appstore.php', // Copy to this path
],
],
];
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48