Skip to content

Commit

Permalink
attached() and detached() are deprecated [WIP]
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Dec 8, 2023
1 parent 4781072 commit 8c43cdd
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 14 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
14 changes: 6 additions & 8 deletions tests/ComponentModel/Container.attached.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,13 @@ class TestClass extends Container implements ArrayAccess
{
use Nette\ComponentModel\ArrayAccess;

public function attached(IComponent $obj): void
public function __construct()
{
Notes::add(static::class . '::ATTACHED(' . $obj::class . ')');
}


public function detached(IComponent $obj): void
{
Notes::add(static::class . '::detached(' . $obj::class . ')');
$this->monitor(
IComponent::class,
fn(IComponent $obj) => Notes::add(static::class . '::ATTACHED(' . $obj::class . ')'),
fn(IComponent $obj) => Notes::add(static::class . '::detached(' . $obj::class . ')'),
);
}
}

Expand Down
8 changes: 2 additions & 6 deletions tests/ComponentModel/Container.clone-new.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,13 @@ class E extends TestClass

function createAttached(IComponent $sender)
{
return function (IComponent $obj) use ($sender) {
Notes::add('ATTACHED(' . $obj::class . ', ' . $sender::class . ')');
};
return fn(IComponent $obj) => Notes::add('ATTACHED(' . $obj::class . ', ' . $sender::class . ')');
}


function createDetached(IComponent $sender)
{
return function (IComponent $obj) use ($sender) {
Notes::add('detached(' . $obj::class . ', ' . $sender::class . ')');
};
return fn(IComponent $obj) => Notes::add('detached(' . $obj::class . ', ' . $sender::class . ')');
}


Expand Down

0 comments on commit 8c43cdd

Please sign in to comment.