1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
7
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
8
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
9
|
|
|
* |
10
|
|
|
* Copyright (c) 2024 Mykhailo Shtanko [email protected] |
11
|
|
|
* |
12
|
|
|
* For the full copyright and license information, please view the LICENSE.MD |
13
|
|
|
* file that was distributed with this source code. |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
namespace FRZB\Component\MetricsPower\Logger; |
17
|
|
|
|
18
|
|
|
use Fp\Collections\HashMap; |
19
|
|
|
use FRZB\Component\DependencyInjection\Attribute\AsService; |
20
|
|
|
use FRZB\Component\MetricsPower\Logger\ContextExtractor\ContextExtractorInterface; |
21
|
|
|
use FRZB\Component\MetricsPower\Logger\ContextExtractor\DefaultContextExtractor; |
22
|
|
|
use FRZB\Component\MetricsPower\Logger\Exception\ContextExtractorLocatorException; |
23
|
|
|
use Psr\Container\ContainerExceptionInterface; |
24
|
|
|
use Psr\Container\NotFoundExceptionInterface; |
25
|
|
|
use Symfony\Component\DependencyInjection\Attribute\TaggedIterator; |
26
|
|
|
use Symfony\Component\DependencyInjection\Attribute\TaggedLocator; |
27
|
|
|
use Symfony\Component\DependencyInjection\ServiceLocator; |
28
|
|
|
use Symfony\Contracts\Service\ServiceProviderInterface; |
29
|
|
|
|
30
|
|
|
#[AsService] |
31
|
|
|
final readonly class ContextExtractorLocator implements ContextExtractorLocatorInterface |
|
|
|
|
32
|
|
|
{ |
33
|
|
|
public function __construct( |
34
|
|
|
#[TaggedLocator(ContextExtractorInterface::class, defaultIndexMethod: 'getType')] |
35
|
|
|
private ServiceProviderInterface $serviceProvider, |
36
|
|
|
) { |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function get(object|string $target): ContextExtractorInterface |
40
|
|
|
{ |
41
|
|
|
try { |
42
|
|
|
return $this->serviceProvider->get(\is_object($target) ? $target::class : $target); |
43
|
|
|
} catch (NotFoundExceptionInterface) { |
44
|
|
|
return $this->serviceProvider->get(DefaultContextExtractor::getType()); |
45
|
|
|
} catch (ContainerExceptionInterface $e) { |
46
|
|
|
throw ContextExtractorLocatorException::fromThrowable($e); |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|