summaryrefslogtreecommitdiffstats
path: root/admin/survey/minify/vendor/mrclay/props-dic/src/Props/Container.php
blob: e1c578989dbc096a1be2c7fe72a75786664033ca (plain) (blame)
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
<?php

namespace Props;

use Interop\Container\ContainerInterface;

/**
 * Container holding values which can be resolved upon reading and optionally stored and shared
 * across reads.
 *
 * Values are read/set as properties.
 *
 * @note see scripts/example.php
 */
class Container implements ContainerInterface
{
    /**
     * @var callable[]
     */
    private $factories = array();

    /**
     * @var array
     */
    private $cache = array();

    /**
     * Fetch a value.
     *
     * @param string $name
     * @return mixed
     * @throws FactoryUncallableException|ValueUnresolvableException|NotFoundException
     */
    public function __get($name)
    {
        if (array_key_exists($name, $this->cache)) {
            return $this->cache[$name];
        }
        $value = $this->build($name);
        $this->cache[$name] = $value;
        return $value;
    }

    /**
     * {@inheritdoc}
     */
    public function get($name)
    {
        return $this->__get($name);
    }

    /**
     * Set a value.
     *
     * @param string $name
     * @param mixed $value
     * @throws \InvalidArgumentException
     */
    public function __set($name, $value)
    {
        if ($value instanceof \Closure) {
            $this->setFactory($name, $value);
            return;
        }

        $this->cache[$name] = $value;
        unset($this->factories[$name]);
    }

    /**
     * Set a value to be later returned as is. You only need to use this if you wish to store
     * a Closure.
     *
     * @param string $name
     * @param mixed $value
     * @throws \InvalidArgumentException
     */
    public function setValue($name, $value)
    {
        unset($this->factories[$name]);
        $this->cache[$name] = $value;
    }

    /**
     * @param string $name
     */
    public function __unset($name)
    {
        unset($this->cache[$name]);
        unset($this->factories[$name]);
    }

    /**
     * @param string $name
     * @return bool
     */
    public function __isset($name)
    {
        return array_key_exists($name, $this->factories) || array_key_exists($name, $this->cache);
    }

    /**
     * {@inheritdoc}
     */
    public function has($name)
    {
        return $this->__isset($name);
    }

    /**
     * Fetch a freshly-resolved value.
     *
     * @param string $method method name must start with "new_"
     * @param array $args
     * @return mixed
     * @throws BadMethodCallException
     */
    public function __call($method, $args)
    {
        if (0 !== strpos($method, 'new_')) {
            throw new BadMethodCallException("Method name must begin with 'new_'");
        }

        return $this->build(substr($method, 4));
    }

    /**
     * Can we fetch a new value via new_$name()?
     *
     * @param string $name
     * @return bool
     */
    public function hasFactory($name)
    {
        return array_key_exists($name, $this->factories);
    }

    /**
     * Set a factory to generate a value when the container is read.
     *
     * @param string   $name    The name of the value
     * @param callable $factory Factory for the value
     * @throws FactoryUncallableException
     */
    public function setFactory($name, $factory)
    {
        if (!is_callable($factory, true)) {
            throw new FactoryUncallableException('$factory must appear callable');
        }

        unset($this->cache[$name]);
        $this->factories[$name] = $factory;
    }

    /**
     * Get an already-set factory callable (Closure, invokable, or callback)
     *
     * @param string $name The name of the value
     * @return callable
     * @throws NotFoundException
     */
    public function getFactory($name)
    {
        if (!array_key_exists($name, $this->factories)) {
            throw new NotFoundException("No factory available for: $name");
        }

        return $this->factories[$name];
    }

    /**
     * Add a function that gets applied to the return value of an existing factory
     *
     * @note A cached value (from a previous property read) will thrown away. The next property read
     *       (and all new_NAME() calls) will call the original factory.
     *
     * @param string   $name     The name of the value
     * @param callable $extender Function that is applied to extend the returned value
     * @return \Closure
     * @throws FactoryUncallableException|NotFoundException
     */
    public function extend($name, $extender)
    {
        if (!is_callable($extender, true)) {
            throw new FactoryUncallableException('$extender must appear callable');
        }

        if (!array_key_exists($name, $this->factories)) {
            throw new NotFoundException("No factory available for: $name");
        }

        $factory = $this->factories[$name];

        $newFactory = function (Container $c) use ($extender, $factory) {
            return call_user_func($extender, call_user_func($factory, $c), $c);
        };

        $this->setFactory($name, $newFactory);

        return $newFactory;
    }

    /**
     * Get all keys available
     *
     * @return string[]
     */
    public function getKeys()
    {
        $keys = array_keys($this->cache) + array_keys($this->factories);
        return array_unique($keys);
    }

    /**
     * Build a value
     *
     * @param string $name
     * @return mixed
     * @throws FactoryUncallableException|ValueUnresolvableException|NotFoundException
     */
    private function build($name)
    {
        if (!array_key_exists($name, $this->factories)) {
            throw new NotFoundException("Missing value: $name");
        }

        $factory = $this->factories[$name];

        if (is_callable($factory)) {
            try {
                return call_user_func($factory, $this);
            } catch (\Exception $e) {
                throw new ValueUnresolvableException("Factory for '$name' threw an exception.", 0, $e);
            }
        }

        $msg = "Factory for '$name' was uncallable";
        if (is_string($factory)) {
            $msg .= ": '$factory'";
        } elseif (is_array($factory)) {
            if (is_string($factory[0])) {
                $msg .= ": '{$factory[0]}::{$factory[1]}'";
            } else {
                $msg .= ": " . get_class($factory[0]) . "->{$factory[1]}";
            }
        }
        throw new FactoryUncallableException($msg);
    }
}