This repository has been archived by the owner on Dec 13, 2022. It is now read-only.
/
ServiceProvider.php
308 lines (252 loc) · 11.6 KB
/
ServiceProvider.php
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
<?php
/*
* Copyright 2005 - 2019 Centreon (https://www.centreon.com/)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* For more information : contact@centreon.com
*
*/
namespace Centreon;
use Pimple\Container;
use Pimple\Psr11\ServiceLocator;
use CentreonLegacy\ServiceProvider as LegacyServiceProvider;
use Centreon\Application\Webservice;
use Centreon\Infrastructure\Provider\AutoloadServiceProviderInterface;
use Centreon\Infrastructure\Service;
use Centreon\Infrastructure\Event;
use Centreon\Infrastructure\Service\CentreonWebserviceService;
use Centreon\Infrastructure\Service\CentreonClapiService;
use Centreon\Infrastructure\Service\CentcoreConfigService;
use Centreon\Infrastructure\Service\CentreonDBManagerService;
use Centreon\Domain\Service\I18nService;
use Centreon\Domain\Service\FrontendComponentService;
use Centreon\Domain\Service\BrokerConfigurationService;
use Centreon\Domain\Repository\CfgCentreonBrokerRepository;
use Centreon\Domain\Repository\CfgCentreonBrokerInfoRepository;
use CentreonClapi\CentreonACL;
use Centreon\Application\Validation;
use Symfony\Component\Validator;
use Symfony\Component\Validator\Constraints;
use CentreonACL as CACL;
use Doctrine\Common\Annotations\AnnotationReader;
use Symfony\Component\Serializer;
use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
class ServiceProvider implements AutoloadServiceProviderInterface
{
//webservices
public const CENTREON_WEBSERVICE = 'centreon.webservice';
public const MENU_WEBSERVICE = 'centreon.menu.webservice';
//services
public const CENTREON_PAGINATION = 'centreon.pagination';
public const CENTREON_I18N_SERVICE = 'centreon.i18n_service';
public const CENTREON_FRONTEND_COMPONENT_SERVICE = 'centreon.frontend_component_service';
public const CENTREON_BROKER_CONFIGURATION_SERVICE = 'centreon.broker_configuration_service';
//repositories
public const CENTREON_BROKER_REPOSITORY = 'centreon.broker_repository';
public const CENTREON_BROKER_INFO_REPOSITORY = 'centreon.broker_info_repository';
//managers and infrastructure services
public const CENTREON_DB_MANAGER = 'centreon.db-manager';
public const CENTREON_CLAPI = 'centreon.clapi';
public const UPLOAD_MANGER = 'upload.manager';
public const CENTREON_EVENT_DISPATCHER = 'centreon.event_dispatcher';
public const CENTREON_USER = 'centreon.user';
public const YML_CONFIG = 'yml.config';
public const CENTREON_VALIDATOR_FACTORY = 'centreon.validator_factory';
public const CENTREON_VALIDATOR_TRANSLATOR = 'centreon.validator_translator';
public const VALIDATOR = 'validator';
public const VALIDATOR_EXPRESSION = 'validator.expression';
public const SERIALIZER = 'serializer';
public const SERIALIZER_OBJECT_NORMALIZER = 'serializer.object.normalizer';
public const CENTREON_ACL = 'centreon.acl';
public const CENTREON_GLOBAL_ACL = 'centreon.global.acl';
/**
* {@inheritdoc}
*/
public function register(Container $pimple): void
{
//init global yml config from src/Centreon
$pimple[static::YML_CONFIG] = function (Container $pimple) {
return $pimple[\CentreonLegacy\ServiceProvider::CONFIGURATION]->getModuleConfig(__DIR__);
};
$pimple[static::CENTREON_WEBSERVICE] = function (Container $container): CentreonWebserviceService {
$service = new CentreonWebserviceService();
return $service;
};
$pimple[static::CENTREON_WEBSERVICE]
->add(Application\Webservice\TopologyWebservice::class)
->add(Application\Webservice\ContactGroupsWebservice::class)
->add(Application\Webservice\ImagesWebservice::class)
->add(Application\Webservice\AclGroupWebservice::class)
// add webservice to get translation from centreon and its extensions
->add(Webservice\CentreonI18n::class)
// add webservice to get frontend hooks and pages installed by modules and widgets
->add(Webservice\CentreonFrontendComponent::class);
if (defined('OpenApi\UNDEFINED') !== false) {
$pimple[static::CENTREON_WEBSERVICE]->add(\Centreon\Application\Webservice\OpenApiWebservice::class);
}
$pimple[static::CENTREON_I18N_SERVICE] = function (Container $pimple): I18nService {
$pimple['translator']; // bind lang
$service = new I18nService(
$pimple[LegacyServiceProvider::CENTREON_LEGACY_MODULE_INFORMATION],
$pimple['finder'],
$pimple['filesystem']
);
return $service;
};
$pimple[static::CENTREON_FRONTEND_COMPONENT_SERVICE] = function (Container $pimple): FrontendComponentService {
return new FrontendComponentService(
new ServiceLocator(
$pimple,
FrontendComponentService::dependencies()
)
);
};
$pimple[static::CENTREON_CLAPI] = function (Container $container): CentreonClapiService {
$service = new CentreonClapiService();
return $service;
};
$pimple[static::CENTREON_DB_MANAGER] = function (Container $container): CentreonDBManagerService {
$services = [
'realtime_db',
'configuration_db',
];
$locator = new ServiceLocator($container, $services);
$service = new CentreonDBManagerService($locator);
return $service;
};
$pimple[static::CENTREON_PAGINATION] = function (Container $container): Service\CentreonPaginationService {
$service = new Service\CentreonPaginationService(
new ServiceLocator(
$container,
Service\CentreonPaginationService::dependencies()
)
);
return $service;
};
$pimple['centreon.user'] = function (Container $container): ?\CentreonUser {
// @codeCoverageIgnoreStart
if (!empty($GLOBALS['centreon']->user) && $GLOBALS['centreon']->user instanceof \CentreonUser) {
return $GLOBALS['centreon']->user;
} elseif (php_sapi_name() !== 'cli' && session_status() == PHP_SESSION_NONE) {
session_start();
}
return $_SESSION['centreon']->user; // @codeCoverageIgnoreEnd
};
$pimple[static::CENTREON_ACL] = function (Container $container): CentreonACL {
$service = new CentreonACL($container);
return $service;
};
$pimple[static::CENTREON_GLOBAL_ACL] = function (): CACL {
$service = new CACL($_SESSION['centreon']->user->user_id, $_SESSION['centreon']->user->admin);
return $service;
};
$pimple['centreon.config'] = function (): CentcoreConfigService {
$service = new CentcoreConfigService();
return $service;
};
/**
* Repositories
*/
// @todo class is available via centreon.db-manager
$pimple[static::CENTREON_BROKER_REPOSITORY] = function (Container $container): CfgCentreonBrokerRepository {
$service = new CfgCentreonBrokerRepository($container['configuration_db']);
return $service;
};
// @todo class is available via centreon.db-manager
$pimple[static::CENTREON_BROKER_INFO_REPOSITORY] =
function (Container $container): CfgCentreonBrokerInfoRepository {
$service = new CfgCentreonBrokerInfoRepository($container['configuration_db']);
return $service;
};
/**
* Services
*/
$pimple[static::CENTREON_BROKER_CONFIGURATION_SERVICE] =
function (Container $container): BrokerConfigurationService {
$service = new BrokerConfigurationService($container['configuration_db']);
$service->setBrokerInfoRepository($container[ServiceProvider::CENTREON_BROKER_INFO_REPOSITORY]);
return $service;
};
$pimple[static::UPLOAD_MANGER] = function (Container $pimple): Service\UploadFileService {
$services = [];
$locator = new ServiceLocator($pimple, $services);
$service = new Service\UploadFileService($locator, $_FILES);
return $service;
};
$pimple[static::CENTREON_EVENT_DISPATCHER] = function (): Event\EventDispatcher {
$eventDispatcher = new Event\EventDispatcher();
$eventDispatcher->setDispatcherLoader(
new Event\FileLoader(
__DIR__ . '/../../www/modules/',
'custom-module-form.php'
)
);
return $eventDispatcher;
};
$this->registerValidator($pimple);
$pimple[static::SERIALIZER_OBJECT_NORMALIZER] = function (): Serializer\Normalizer\ObjectNormalizer {
$classMetadataFactory = new Serializer\Mapping\Factory\ClassMetadataFactory(
new Serializer\Mapping\Loader\AnnotationLoader(new AnnotationReader())
);
return new Serializer\Normalizer\ObjectNormalizer(
$classMetadataFactory,
new Serializer\NameConverter\MetadataAwareNameConverter($classMetadataFactory),
null,
new ReflectionExtractor()
);
};
$pimple[static::SERIALIZER] = function ($container): Serializer\Serializer {
return new Serializer\Serializer([
$container[static::SERIALIZER_OBJECT_NORMALIZER],
new Serializer\Normalizer\ArrayDenormalizer(),
], [
new Serializer\Encoder\JsonEncoder(),
]);
};
}
/**
* Register services related with validation
*
* @param \Pimple\Container $pimple
*/
public function registerValidator(Container $pimple): void
{
$pimple[static::VALIDATOR] = function (Container $container): Validator\Validator\ValidatorInterface {
return Validator\Validation::createValidatorBuilder()
->addMethodMapping('loadValidatorMetadata')
->setConstraintValidatorFactory($container[ServiceProvider::CENTREON_VALIDATOR_FACTORY])
->setTranslator($container[ServiceProvider::CENTREON_VALIDATOR_TRANSLATOR])
->getValidator();
};
$pimple[static::CENTREON_VALIDATOR_FACTORY] =
function (Container $container): Validation\CentreonValidatorFactory {
$service = new Validation\CentreonValidatorFactory($container);
return $service;
};
$pimple[static::CENTREON_VALIDATOR_TRANSLATOR] =
function (Container $container): Validation\CentreonValidatorTranslator {
return new Validation\CentreonValidatorTranslator();
};
$pimple[static::VALIDATOR_EXPRESSION] = function (): Constraints\ExpressionValidator {
return new Constraints\ExpressionValidator();
};
}
/**
* {@inheritdoc}
*/
public static function order(): int
{
return 1;
}
}