Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
ContainerBuilder.php
1<?php
2
4
8
18{
19 private static function getLocalPaths($path): array
20 {
21 $paths = [];
23
24 if (File::isFileExists($root . '/bitrix/' . $path))
25 {
26 $paths[] = $root . '/bitrix/' . $path;
27 }
28
29 if (File::isFileExists($root . '/local/' . $path))
30 {
31 $paths[] = $root . 'local/' . $path;
32 }
33
34 return $paths;
35 }
36
37 private static function getConfigPaths(): array
38 {
39 if (!defined('CATALOG_CONTAINER_PATH'))
40 {
41 throw new NotSupportedException('Default container path not found.');
42 }
43
44 $configPaths = static::getLocalPaths(CATALOG_CONTAINER_PATH);
45
46 if (empty($configPaths))
47 {
48 throw new NotSupportedException('Default container config does not exist.');
49 }
50
51 return $configPaths;
52 }
53
54 private static function loadDependencies(string $customPath = null): array
55 {
56 $dependencies = [];
57
58 $configPaths = static::getConfigPaths();
59
60 if ($customPath)
61 {
62 $configPaths[] = $customPath;
63 }
64
65 foreach ($configPaths as $configPath)
66 {
67 $current = include($configPath);
68
69 if (is_array($current))
70 {
71 $dependencies[] = $current;
72 }
73 else
74 {
75 throw new NotSupportedException(sprintf(
76 'Config {%s} must return an array.', $current
77 ));
78 }
79 }
80
81 return array_merge(...$dependencies);
82 }
83
84 private static function buildContainer(array $dependencies): ContainerContract
85 {
86 $containerClass = $dependencies[Dependency::CONTAINER] ?? null;
87
88 if ($containerClass === null)
89 {
90 throw new NotSupportedException(sprintf(
91 'Container dependency {%s} must be configured in the config file.',
93 ));
94 }
95
97 return new $containerClass();
98 }
99
100 public static function buildFromConfig(string $customPath = null): ContainerContract
101 {
102 $dependencies = static::loadDependencies($customPath);
103 $container = static::buildContainer($dependencies);
104
105 foreach ($dependencies as $dependency => $entity)
106 {
107 $container->inject($dependency, $entity);
108 }
109
110 return $container;
111 }
112}
static buildFromConfig(string $customPath=null)