Skip to content

Commit

Permalink
added PHP 8 attributes Persistent & CrossOrigin
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Nov 2, 2020
1 parent 6bc88c8 commit d247113
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 2 deletions.
18 changes: 18 additions & 0 deletions src/Application/Attributes/CrossOrigin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

/**
* This file is part of the Nette Framework (https://nette.org)
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
*/

declare(strict_types=1);

namespace Nette\Application\Attributes;

use Attribute;


#[Attribute(Attribute::TARGET_METHOD)]
class CrossOrigin
{
}
18 changes: 18 additions & 0 deletions src/Application/Attributes/Persistent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

/**
* This file is part of the Nette Framework (https://nette.org)
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
*/

declare(strict_types=1);

namespace Nette\Application\Attributes;

use Attribute;


#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_CLASS)]
class Persistent
{
}
1 change: 1 addition & 0 deletions src/Application/UI/Component.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ public function checkRequirements($element): void
$element instanceof \ReflectionMethod
&& substr($element->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();
Expand Down
5 changes: 4 additions & 1 deletion src/Application/UI/ComponentReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
8 changes: 7 additions & 1 deletion src/Application/UI/Presenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}


Expand Down
41 changes: 41 additions & 0 deletions tests/UI/Presenter.getPersistentComponents.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

/**
* Test: Nette\Application\UI\Presenter::getPersistentComponents
*/

declare(strict_types=1);

use Nette\Application\UI\Presenter;
use Tester\Assert;


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


class OnePresenter extends Presenter
{
}


/**
* @persistent(a, b)
*/
class TwoPresenter extends Presenter
{
}


#[\Nette\Application\Attributes\Persistent('a', 'b')]
class ThreePresenter extends Presenter
{
}


Assert::same([], OnePresenter::getPersistentComponents());

Assert::same(['a', 'b'], TwoPresenter::getPersistentComponents());

if (PHP_VERSION_ID >= 80000) {
Assert::same(['a', 'b'], ThreePresenter::getPersistentComponents());
}
13 changes: 13 additions & 0 deletions tests/UI/Presenter.link().persistent.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand All @@ -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');

Expand Down

0 comments on commit d247113

Please sign in to comment.