Skip to content

Commit d247113

Browse files
committed
added PHP 8 attributes Persistent & CrossOrigin
1 parent 6bc88c8 commit d247113

7 files changed

Lines changed: 102 additions & 2 deletions

File tree

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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\Application\Attributes;
11+
12+
use Attribute;
13+
14+
15+
#[Attribute(Attribute::TARGET_METHOD)]
16+
class CrossOrigin
17+
{
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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\Application\Attributes;
11+
12+
use Attribute;
13+
14+
15+
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_CLASS)]
16+
class Persistent
17+
{
18+
}

src/Application/UI/Component.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ public function checkRequirements($element): void
116116
$element instanceof \ReflectionMethod
117117
&& substr($element->getName(), 0, 6) === 'handle'
118118
&& !ComponentReflection::parseAnnotation($element, 'crossOrigin')
119+
&& (PHP_VERSION_ID < 80000 || !$element->getAttributes(Nette\Application\Attributes\CrossOrigin::class))
119120
&& !$this->getPresenter()->getHttpRequest()->isSameSite()
120121
) {
121122
$this->getPresenter()->detectedCsrf();

src/Application/UI/ComponentReflection.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,10 @@ public function getPersistentParams(string $class = null): array
4949
$defaults = get_class_vars($class);
5050
foreach ($defaults as $name => $default) {
5151
$rp = new \ReflectionProperty($class, $name);
52-
if (!$rp->isStatic() && self::parseAnnotation($rp, 'persistent')) {
52+
if (!$rp->isStatic()
53+
&& ((PHP_VERSION_ID >= 80000 && $rp->getAttributes(Nette\Application\Attributes\Persistent::class))
54+
|| self::parseAnnotation($rp, 'persistent'))
55+
) {
5356
$params[$name] = [
5457
'def' => $default,
5558
'type' => Nette\Utils\Reflection::getPropertyType($rp) ?: gettype($default),

src/Application/UI/Presenter.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1118,7 +1118,13 @@ public function restoreRequest(string $key): void
11181118
*/
11191119
public static function getPersistentComponents(): array
11201120
{
1121-
return (array) ComponentReflection::parseAnnotation(new \ReflectionClass(static::class), 'persistent');
1121+
$rc = new \ReflectionClass(static::class);
1122+
$attrs = PHP_VERSION_ID >= 80000
1123+
? $rc->getAttributes(Application\Attributes\Persistent::class)
1124+
: null;
1125+
return $attrs
1126+
? $attrs[0]->getArguments()
1127+
: (array) ComponentReflection::parseAnnotation($rc, 'persistent');
11221128
}
11231129

11241130

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
/**
4+
* Test: Nette\Application\UI\Presenter::getPersistentComponents
5+
*/
6+
7+
declare(strict_types=1);
8+
9+
use Nette\Application\UI\Presenter;
10+
use Tester\Assert;
11+
12+
13+
require __DIR__ . '/../bootstrap.php';
14+
15+
16+
class OnePresenter extends Presenter
17+
{
18+
}
19+
20+
21+
/**
22+
* @persistent(a, b)
23+
*/
24+
class TwoPresenter extends Presenter
25+
{
26+
}
27+
28+
29+
#[\Nette\Application\Attributes\Persistent('a', 'b')]
30+
class ThreePresenter extends Presenter
31+
{
32+
}
33+
34+
35+
Assert::same([], OnePresenter::getPersistentComponents());
36+
37+
Assert::same(['a', 'b'], TwoPresenter::getPersistentComponents());
38+
39+
if (PHP_VERSION_ID >= 80000) {
40+
Assert::same(['a', 'b'], ThreePresenter::getPersistentComponents());
41+
}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,13 @@ class ThirdPresenter extends BasePresenter
9595
}
9696

9797

98+
class FourthPresenter extends BasePresenter
99+
{
100+
#[Application\Attributes\Persistent]
101+
public $p1;
102+
}
103+
104+
98105
Assert::same([
99106
'p1' => ['def' => null, 'type' => 'NULL', 'since' => 'BasePresenter'],
100107
't1' => ['def' => null, 'type' => 'NULL', 'since' => 'PersistentParam1'],
@@ -120,6 +127,12 @@ Assert::same([
120127
't2' => ['def' => null, 'type' => 'NULL', 'since' => 'PersistentParam2A'],
121128
], ThirdPresenter::getReflection()->getPersistentParams());
122129

130+
if (PHP_VERSION_ID >= 80000) {
131+
Assert::same([
132+
'p1' => ['def' => null, 'type' => 'NULL', 'since' => 'BasePresenter'],
133+
't1' => ['def' => null, 'type' => 'NULL', 'since' => 'PersistentParam1'],
134+
], FourthPresenter::getReflection()->getPersistentParams());
135+
}
123136

124137
$url = new Http\UrlScript('http://localhost/index.php', '/index.php');
125138

0 commit comments

Comments
 (0)