ServiceLocator::createCircularReferenceException()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of DivineNii opensource projects.
7
 *
8
 * PHP version 7.4 and above required
9
 *
10
 * @author    Divine Niiquaye Ibok <[email protected]>
11
 * @copyright 2021 DivineNii (https://divinenii.com/)
12
 * @license   https://opensource.org/licenses/BSD-3-Clause License
13
 *
14
 * For the full copyright and license information, please view the LICENSE
15
 * file that was distributed with this source code.
16
 */
17
18
namespace Rade\DI\Services;
19
20
use Nette\Utils\Callback;
21
use Psr\Container\ContainerExceptionInterface;
22
use Rade\DI\Exceptions\CircularReferenceException;
23
use Symfony\Contracts\Service\{ServiceLocatorTrait, ServiceProviderInterface as ServiceProviderContext};
24
25
/**
26
 * Rade PSR-11 service locator.
27
 *
28
 * @author Divine Niiquaye Ibok <[email protected]>
29
 */
30
class ServiceLocator implements ServiceProviderContext
31
{
32
    use ServiceLocatorTrait;
33
34
    /**
35
     * {@inheritdoc}
36
     */
37 7
    public function get($id)
38
    {
39 7
        if (!isset($this->factories[$id])) {
40 3
            throw $this->createNotFoundException($id);
41
        }
42
43 5
        if (isset($this->loading[$id])) {
44 1
            $ids = \array_splice($this->loading, 1);
45
46 1
            throw $this->createCircularReferenceException($id, [...\array_keys($ids), $id]);
47
        }
48
49 5
        $this->loading[$id] = $id;
50
51
        try {
52 5
            $service = $this->factories[$id];
53
54 5
            return \is_callable($service) ? $service() : $service;
55
        } finally {
56 5
            unset($this->loading[$id]);
57
        }
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63 1
    public function getProvidedServices(): array
64
    {
65 1
        if (null === $this->providedTypes) {
66 1
            $this->providedTypes = [];
67
68 1
            foreach ($this->factories as $name => $factory) {
69 1
                if (\is_callable($factory)) {
70 1
                    $type = Callback::toReflection($factory)->getReturnType();
71
72 1
                    $this->providedTypes[$name] = $type ? ($type->allowsNull() ? '?' : '') . ($type instanceof \ReflectionNamedType ? $type->getName() : $type) : '?';
73 1
                } elseif (\is_object($factory) && !$factory instanceof \stdClass) {
74 1
                    $this->providedTypes[$name] = \get_class($factory);
75
                } else {
76 1
                    $this->providedTypes[$name] = '?';
77
                }
78
            }
79
        }
80
81 1
        return $this->providedTypes;
82
    }
83
84
    /**
85
     * @param array<int,string> $path
86
     */
87 1
    private function createCircularReferenceException(string $id, array $path): ContainerExceptionInterface
88
    {
89 1
        return new CircularReferenceException($id, $path);
90
    }
91
}
92