summaryrefslogtreecommitdiff
path: root/src/Container/Container.php
blob: 59a17a042a35a099577d0f9a0ffadb182144a289 (plain)
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
<?php

namespace Engelsystem\Container;

use Psr\Container\ContainerExceptionInterface;
use Psr\Container\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface;

class Container implements ContainerInterface
{
    /**
     * The globally available container
     *
     * @var static
     */
    protected static $instance;

    /**
     * Contains the shared instances
     *
     * @var mixed[]
     */
    protected $instances = [];

    /**
     * Finds an entry of the container by its identifier and returns it
     *
     * @param string $id Identifier of the entry to look for
     *
     * @throws NotFoundExceptionInterface  No entry was found for **this** identifier
     * @throws ContainerExceptionInterface Error while retrieving the entry
     *
     * @return mixed Entry
     */
    public function get($id)
    {
        if ($this->has($id)) {
            return $this->resolve($id);
        }

        throw new NotFoundException(sprintf('The entry with the id "%s" could not be found', $id));
    }

    /**
     * Register a shared entry in the container
     *
     * @param string $abstract Identifier of the entry to set
     * @param mixed  $instance Entry
     */
    public function instance($abstract, $instance)
    {
        $this->singleton($abstract, $instance);
    }

    /**
     * Register a shared entry as singleton in the container
     *
     * @param string $abstract
     * @param mixed  $instance
     */
    public function singleton($abstract, $instance)
    {
        $this->instances[$abstract] = $instance;
    }

    /**
     * Returns true if the container can return an entry for the given identifier
     * Returns false otherwise
     *
     * `has($id)` returning true does not mean that `get($id)` will not throw an exception
     * It does however mean that `get($id)` will not throw a `NotFoundExceptionInterface`
     *
     * @param string $id Identifier of the entry to look for
     *
     * @return bool
     */
    public function has($id)
    {
        return isset($this->instances[$id]);
    }

    /**
     * Resolve the requested object
     *
     * @param string $abstract
     * @return mixed
     */
    protected function resolve($abstract)
    {
        return $this->instances[$abstract];
    }

    /**
     * Get the globally available instance of the container
     *
     * @return self
     */
    public static function getInstance()
    {
        if (is_null(static::$instance)) {
            static::$instance = new static;
        }

        return static::$instance;
    }

    /**
     * Set the globally available instance of the container
     *
     * @param Container $container
     */
    public static function setInstance(Container $container)
    {
        static::$instance = $container;
    }
}