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

nette / component-model / 8922521753

02 May 2024 10:39AM UTC coverage: 85.514%. Remained the same
8922521753

push

github

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

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

12 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
 * Component is the base class for all components.
17
 *
18
 * Components are objects implementing IComponent. They has parent component and own name.
19
 *
20
 * @template T of IContainer
21
 * @implements IComponent<T>
22
 * @property-read string $name
23
 * @property-read T|null $parent
24
 */
25
abstract class Component implements IComponent
26
{
27
        use Nette\SmartObject;
28

29
        private ?IContainer $parent = null;
30
        private ?string $name = null;
31

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

35

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

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

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

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

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

76
                return $this->monitors[$type][0];
1✔
77
        }
78

79

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

91

92
        /**
93
         * Starts monitoring of ancestors.
94
         */
95
        final public function monitor(string $type, ?callable $attached = null, ?callable $detached = null): void
1✔
96
        {
97
                if (!$attached && !$detached) {
1✔
NEW
98
                        throw new Nette\InvalidStateException('At least one handler is required.');
×
99
                }
100

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

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

112

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

121

122
        /********************* interface IComponent ****************d*g**/
123

124

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

130

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

140

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

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

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

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

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

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

179
                return $this;
1✔
180
        }
181

182

183
        /**
184
         * Is called by a component when it is about to be set new parent. Descendant can
185
         * override this method to disallow a parent change by throwing an Nette\InvalidStateException
186
         * @param  T  $parent
187
         * @throws Nette\InvalidStateException
188
         */
189
        protected function validateParent(IContainer $parent): void
1✔
190
        {
191
        }
1✔
192

193

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

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

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

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

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

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

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

259

260
        /********************* cloneable, serializable ****************d*g**/
261

262

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

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

282

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

291

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