1 | <?php |
||
32 | class Jarvis extends Container implements BroadcasterInterface |
||
33 | { |
||
34 | use BroadcasterTrait; |
||
35 | |||
36 | const DEFAULT_DEBUG = false; |
||
37 | const CONTAINER_PROVIDER_FQCN = ContainerProvider::class; |
||
38 | |||
39 | private $masterSetter = false; |
||
40 | |||
41 | /** |
||
42 | * Creates an instance of Jarvis. It can take settings as first argument. |
||
43 | * List of accepted options: |
||
44 | * - providers (type: string|array): fqcn of your container provider |
||
45 | * - extra |
||
46 | * |
||
47 | * @param array $settings Your own settings to modify Jarvis behavior |
||
48 | */ |
||
49 | public function __construct(array $settings = []) |
||
50 | { |
||
51 | parent::__construct(); |
||
52 | |||
53 | $this['settings'] = $settings; |
||
54 | $providers = array_merge([static::CONTAINER_PROVIDER_FQCN], (array) ($settings['providers'] ?? [])); |
||
55 | foreach (array_unique($providers) as $classname) { |
||
56 | $this->hydrate(new $classname()); |
||
57 | } |
||
58 | } |
||
59 | |||
60 | public function __destruct() |
||
61 | { |
||
62 | $this->masterBroadcast(BroadcasterInterface::TERMINATE_EVENT); |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * This method is an another way to get a locked value. |
||
67 | * |
||
68 | * Example: $this['foo'] is equal to $this->foo, but it ONLY works for locked values. |
||
69 | * |
||
70 | * @param string $key The key of the locked value |
||
71 | * @return mixed |
||
72 | * @throws \InvalidArgumentException if the requested key is not associated to a locked service |
||
73 | */ |
||
74 | public function __get(string $key) |
||
75 | { |
||
76 | if (!isset($this->locked[$key])) { |
||
77 | throw new \InvalidArgumentException(sprintf('"%s" is not a key of a locked value.', $key)); |
||
78 | } |
||
79 | |||
80 | $this->masterSet($key, $this[$key]); |
||
81 | |||
82 | return $this->$key; |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * Sets new attributes to Jarvis. Note that this method is reserved to Jarvis itself only. |
||
87 | * |
||
88 | * @param string $key The key name of the new attribute |
||
89 | * @param mixed $value The value to associate to provided key |
||
90 | * @throws \LogicException if this method is not called by Jarvis itself |
||
91 | */ |
||
92 | public function __set(string $key, $value) |
||
93 | { |
||
94 | if (!$this->masterSetter) { |
||
95 | throw new \LogicException('You are not allowed to set new attribute into Jarvis.'); |
||
96 | } |
||
97 | |||
98 | $this->$key = $value; |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * {@inheritdoc} |
||
103 | */ |
||
104 | public function offsetSet($id, $v): void |
||
105 | { |
||
106 | parent::offsetSet($id, $v); |
||
107 | |||
108 | if (!($v instanceof \Closure)) { |
||
109 | return; |
||
110 | } |
||
111 | |||
112 | $refMethod = new \ReflectionMethod($v, '__invoke'); |
||
113 | if (null === $returntype = $refMethod->getReturnType()) { |
||
114 | return; |
||
115 | } |
||
116 | |||
117 | $alias = $returntype->getName(); |
||
118 | if ( |
||
119 | $alias === $id |
||
120 | || (!class_exists($alias) && !interface_exists($alias)) |
||
121 | ) { |
||
122 | return; |
||
123 | } |
||
124 | |||
125 | if (!isset($this[$alias])) { |
||
126 | $this->alias($alias, $id); |
||
127 | } else { |
||
128 | unset($this[$alias]); |
||
129 | } |
||
130 | } |
||
131 | |||
132 | /** |
||
133 | * @param Request|null $request |
||
134 | * @return Response |
||
135 | */ |
||
136 | public function run(Request $request = null): Response |
||
161 | |||
162 | /** |
||
163 | * Sets new attribute into Jarvis. |
||
164 | * |
||
165 | * @param string $key The name of the new attribute |
||
166 | * @param mixed $value The value of the new attribute |
||
167 | * @return self |
||
168 | */ |
||
169 | private function masterSet(string $key, $value): void |
||
175 | } |
||
176 |
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.