Skip to content

Commit 5dc023d

Browse files
committed
Presenter: persistent parameters are transmitted between traits (in addition to the class hierarchy) [Closes #183]
1 parent 7765c92 commit 5dc023d

3 files changed

Lines changed: 23 additions & 11 deletions

File tree

src/Application/UI/ComponentReflection.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function getPersistentParams(string $class = null): array
5252
if (!$rp->isStatic() && self::parseAnnotation($rp, 'persistent')) {
5353
$params[$name] = [
5454
'def' => $default,
55-
'since' => $isPresenter ? $class : null,
55+
'since' => $isPresenter ? Nette\Utils\Reflection::getPropertyDeclaringClass($rp)->getName() : null,
5656
];
5757
}
5858
}
@@ -94,13 +94,14 @@ public function getPersistentComponents(string $class = null): array
9494
*/
9595
public function saveState(Component $component, array &$params): void
9696
{
97+
$traits = array_flip(Nette\Utils\Reflection::getAllTraits(new \ReflectionClass($component)));
9798
foreach ($this->getPersistentParams() as $name => $meta) {
9899
if (isset($params[$name])) {
99100
// injected value
100101

101102
} elseif (
102103
array_key_exists($name, $params) // nulls are skipped
103-
|| (isset($meta['since']) && !$component instanceof $meta['since']) // not related
104+
|| (isset($meta['since']) && !$component instanceof $meta['since'] && !isset($traits[$meta['since']])) // not related
104105
|| !isset($component->$name)
105106
) {
106107
continue;

src/Application/UI/Presenter.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1100,6 +1100,17 @@ protected function getGlobalState(string $forClass = null): array
11001100
}
11011101

11021102
if ($forClass !== null) {
1103+
$tree = [$forClass => $forClass] + class_parents($forClass);
1104+
$addTraits = function ($type) use (&$tree, &$addTraits) {
1105+
$tree += class_uses($type);
1106+
foreach (class_uses($type) as $trait) {
1107+
$addTraits($trait);
1108+
}
1109+
};
1110+
foreach ($tree as $class) {
1111+
$addTraits($class);
1112+
}
1113+
11031114
$since = null;
11041115
foreach ($state as $key => $foo) {
11051116
if (!isset($sinces[$key])) {
@@ -1109,7 +1120,7 @@ protected function getGlobalState(string $forClass = null): array
11091120
}
11101121
if ($since !== $sinces[$key]) {
11111122
$since = $sinces[$key];
1112-
$ok = $since && is_a($forClass, $since, true);
1123+
$ok = $since && isset($tree[$since]);
11131124
}
11141125
if (!$ok) {
11151126
unset($state[$key]);

tests/UI/Presenter.link().persistent.phpt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class TestPresenter extends BasePresenter
6464
$this->t2 = 4;
6565
Assert::same('/index.php?p2=2&p1=1&t1=3&t2=4&action=default&presenter=Test', $this->link('this'));
6666
Assert::same('/index.php?p1=1&t1=3&action=default&presenter=Second', $this->link('Second:'));
67-
Assert::same('/index.php?p1=1&t1=3&action=default&presenter=Third', $this->link('Third:'));
67+
Assert::same('/index.php?p1=1&t1=3&t2=4&action=default&presenter=Third', $this->link('Third:'));
6868

6969
$this->p1 = 20;
7070
Assert::same('/index.php?t1=3&action=default&presenter=Second', $this->link('Second:'));
@@ -97,27 +97,27 @@ class ThirdPresenter extends BasePresenter
9797

9898
Assert::same([
9999
'p1' => ['def' => null, 'since' => 'BasePresenter'],
100-
't1' => ['def' => null, 'since' => 'BasePresenter'],
100+
't1' => ['def' => null, 'since' => 'PersistentParam1'],
101101
], BasePresenter::getReflection()->getPersistentParams());
102102

103103
Assert::same([
104104
'p2' => ['def' => null, 'since' => 'TestPresenter'],
105105
'p1' => ['def' => null, 'since' => 'BasePresenter'],
106-
't1' => ['def' => null, 'since' => 'BasePresenter'],
107-
't2' => ['def' => null, 'since' => 'TestPresenter'],
106+
't1' => ['def' => null, 'since' => 'PersistentParam1'],
107+
't2' => ['def' => null, 'since' => 'PersistentParam2A'],
108108
], TestPresenter::getReflection()->getPersistentParams());
109109

110110
Assert::same([
111111
'p1' => ['def' => 20, 'since' => 'BasePresenter'],
112112
'p3' => ['def' => null, 'since' => 'SecondPresenter'],
113-
't1' => ['def' => null, 'since' => 'BasePresenter'],
114-
't3' => ['def' => null, 'since' => 'SecondPresenter'],
113+
't1' => ['def' => null, 'since' => 'PersistentParam1'],
114+
't3' => ['def' => null, 'since' => 'PersistentParam3'],
115115
], SecondPresenter::getReflection()->getPersistentParams());
116116

117117
Assert::same([
118118
'p1' => ['def' => null, 'since' => 'BasePresenter'],
119-
't1' => ['def' => null, 'since' => 'BasePresenter'],
120-
't2' => ['def' => null, 'since' => 'ThirdPresenter'],
119+
't1' => ['def' => null, 'since' => 'PersistentParam1'],
120+
't2' => ['def' => null, 'since' => 'PersistentParam2A'],
121121
], ThirdPresenter::getReflection()->getPersistentParams());
122122

123123

0 commit comments

Comments
 (0)