Skip to content

Commit da99822

Browse files
committed
monitor() without handlers triggers deprecation notice
1 parent aef2cd8 commit da99822

File tree

3 files changed

+22
-32
lines changed

3 files changed

+22
-32
lines changed

src/ComponentModel/Component.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ final public function lookupPath(?string $type = null, bool $throw = true): ?str
9393
final public function monitor(string $type, ?callable $attached = null, ?callable $detached = null): void
9494
{
9595
if (func_num_args() === 1) {
96+
$class = (new \ReflectionMethod($this, 'attached'))->getDeclaringClass()->getName();
97+
trigger_error(__METHOD__ . "(): Methods $class::attached() and $class::detached() are deprecated, use monitor(\$type, [attached], [detached])", E_USER_DEPRECATED);
9698
$attached = [$this, 'attached'];
9799
$detached = [$this, 'detached'];
98100
}

tests/ComponentModel/Container.attached.phpt

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,18 @@ use Nette\ComponentModel\Container;
1010
use Nette\ComponentModel\IComponent;
1111
use Tester\Assert;
1212

13-
1413
require __DIR__ . '/../bootstrap.php';
1514

1615

17-
class TestClass extends Container implements ArrayAccess
16+
function handler(IComponent $sender, string $label): Closure
1817
{
19-
use Nette\ComponentModel\ArrayAccess;
20-
21-
public function attached(IComponent $obj): void
22-
{
23-
Notes::add(static::class . '::ATTACHED(' . $obj::class . ')');
24-
}
18+
return fn(IComponent $obj) => Notes::add($sender::class . '::' . $label . '(' . $obj::class . ')');
19+
}
2520

2621

27-
public function detached(IComponent $obj): void
28-
{
29-
Notes::add(static::class . '::detached(' . $obj::class . ')');
30-
}
22+
class TestClass extends Container implements ArrayAccess
23+
{
24+
use Nette\ComponentModel\ArrayAccess;
3125
}
3226

3327

@@ -47,12 +41,13 @@ class E extends TestClass
4741
{
4842
}
4943

44+
5045
$d = new D;
5146
$d['e'] = new E;
5247
$b = new B;
53-
$b->monitor(A::class);
48+
$b->monitor(A::class, handler($b, 'ATTACHED'), handler($b, 'detached'));
5449
$b['c'] = new C;
55-
$b['c']->monitor(A::class);
50+
$b['c']->monitor(A::class, handler($b['c'], 'ATTACHED'), handler($b['c'], 'detached'));
5651
$b['c']['d'] = $d;
5752

5853
// 'a' becoming 'b' parent
@@ -90,7 +85,7 @@ class FooForm extends TestClass
9085
protected function validateParent(Nette\ComponentModel\IContainer $parent): void
9186
{
9287
parent::validateParent($parent);
93-
$this->monitor(self::class);
88+
$this->monitor(self::class, handler($this, 'ATTACHED'), handler($this, 'detached'));
9489
}
9590
}
9691

@@ -99,8 +94,8 @@ class FooControl extends TestClass
9994
protected function validateParent(Nette\ComponentModel\IContainer $parent): void
10095
{
10196
parent::validateParent($parent);
102-
$this->monitor(FooPresenter::class);
103-
$this->monitor(TestClass::class); // double
97+
$this->monitor(FooPresenter::class, $h1 = handler($this, 'ATTACHED'), $h2 = handler($this, 'detached'));
98+
$this->monitor(TestClass::class, $h1, $h2); // double
10499
}
105100
}
106101

tests/ComponentModel/Container.clone.phpt

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,18 @@ use Nette\ComponentModel\IComponent;
1111
use Nette\ComponentModel\IContainer;
1212
use Tester\Assert;
1313

14-
1514
require __DIR__ . '/../bootstrap.php';
1615

1716

18-
class TestClass extends Container implements ArrayAccess
17+
function handler(IComponent $sender, string $label): Closure
1918
{
20-
use Nette\ComponentModel\ArrayAccess;
21-
22-
public function attached(IComponent $obj): void
23-
{
24-
Notes::add(static::class . '::ATTACHED(' . $obj::class . ')');
25-
}
19+
return fn(IComponent $obj) => Notes::add($sender::class . '::' . $label . '(' . $obj::class . ')');
20+
}
2621

2722

28-
public function detached(IComponent $obj): void
29-
{
30-
Notes::add(static::class . '::detached(' . $obj::class . ')');
31-
}
23+
class TestClass extends Container implements ArrayAccess
24+
{
25+
use Nette\ComponentModel\ArrayAccess;
3226
}
3327

3428

@@ -67,9 +61,8 @@ $a['b']['c'] = new C;
6761
$a['b']['c']['d'] = new D;
6862
$a['b']['c']['d']['e'] = new E;
6963

70-
$a['b']->monitor(A::class);
71-
$a['b']->monitor(A::class);
72-
$a['b']['c']->monitor(A::class);
64+
$a['b']->monitor(A::class, handler($a['b'], 'ATTACHED'), handler($a['b'], 'detached'));
65+
$a['b']['c']->monitor(A::class, handler($a['b']['c'], 'ATTACHED'), handler($a['b']['c'], 'detached'));
7366

7467
Assert::same([
7568
'B::ATTACHED(A)',

0 commit comments

Comments
 (0)