Skip to content

Commit

Permalink
monitor() without handlers triggers deprecation notice
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Dec 10, 2023
1 parent d1bf418 commit c38028d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 32 deletions.
1 change: 1 addition & 0 deletions src/ComponentModel/Component.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ final public function lookupPath(?string $type = null, bool $throw = true): ?str
final public function monitor(string $type, ?callable $attached = null, ?callable $detached = null): void
{
if (func_num_args() === 1) {
trigger_error('The attached() and detached() methods are deprecated, use the second and third arguments of the monitor() method instead.', E_USER_DEPRECATED);
$attached = [$this, 'attached'];
$detached = [$this, 'detached'];
}
Expand Down
29 changes: 12 additions & 17 deletions tests/ComponentModel/Container.attached.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,18 @@ use Nette\ComponentModel\Container;
use Nette\ComponentModel\IComponent;
use Tester\Assert;


require __DIR__ . '/../bootstrap.php';


class TestClass extends Container implements ArrayAccess
function handler(IComponent $sender, string $label): Closure
{
use Nette\ComponentModel\ArrayAccess;

public function attached(IComponent $obj): void
{
Notes::add(static::class . '::ATTACHED(' . $obj::class . ')');
}
return fn(IComponent $obj) => Notes::add($sender::class . '::' . $label . '(' . $obj::class . ')');
}


public function detached(IComponent $obj): void
{
Notes::add(static::class . '::detached(' . $obj::class . ')');
}
class TestClass extends Container implements ArrayAccess
{
use Nette\ComponentModel\ArrayAccess;
}


Expand All @@ -47,12 +41,13 @@ class E extends TestClass
{
}


$d = new D;
$d['e'] = new E;
$b = new B;
$b->monitor(A::class);
$b->monitor(A::class, handler($b, 'ATTACHED'), handler($b, 'detached'));
$b['c'] = new C;
$b['c']->monitor(A::class);
$b['c']->monitor(A::class, handler($b['c'], 'ATTACHED'), handler($b['c'], 'detached'));
$b['c']['d'] = $d;

// 'a' becoming 'b' parent
Expand Down Expand Up @@ -90,7 +85,7 @@ class FooForm extends TestClass
protected function validateParent(Nette\ComponentModel\IContainer $parent): void
{
parent::validateParent($parent);
$this->monitor(self::class);
$this->monitor(self::class, handler($this, 'ATTACHED'), handler($this, 'detached'));
}
}

Expand All @@ -99,8 +94,8 @@ class FooControl extends TestClass
protected function validateParent(Nette\ComponentModel\IContainer $parent): void
{
parent::validateParent($parent);
$this->monitor(FooPresenter::class);
$this->monitor(TestClass::class); // double
$this->monitor(FooPresenter::class, $h1 = handler($this, 'ATTACHED'), $h2 = handler($this, 'detached'));
$this->monitor(TestClass::class, $h1, $h2); // double
}
}

Expand Down
23 changes: 8 additions & 15 deletions tests/ComponentModel/Container.clone.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,18 @@ use Nette\ComponentModel\IComponent;
use Nette\ComponentModel\IContainer;
use Tester\Assert;


require __DIR__ . '/../bootstrap.php';


class TestClass extends Container implements ArrayAccess
function handler(IComponent $sender, string $label): Closure
{
use Nette\ComponentModel\ArrayAccess;

public function attached(IComponent $obj): void
{
Notes::add(static::class . '::ATTACHED(' . $obj::class . ')');
}
return fn(IComponent $obj) => Notes::add($sender::class . '::' . $label . '(' . $obj::class . ')');
}


public function detached(IComponent $obj): void
{
Notes::add(static::class . '::detached(' . $obj::class . ')');
}
class TestClass extends Container implements ArrayAccess
{
use Nette\ComponentModel\ArrayAccess;
}


Expand Down Expand Up @@ -67,9 +61,8 @@ $a['b']['c'] = new C;
$a['b']['c']['d'] = new D;
$a['b']['c']['d']['e'] = new E;

$a['b']->monitor(A::class);
$a['b']->monitor(A::class);
$a['b']['c']->monitor(A::class);
$a['b']->monitor(A::class, handler($a['b'], 'ATTACHED'), handler($a['b'], 'detached'));
$a['b']['c']->monitor(A::class, handler($a['b']['c'], 'ATTACHED'), handler($a['b']['c'], 'detached'));

Assert::same([
'B::ATTACHED(A)',
Expand Down

0 comments on commit c38028d

Please sign in to comment.