• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

nette / component-model / 19833976875

01 Dec 2025 06:55PM UTC coverage: 84.793%. Remained the same
19833976875

push

github

dg
monitor() without handlers triggers deprecation notice

0 of 2 new or added lines in 1 file covered. (0.0%)

11 existing lines in 1 file now uncovered.

184 of 217 relevant lines covered (84.79%)

0.85 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

83.81
/src/ComponentModel/Component.php
1
<?php
2

3
/**
4
 * This file is part of the Nette Framework (https://nette.org)
5
 * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
6
 */
7

8
declare(strict_types=1);
9

10
namespace Nette\ComponentModel;
11

12
use Nette;
13
use function func_num_args, in_array, substr;
14

15

16
/**
17
 * Base class for all components. Components have a parent, name, and can be monitored by ancestors.
18
 *
19
 * @property-read string $name
20
 * @property-read IContainer|null $parent
21
 */
22
abstract class Component implements IComponent
23
{
24
        use Nette\SmartObject;
25

26
        private ?IContainer $parent = null;
27
        private ?string $name = null;
28

29
        /** @var array<string, array{?IComponent, ?int, ?string, array<int, array{?callable, ?callable}>}> means [type => [obj, depth, path, [attached, detached]]] */
30
        private array $monitors = [];
31

32

33
        /**
34
         * Finds the closest ancestor of specified type.
35
         * @param  bool  $throw   throw exception if component doesn't exist?
36
         * @return ($throw is true ? IComponent : ?IComponent)
37
         */
38
        final public function lookup(?string $type, bool $throw = true): ?IComponent
1✔
39
        {
40
                $type ??= '';
1✔
41
                if (!isset($this->monitors[$type])) { // not monitored or not processed yet
1✔
42
                        $obj = $this->parent;
1✔
43
                        $path = self::NameSeparator . $this->name;
1✔
44
                        $depth = 1;
1✔
45
                        while ($obj !== null) {
1✔
46
                                $parent = $obj->getParent();
1✔
47
                                if ($type ? $obj instanceof $type : $parent === null) {
1✔
48
                                        break;
1✔
49
                                }
50

51
                                $path = self::NameSeparator . $obj->getName() . $path;
1✔
52
                                $depth++;
1✔
53
                                $obj = $parent; // IComponent::getParent()
1✔
54
                                if ($obj === $this) {
1✔
55
                                        $obj = null; // prevent cycling
×
56
                                }
57
                        }
58

59
                        if ($obj) {
1✔
60
                                $this->monitors[$type] = [$obj, $depth, substr($path, 1), []];
1✔
61

62
                        } else {
63
                                $this->monitors[$type] = [null, null, null, []]; // not found
1✔
64
                        }
65
                }
66

67
                if ($throw && $this->monitors[$type][0] === null) {
1✔
68
                        $message = $this->name !== null
1✔
69
                                ? "Component '$this->name' is not attached to '$type'."
1✔
70
                                : "Component of type '" . static::class . "' is not attached to '$type'.";
×
71
                        throw new Nette\InvalidStateException($message);
1✔
72
                }
73

74
                return $this->monitors[$type][0];
1✔
75
        }
76

77

78
        /**
79
         * Finds the closest ancestor specified by class or interface name and returns backtrace path.
80
         * A path is the concatenation of component names separated by self::NameSeparator.
81
         * @return ($throw is true ? string : ?string)
82
         */
83
        final public function lookupPath(?string $type = null, bool $throw = true): ?string
1✔
84
        {
85
                $this->lookup($type, $throw);
1✔
86
                return $this->monitors[$type ?? ''][2];
1✔
87
        }
88

89

90
        /**
91
         * Starts monitoring ancestors for attach/detach events.
92
         */
93
        final public function monitor(string $type, ?callable $attached = null, ?callable $detached = null): void
1✔
94
        {
95
                if (func_num_args() === 1) {
1✔
NEW
96
                        $class = (new \ReflectionMethod($this, 'attached'))->getDeclaringClass()->getName();
×
NEW
97
                        trigger_error(__METHOD__ . "(): Methods $class::attached() and $class::detached() are deprecated, use monitor(\$type, [attached], [detached])", E_USER_DEPRECATED);
×
98
                        $attached = [$this, 'attached'];
×
99
                        $detached = [$this, 'detached'];
×
100
                }
101

102
                if (
103
                        ($obj = $this->lookup($type, throw: false))
1✔
104
                        && $attached
1✔
105
                        && !in_array([$attached, $detached], $this->monitors[$type][3], strict: true)
1✔
106
                ) {
107
                        $attached($obj);
1✔
108
                }
109

110
                $this->monitors[$type][3][] = [$attached, $detached]; // mark as monitored
1✔
111
        }
1✔
112

113

114
        /**
115
         * Stops monitoring ancestors of specified type.
116
         */
117
        final public function unmonitor(string $type): void
118
        {
UNCOV
119
                unset($this->monitors[$type]);
×
120
        }
121

122

123
        /**
124
         * This method will be called when the component (or component's parent)
125
         * becomes attached to a monitored object. Do not call this method yourself.
126
         * @deprecated  use monitor($type, $attached)
127
         */
128
        protected function attached(IComponent $obj): void
129
        {
130
        }
131

132

133
        /**
134
         * This method will be called before the component (or component's parent)
135
         * becomes detached from a monitored object. Do not call this method yourself.
136
         * @deprecated  use monitor($type, null, $detached)
137
         */
138
        protected function detached(IComponent $obj): void
139
        {
140
        }
141

142

143
        /********************* interface IComponent ****************d*g**/
144

145

146
        final public function getName(): ?string
147
        {
148
                return $this->name;
1✔
149
        }
150

151

152
        /**
153
         * Returns the parent container if any.
154
         */
155
        final public function getParent(): ?IContainer
156
        {
157
                return $this->parent;
1✔
158
        }
159

160

161
        /**
162
         * Sets or removes the parent of this component. This method is managed by containers and should
163
         * not be called by applications
164
         * @throws Nette\InvalidStateException
165
         * @internal
166
         */
167
        public function setParent(?IContainer $parent, ?string $name = null): static
1✔
168
        {
169
                if ($parent === null && $this->parent === null && $name !== null) {
1✔
UNCOV
170
                        $this->name = $name; // just rename
×
UNCOV
171
                        return $this;
×
172

173
                } elseif ($parent === $this->parent && $name === null) {
1✔
UNCOV
174
                        return $this; // nothing to do
×
175
                }
176

177
                // A component cannot be given a parent if it already has a parent.
178
                if ($this->parent !== null && $parent !== null) {
1✔
179
                        throw new Nette\InvalidStateException("Component '$this->name' already has a parent.");
1✔
180
                }
181

182
                // remove from parent?
183
                if ($parent === null) {
1✔
184
                        $this->refreshMonitors(0);
1✔
185
                        $this->parent = null;
1✔
186

187
                } else { // add to parent
188
                        $this->validateParent($parent);
1✔
189
                        $this->parent = $parent;
1✔
190
                        if ($name !== null) {
1✔
191
                                $this->name = $name;
1✔
192
                        }
193

194
                        $tmp = [];
1✔
195
                        $this->refreshMonitors(0, $tmp);
1✔
196
                }
197

198
                return $this;
1✔
199
        }
200

201

202
        /**
203
         * Validates the new parent before it's set.
204
         * Descendant classes can override this to implement custom validation logic.
205
         * @throws Nette\InvalidStateException
206
         */
207
        protected function validateParent(IContainer $parent): void
1✔
208
        {
209
        }
1✔
210

211

212
        /**
213
         * Refreshes monitors.
214
         * @param  array<string,true>|null  $missing  (array = attaching, null = detaching)
215
         * @param  array<int,array{callable,IComponent}>  $listeners
216
         */
217
        private function refreshMonitors(int $depth, ?array &$missing = null, array &$listeners = []): void
1✔
218
        {
219
                if ($this instanceof IContainer) {
1✔
220
                        foreach ($this->getComponents() as $component) {
1✔
221
                                if ($component instanceof self) {
1✔
222
                                        $component->refreshMonitors($depth + 1, $missing, $listeners);
1✔
223
                                }
224
                        }
225
                }
226

227
                if ($missing === null) { // detaching
1✔
228
                        foreach ($this->monitors as $type => $rec) {
1✔
229
                                if (isset($rec[1]) && $rec[1] > $depth) {
1✔
230
                                        if ($rec[3]) { // monitored
1✔
231
                                                $this->monitors[$type] = [null, null, null, $rec[3]];
1✔
232
                                                foreach ($rec[3] as $pair) {
1✔
233
                                                        $listeners[] = [$pair[1], $rec[0]];
1✔
234
                                                }
235
                                        } else { // not monitored, just randomly cached
236
                                                unset($this->monitors[$type]);
1✔
237
                                        }
238
                                }
239
                        }
240
                } else { // attaching
241
                        foreach ($this->monitors as $type => $rec) {
1✔
242
                                if (isset($rec[0])) { // is in cache yet
1✔
UNCOV
243
                                        continue;
×
244

245
                                } elseif (!$rec[3]) { // not monitored, just randomly cached
1✔
UNCOV
246
                                        unset($this->monitors[$type]);
×
247

248
                                } elseif (isset($missing[$type])) { // known from previous lookup
1✔
UNCOV
249
                                        $this->monitors[$type] = [null, null, null, $rec[3]];
×
250

251
                                } else {
252
                                        unset($this->monitors[$type]); // forces re-lookup
1✔
253
                                        if ($obj = $this->lookup($type, throw: false)) {
1✔
254
                                                foreach ($rec[3] as $pair) {
1✔
255
                                                        $listeners[] = [$pair[0], $obj];
1✔
256
                                                }
257
                                        } else {
258
                                                $missing[$type] = true;
1✔
259
                                        }
260

261
                                        $this->monitors[$type][3] = $rec[3]; // mark as monitored
1✔
262
                                }
263
                        }
264
                }
265

266
                if ($depth === 0) { // call listeners
1✔
267
                        $prev = [];
1✔
268
                        foreach ($listeners as $item) {
1✔
269
                                if ($item[0] && !in_array($item, $prev, strict: true)) {
1✔
270
                                        $item[0]($item[1]);
1✔
271
                                        $prev[] = $item;
1✔
272
                                }
273
                        }
274
                }
275
        }
1✔
276

277

278
        /********************* cloneable, serializable ****************d*g**/
279

280

281
        /**
282
         * Object cloning.
283
         */
284
        public function __clone()
285
        {
286
                if ($this->parent === null) {
1✔
UNCOV
287
                        return;
×
288

289
                } elseif ($this->parent instanceof Container) {
1✔
290
                        $this->parent = $this->parent->_isCloning();
1✔
291
                        if ($this->parent === null) { // not cloning
1✔
292
                                $this->refreshMonitors(0);
1✔
293
                        }
294
                } else {
UNCOV
295
                        $this->parent = null;
×
UNCOV
296
                        $this->refreshMonitors(0);
×
297
                }
298
        }
1✔
299

300

301
        /**
302
         * Prevents serialization.
303
         */
304
        final public function __serialize()
305
        {
UNCOV
306
                throw new Nette\NotImplementedException('Object serialization is not supported by class ' . static::class);
×
307
        }
308
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc