Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
/*
4
 * This file is part of the Symfony package.
5
 *
6
 * (c) Fabien Potencier <fabien@symfony.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
 
12
namespace Symfony\Contracts\Service;
13
 
14
use Psr\Container\ContainerExceptionInterface;
15
use Psr\Container\NotFoundExceptionInterface;
16
 
17
// Help opcache.preload discover always-needed symbols
18
class_exists(ContainerExceptionInterface::class);
19
class_exists(NotFoundExceptionInterface::class);
20
 
21
/**
22
 * A trait to help implement ServiceProviderInterface.
23
 *
24
 * @author Robin Chalas <robin.chalas@gmail.com>
25
 * @author Nicolas Grekas <p@tchwork.com>
26
 */
27
trait ServiceLocatorTrait
28
{
29
    private array $factories;
30
    private array $loading = [];
31
    private array $providedTypes;
32
 
33
    /**
34
     * @param callable[] $factories
35
     */
36
    public function __construct(array $factories)
37
    {
38
        $this->factories = $factories;
39
    }
40
 
41
    public function has(string $id): bool
42
    {
43
        return isset($this->factories[$id]);
44
    }
45
 
46
    public function get(string $id): mixed
47
    {
48
        if (!isset($this->factories[$id])) {
49
            throw $this->createNotFoundException($id);
50
        }
51
 
52
        if (isset($this->loading[$id])) {
53
            $ids = array_values($this->loading);
54
            $ids = \array_slice($this->loading, array_search($id, $ids));
55
            $ids[] = $id;
56
 
57
            throw $this->createCircularReferenceException($id, $ids);
58
        }
59
 
60
        $this->loading[$id] = $id;
61
        try {
62
            return $this->factories[$id]($this);
63
        } finally {
64
            unset($this->loading[$id]);
65
        }
66
    }
67
 
68
    public function getProvidedServices(): array
69
    {
70
        if (!isset($this->providedTypes)) {
71
            $this->providedTypes = [];
72
 
73
            foreach ($this->factories as $name => $factory) {
74
                if (!\is_callable($factory)) {
75
                    $this->providedTypes[$name] = '?';
76
                } else {
77
                    $type = (new \ReflectionFunction($factory))->getReturnType();
78
 
79
                    $this->providedTypes[$name] = $type ? ($type->allowsNull() ? '?' : '').($type instanceof \ReflectionNamedType ? $type->getName() : $type) : '?';
80
                }
81
            }
82
        }
83
 
84
        return $this->providedTypes;
85
    }
86
 
87
    private function createNotFoundException(string $id): NotFoundExceptionInterface
88
    {
89
        if (!$alternatives = array_keys($this->factories)) {
90
            $message = 'is empty...';
91
        } else {
92
            $last = array_pop($alternatives);
93
            if ($alternatives) {
94
                $message = sprintf('only knows about the "%s" and "%s" services.', implode('", "', $alternatives), $last);
95
            } else {
96
                $message = sprintf('only knows about the "%s" service.', $last);
97
            }
98
        }
99
 
100
        if ($this->loading) {
101
            $message = sprintf('The service "%s" has a dependency on a non-existent service "%s". This locator %s', end($this->loading), $id, $message);
102
        } else {
103
            $message = sprintf('Service "%s" not found: the current service locator %s', $id, $message);
104
        }
105
 
106
        return new class($message) extends \InvalidArgumentException implements NotFoundExceptionInterface {
107
        };
108
    }
109
 
110
    private function createCircularReferenceException(string $id, array $path): ContainerExceptionInterface
111
    {
112
        return new class(sprintf('Circular reference detected for service "%s", path: "%s".', $id, implode(' -> ', $path))) extends \RuntimeException implements ContainerExceptionInterface {
113
        };
114
    }
115
}