1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
/** |
4
|
|
|
* Contains trait ContainerArrayAccessTrait. |
5
|
|
|
* |
6
|
|
|
* PHP version 7.0+ |
7
|
|
|
* |
8
|
|
|
* LICENSE: |
9
|
|
|
* This file is part of Yet Another Php Eve Api Library also know as Yapeal |
10
|
|
|
* which can be used to access the Eve Online API data and place it into a |
11
|
|
|
* database. |
12
|
|
|
* Copyright (C) 2016 Michael Cummings |
13
|
|
|
* |
14
|
|
|
* This program is free software: you can redistribute it and/or modify it |
15
|
|
|
* under the terms of the GNU Lesser General Public License as published by the |
16
|
|
|
* Free Software Foundation, either version 3 of the License, or (at your |
17
|
|
|
* option) any later version. |
18
|
|
|
* |
19
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT |
20
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
21
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License |
22
|
|
|
* for more details. |
23
|
|
|
* |
24
|
|
|
* You should have received a copy of the GNU Lesser General Public License |
25
|
|
|
* along with this program. If not, see |
26
|
|
|
* <http://spdx.org/licenses/LGPL-3.0.html>. |
27
|
|
|
* |
28
|
|
|
* You should be able to find a copy of this license in the COPYING-LESSER.md |
29
|
|
|
* file. A copy of the GNU GPL should also be available in the COPYING.md file. |
30
|
|
|
* |
31
|
|
|
* @author Michael Cummings <[email protected]> |
32
|
|
|
* @copyright 2016 Michael Cummings |
33
|
|
|
* @license LGPL-3.0+ |
34
|
|
|
*/ |
35
|
|
|
namespace Yapeal\Container; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Trait ContainerArrayAccessTrait. |
39
|
|
|
* |
40
|
|
|
* @property array $values |
41
|
|
|
* @property \SplObjectStorage $factories |
42
|
|
|
* @property bool[] $frozen |
43
|
|
|
* @property bool[] $keys |
44
|
|
|
* @property \SplObjectStorage $protected |
45
|
|
|
* @property mixed[] $raw |
46
|
|
|
*/ |
47
|
|
|
trait ContainerArrayAccessTrait |
48
|
|
|
{ |
49
|
|
|
/** |
50
|
|
|
* Checks if a parameter or an object is set. |
51
|
|
|
* |
52
|
|
|
* @param string|int $id The unique identifier for the parameter or object |
53
|
|
|
* |
54
|
|
|
* @return bool |
55
|
|
|
*/ |
56
|
|
|
public function offsetExists($id): bool |
57
|
|
|
{ |
58
|
|
|
return array_key_exists($id, $this->keys); |
59
|
|
|
} |
60
|
|
|
/** |
61
|
|
|
* Gets a parameter or an object. |
62
|
|
|
* |
63
|
|
|
* @param string|int $id The unique identifier for the parameter or object |
64
|
|
|
* |
65
|
|
|
* @return mixed The value of the parameter or an object |
66
|
|
|
* |
67
|
|
|
* @throws \InvalidArgumentException if the identifier is not defined |
68
|
|
|
*/ |
69
|
|
|
public function offsetGet($id) |
70
|
|
|
{ |
71
|
|
|
if (!$this->offsetExists($id)) { |
72
|
|
|
throw new \InvalidArgumentException(sprintf('Identifier "%s" is not defined.', $id)); |
73
|
|
|
} |
74
|
|
|
if (array_key_exists($id, $this->raw) |
75
|
|
|
|| !is_object($this->values[$id]) |
76
|
|
|
|| isset($this->protected[$this->values[$id]]) |
77
|
|
|
|| !method_exists($this->values[$id], '__invoke') |
78
|
|
|
) { |
79
|
|
|
return $this->values[$id]; |
80
|
|
|
} |
81
|
|
|
if (isset($this->factories[$this->values[$id]])) { |
82
|
|
|
return $this->values[$id]($this); |
83
|
|
|
} |
84
|
|
|
$raw = $this->values[$id]; |
85
|
|
|
$val = $this->values[$id] = $raw($this); |
86
|
|
|
$this->raw[$id] = $raw; |
87
|
|
|
$this->frozen[$id] = true; |
88
|
|
|
return $val; |
89
|
|
|
} |
90
|
|
|
/** |
91
|
|
|
* Sets a parameter or an object. |
92
|
|
|
* |
93
|
|
|
* Objects must be defined as Closures. |
94
|
|
|
* |
95
|
|
|
* Allowing any PHP callable leads to difficult to debug problems |
96
|
|
|
* as function names (strings) are callable (creating a function with |
97
|
|
|
* the same name as an existing parameter would break your container). |
98
|
|
|
* |
99
|
|
|
* @param string|int $id The unique identifier for the parameter or object |
100
|
|
|
* @param mixed $value The value of the parameter or a closure to define an object |
101
|
|
|
* |
102
|
|
|
* @throws \RuntimeException Prevent override of a frozen service |
103
|
|
|
*/ |
104
|
|
|
public function offsetSet($id, $value) |
105
|
|
|
{ |
106
|
|
|
if (array_key_exists($id, $this->frozen)) { |
107
|
|
|
throw new \RuntimeException(sprintf('Cannot override frozen service "%s".', $id)); |
108
|
|
|
} |
109
|
|
|
$this->values[$id] = $value; |
110
|
|
|
$this->keys[$id] = true; |
111
|
|
|
} |
112
|
|
|
/** |
113
|
|
|
* Un-sets a parameter or an object. |
114
|
|
|
* |
115
|
|
|
* @param string|int $id The unique identifier for the parameter or object |
116
|
|
|
*/ |
117
|
|
|
public function offsetUnset($id) |
118
|
|
|
{ |
119
|
|
|
if ($this->offsetExists($id)) { |
120
|
|
|
if (is_object($this->values[$id])) { |
121
|
|
|
unset($this->factories[$this->values[$id]], $this->protected[$this->values[$id]]); |
122
|
|
|
} |
123
|
|
|
unset($this->values[$id], $this->frozen[$id], $this->raw[$id], $this->keys[$id]); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|