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

nette / component-model / 10276051981

07 Aug 2024 12:37AM UTC coverage: 85.514%. Remained the same
10276051981

push

github

dg
removed attached() & detached() methods (BC break)

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

14 existing lines in 1 file now uncovered.

183 of 214 relevant lines covered (85.51%)

0.86 hits per line

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

85.29
/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

14

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

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

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

33

34
        /**
35
         * Finds the closest ancestor of specified type.
36
         * @param  bool  $throw   throw exception if component doesn't exist?
37
         * @return ($throw is true ? IComponent : ?IComponent)
38
         */
39
        final public function lookup(?string $type, bool $throw = true): ?IComponent
1✔
40
        {
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✔
UNCOV
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✔
UNCOV
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::NAME_SEPARATOR.
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 (!$attached && !$detached) {
1✔
NEW
96
                        throw new Nette\InvalidStateException('At least one handler is required.');
×
97
                }
98

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

107
                $this->monitors[$type][3][] = [$attached, $detached]; // mark as monitored
1✔
108
        }
1✔
109

110

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

119

120
        /********************* interface IComponent ****************d*g**/
121

122

123
        final public function getName(): ?string
124
        {
125
                return $this->name;
1✔
126
        }
127

128

129
        /**
130
         * Returns the parent container if any.
131
         * @return T
132
         */
133
        final public function getParent(): ?IContainer
134
        {
135
                return $this->parent;
1✔
136
        }
137

138

139
        /**
140
         * Sets or removes the parent of this component. This method is managed by containers and should
141
         * not be called by applications
142
         * @param  T  $parent
143
         * @throws Nette\InvalidStateException
144
         * @internal
145
         */
146
        public function setParent(?IContainer $parent, ?string $name = null): static
1✔
147
        {
148
                if ($parent === null && $this->parent === null && $name !== null) {
1✔
UNCOV
149
                        $this->name = $name; // just rename
×
UNCOV
150
                        return $this;
×
151

152
                } elseif ($parent === $this->parent && $name === null) {
1✔
UNCOV
153
                        return $this; // nothing to do
×
154
                }
155

156
                // A component cannot be given a parent if it already has a parent.
157
                if ($this->parent !== null && $parent !== null) {
1✔
158
                        throw new Nette\InvalidStateException("Component '$this->name' already has a parent.");
1✔
159
                }
160

161
                // remove from parent?
162
                if ($parent === null) {
1✔
163
                        $this->refreshMonitors(0);
1✔
164
                        $this->parent = null;
1✔
165

166
                } else { // add to parent
167
                        $this->validateParent($parent);
1✔
168
                        $this->parent = $parent;
1✔
169
                        if ($name !== null) {
1✔
170
                                $this->name = $name;
1✔
171
                        }
172

173
                        $tmp = [];
1✔
174
                        $this->refreshMonitors(0, $tmp);
1✔
175
                }
176

177
                return $this;
1✔
178
        }
179

180

181
        /**
182
         * Validates the new parent before it's set.
183
         * Descendant classes can override this to implement custom validation logic.
184
         * @param  T  $parent
185
         * @throws Nette\InvalidStateException
186
         */
187
        protected function validateParent(IContainer $parent): void
1✔
188
        {
189
        }
1✔
190

191

192
        /**
193
         * Refreshes monitors.
194
         * @param  array<string,true>|null  $missing  (array = attaching, null = detaching)
195
         * @param  array<int,array{callable,IComponent}>  $listeners
196
         */
197
        private function refreshMonitors(int $depth, ?array &$missing = null, array &$listeners = []): void
1✔
198
        {
199
                if ($this instanceof IContainer) {
1✔
200
                        foreach ($this->getComponents() as $component) {
1✔
201
                                if ($component instanceof self) {
1✔
202
                                        $component->refreshMonitors($depth + 1, $missing, $listeners);
1✔
203
                                }
204
                        }
205
                }
206

207
                if ($missing === null) { // detaching
1✔
208
                        foreach ($this->monitors as $type => $rec) {
1✔
209
                                if (isset($rec[1]) && $rec[1] > $depth) {
1✔
210
                                        if ($rec[3]) { // monitored
1✔
211
                                                $this->monitors[$type] = [null, null, null, $rec[3]];
1✔
212
                                                foreach ($rec[3] as $pair) {
1✔
213
                                                        $listeners[] = [$pair[1], $rec[0]];
1✔
214
                                                }
215
                                        } else { // not monitored, just randomly cached
216
                                                unset($this->monitors[$type]);
1✔
217
                                        }
218
                                }
219
                        }
220
                } else { // attaching
221
                        foreach ($this->monitors as $type => $rec) {
1✔
222
                                if (isset($rec[0])) { // is in cache yet
1✔
UNCOV
223
                                        continue;
×
224

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

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

231
                                } else {
232
                                        unset($this->monitors[$type]); // forces re-lookup
1✔
233
                                        if ($obj = $this->lookup($type, throw: false)) {
1✔
234
                                                foreach ($rec[3] as $pair) {
1✔
235
                                                        $listeners[] = [$pair[0], $obj];
1✔
236
                                                }
237
                                        } else {
238
                                                $missing[$type] = true;
1✔
239
                                        }
240

241
                                        $this->monitors[$type][3] = $rec[3]; // mark as monitored
1✔
242
                                }
243
                        }
244
                }
245

246
                if ($depth === 0) { // call listeners
1✔
247
                        $prev = [];
1✔
248
                        foreach ($listeners as $item) {
1✔
249
                                if ($item[0] && !in_array($item, $prev, strict: true)) {
1✔
250
                                        $item[0]($item[1]);
1✔
251
                                        $prev[] = $item;
1✔
252
                                }
253
                        }
254
                }
255
        }
1✔
256

257

258
        /********************* cloneable, serializable ****************d*g**/
259

260

261
        /**
262
         * Object cloning.
263
         */
264
        public function __clone()
265
        {
266
                if ($this->parent === null) {
1✔
UNCOV
267
                        return;
×
268

269
                } elseif ($this->parent instanceof Container) {
1✔
270
                        $this->parent = $this->parent->_isCloning();
1✔
271
                        if ($this->parent === null) { // not cloning
1✔
272
                                $this->refreshMonitors(0);
1✔
273
                        }
274
                } else {
UNCOV
275
                        $this->parent = null;
×
UNCOV
276
                        $this->refreshMonitors(0);
×
277
                }
278
        }
1✔
279

280

281
        /**
282
         * Prevents serialization.
283
         */
284
        final public function __sleep()
285
        {
UNCOV
286
                throw new Nette\NotImplementedException('Object serialization is not supported by class ' . static::class);
×
287
        }
288

289

290
        /**
291
         * Prevents unserialization.
292
         */
293
        final public function __wakeup()
294
        {
UNCOV
295
                throw new Nette\NotImplementedException('Object unserialization is not supported by class ' . static::class);
×
296
        }
297
}
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