From d2471134ed909210de8a3e8559931902b1bee67b Mon Sep 17 00:00:00 2001 From: David Grudl Date: Fri, 23 Oct 2020 14:54:16 +0200 Subject: [PATCH] added PHP 8 attributes Persistent & CrossOrigin --- src/Application/Attributes/CrossOrigin.php | 18 ++++++++ src/Application/Attributes/Persistent.php | 18 ++++++++ src/Application/UI/Component.php | 1 + src/Application/UI/ComponentReflection.php | 5 ++- src/Application/UI/Presenter.php | 8 +++- .../UI/Presenter.getPersistentComponents.phpt | 41 +++++++++++++++++++ tests/UI/Presenter.link().persistent.phpt | 13 ++++++ 7 files changed, 102 insertions(+), 2 deletions(-) create mode 100644 src/Application/Attributes/CrossOrigin.php create mode 100644 src/Application/Attributes/Persistent.php create mode 100644 tests/UI/Presenter.getPersistentComponents.phpt diff --git a/src/Application/Attributes/CrossOrigin.php b/src/Application/Attributes/CrossOrigin.php new file mode 100644 index 000000000..6ddf9e4dd --- /dev/null +++ b/src/Application/Attributes/CrossOrigin.php @@ -0,0 +1,18 @@ +getName(), 0, 6) === 'handle' && !ComponentReflection::parseAnnotation($element, 'crossOrigin') + && (PHP_VERSION_ID < 80000 || !$element->getAttributes(Nette\Application\Attributes\CrossOrigin::class)) && !$this->getPresenter()->getHttpRequest()->isSameSite() ) { $this->getPresenter()->detectedCsrf(); diff --git a/src/Application/UI/ComponentReflection.php b/src/Application/UI/ComponentReflection.php index df74becfc..8a7270f26 100644 --- a/src/Application/UI/ComponentReflection.php +++ b/src/Application/UI/ComponentReflection.php @@ -49,7 +49,10 @@ public function getPersistentParams(string $class = null): array $defaults = get_class_vars($class); foreach ($defaults as $name => $default) { $rp = new \ReflectionProperty($class, $name); - if (!$rp->isStatic() && self::parseAnnotation($rp, 'persistent')) { + if (!$rp->isStatic() + && ((PHP_VERSION_ID >= 80000 && $rp->getAttributes(Nette\Application\Attributes\Persistent::class)) + || self::parseAnnotation($rp, 'persistent')) + ) { $params[$name] = [ 'def' => $default, 'type' => Nette\Utils\Reflection::getPropertyType($rp) ?: gettype($default), diff --git a/src/Application/UI/Presenter.php b/src/Application/UI/Presenter.php index 48c043fa4..f3a12f4dc 100644 --- a/src/Application/UI/Presenter.php +++ b/src/Application/UI/Presenter.php @@ -1118,7 +1118,13 @@ public function restoreRequest(string $key): void */ public static function getPersistentComponents(): array { - return (array) ComponentReflection::parseAnnotation(new \ReflectionClass(static::class), 'persistent'); + $rc = new \ReflectionClass(static::class); + $attrs = PHP_VERSION_ID >= 80000 + ? $rc->getAttributes(Application\Attributes\Persistent::class) + : null; + return $attrs + ? $attrs[0]->getArguments() + : (array) ComponentReflection::parseAnnotation($rc, 'persistent'); } diff --git a/tests/UI/Presenter.getPersistentComponents.phpt b/tests/UI/Presenter.getPersistentComponents.phpt new file mode 100644 index 000000000..773e680f4 --- /dev/null +++ b/tests/UI/Presenter.getPersistentComponents.phpt @@ -0,0 +1,41 @@ += 80000) { + Assert::same(['a', 'b'], ThreePresenter::getPersistentComponents()); +} diff --git a/tests/UI/Presenter.link().persistent.phpt b/tests/UI/Presenter.link().persistent.phpt index 7e25ebb12..bc2fb9515 100644 --- a/tests/UI/Presenter.link().persistent.phpt +++ b/tests/UI/Presenter.link().persistent.phpt @@ -95,6 +95,13 @@ class ThirdPresenter extends BasePresenter } +class FourthPresenter extends BasePresenter +{ + #[Application\Attributes\Persistent] + public $p1; +} + + Assert::same([ 'p1' => ['def' => null, 'type' => 'NULL', 'since' => 'BasePresenter'], 't1' => ['def' => null, 'type' => 'NULL', 'since' => 'PersistentParam1'], @@ -120,6 +127,12 @@ Assert::same([ 't2' => ['def' => null, 'type' => 'NULL', 'since' => 'PersistentParam2A'], ], ThirdPresenter::getReflection()->getPersistentParams()); +if (PHP_VERSION_ID >= 80000) { + Assert::same([ + 'p1' => ['def' => null, 'type' => 'NULL', 'since' => 'BasePresenter'], + 't1' => ['def' => null, 'type' => 'NULL', 'since' => 'PersistentParam1'], + ], FourthPresenter::getReflection()->getPersistentParams()); +} $url = new Http\UrlScript('http://localhost/index.php', '/index.php');