Repository navigation
Expand file tree
/
Copy pathConfigFactory.php
More file actions
55 lines (49 loc) · 1.55 KB
/
Copy pathConfigFactory.php
File metadata and controls
55 lines (49 loc) · 1.55 KB
1
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
49
50
51
52
53
54
55
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact [email protected]
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace Hyperf\Config;
use Hyperf\Collection\Arr;
use Psr\Container\ContainerInterface;
use Symfony\Component\Finder\Finder;
class ConfigFactory
{
public function __invoke(ContainerInterface $container)
{
$configPath = BASE_PATH . '/config';
$config = $this->readConfig($configPath . '/config.php');
$autoloadConfig = $this->readPaths([$configPath . '/autoload']);
$merged = array_merge_recursive(ProviderConfig::load(), $config, ...$autoloadConfig);
return new Config($merged);
}
private function readConfig(string $configPath): array
{
$config = [];
if (file_exists($configPath) && is_readable($configPath)) {
$config = require $configPath;
}
return is_array($config) ? $config : [];
}
private function readPaths(array $paths): array
{
$configs = [];
$finder = new Finder();
$finder->files()->in($paths)->name('*.php');
foreach ($finder as $file) {
$config = [];
$key = implode('.', array_filter([
str_replace('/', '.', $file->getRelativePath()),
$file->getBasename('.php'),
]));
Arr::set($config, $key, require $file->getRealPath());
$configs[] = $config;
}
return $configs;
}
}