Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
 
3
declare(strict_types=1);
4
 
5
namespace DI\Definition;
6
 
7
use Psr\Container\ContainerInterface;
8
 
9
/**
10
 * Represents a reference to another entry.
11
 *
12
 * @author Matthieu Napoli <matthieu@mnapoli.fr>
13
 */
14
class Reference implements Definition, SelfResolvingDefinition
15
{
16
    /** Entry name. */
17
    private string $name = '';
18
 
19
    /**
20
     * @param string $targetEntryName Name of the target entry
21
     */
22
    public function __construct(
23
        private string $targetEntryName,
24
    ) {
25
    }
26
 
27
    public function getName() : string
28
    {
29
        return $this->name;
30
    }
31
 
32
    public function setName(string $name) : void
33
    {
34
        $this->name = $name;
35
    }
36
 
37
    public function getTargetEntryName() : string
38
    {
39
        return $this->targetEntryName;
40
    }
41
 
42
    public function resolve(ContainerInterface $container) : mixed
43
    {
44
        return $container->get($this->getTargetEntryName());
45
    }
46
 
47
    public function isResolvable(ContainerInterface $container) : bool
48
    {
49
        return $container->has($this->getTargetEntryName());
50
    }
51
 
52
    public function replaceNestedDefinitions(callable $replacer) : void
53
    {
54
        // no nested definitions
55
    }
56
 
57
    public function __toString() : string
58
    {
59
        return sprintf(
60
            'get(%s)',
61
            $this->targetEntryName
62
        );
63
    }
64
}