Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
factory.php
1<?php
2
4
27
28class Factory implements IFactory
29{
31 private static $delegate = null;
32
36 public static function createConfig(string $serviceType): Container
37 {
38 $result = null;
39
40 if(self::$delegate !== null && self::$delegate instanceof IFactory)
41 {
42 if($result = self::$delegate::createConfig($serviceType))
43 {
44 return $result;
45 }
46 }
47
48 return new Container(
49 static::getServiceConfig($serviceType)
50 );
51 }
52
56 public static function setDelegate(IFactory $factory): void
57 {
58 self::$delegate = $factory;
59 }
60
61 protected static function getLogLevel(): int
62 {
63 return (int)Option::get('location', 'log_level', LoggerService\LogLevel::ERROR);
64 }
65
66 protected static function getServiceConfig(string $serviceType)
67 {
68 $result = [];
69
70 switch ($serviceType)
71 {
72 case LoggerService::class:
73 $result = [
74 'logger' => new LoggerService\CEventLogger(),
75 'logLevel'=> static::getLogLevel(),
76 'eventsToLog' => []
77 ];
78 break;
79
80 case ErrorService::class:
81 $result = [
82 'logErrors' => true,
83 'throwExceptionOnError' => false
84 ];
85 break;
86
87 case FormatService::class:
88 $result = [
89 'repository' => new FormatRepository([
90 'dataCollection' => DataCollection::class //Format data collection
91 ]),
92 'defaultFormatCode' => \Bitrix\Location\Infrastructure\FormatCode::getCurrent()
93 ];
94 break;
95
96 case AddressService::class:
97 $result = [
98 'repository' => new AddressRepository()
99 ];
100 break;
101
102 case SourceService::class:
103 $result = [
104 'source' => self::obtainSource()
105 ];
106 break;
107
108 case LocationService::class:
109 $result = [
110 'repository' => static::createLocationRepository(
111 self::obtainSource()
112 )
113 ];
114 break;
115
116 case CurrentRegionFinderService::class:
117 case DisputedAreaService::class:
118 case RecentAddressesService::class:
119 break;
120
121 default:
122 throw new \LogicException("Unknown service type \"${serviceType}\"", ErrorCodes::SERVICE_CONFIG_FABRIC_WRONG_SERVICE);
123 }
124
125 return $result;
126 }
127
132 private static function createLocationRepository(Source $source = null): LocationRepository
133 {
134 $cacheTTL = 2592000; //month
135 $poolSize = 30;
136 $pool = new Repository\Location\Cache\Pool($poolSize);
137
138 $cache = new Repository\Location\Cache(
139 $pool,
140 $cacheTTL,
141 'locationRepositoryCache',
142 \Bitrix\Main\Data\Cache::createInstance(),
144 );
145
146 $repositories = [
147 $cache,
148 new Database()
149 ];
150
151 if($source)
152 {
153 $repositories[] = $source->makeRepository();
154 }
155
156 return new LocationRepository(
157 new Find($repositories),
158 new Save($repositories),
159 new Delete($repositories)
160 );
161 }
162
166 private static function obtainSource(): ?Source
167 {
168 static $result;
169 if (!is_null($result))
170 {
171 return $result;
172 }
173
174 $result = (new Repository\SourceRepository(new Source\OrmConverter()))
175 ->findByCode(SourceCodePicker::getSourceCode())
176 ;
177
178 return $result && $result->isAvailable() ? $result : null;
179 }
180}