Skip to content

Commit

Permalink
Update LayoutParametersInjectionInterface.php
Browse files Browse the repository at this point in the history
reset
  • Loading branch information
TiaNex-Com authored Oct 6, 2024
1 parent aa3df2d commit d50403f
Showing 1 changed file with 16 additions and 43 deletions.
59 changes: 16 additions & 43 deletions src/LayoutParametersInjectionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,52 +4,25 @@

namespace Yiisoft\Yii\View\Renderer;

use LogicException;
use Yiisoft\Csrf\CsrfTokenInterface;
use Yiisoft\Csrf\CsrfTrait;

/**
* `CsrfViewInjection` injects the necessary data into the view to protect against a CSRF attack.
* LayoutParametersInjectionInterface is an interface that must be implemented by classes to inject layout parameters.
*/
final class CsrfViewInjection implements CsrfParametersInjectionInterface
interface LayoutParametersInjectionInterface
{
use CsrfTrait;

public const DEFAULT_META_ATTRIBUTE_NAME = 'csrf';
public const DEFAULT_PARAMETER_NAME = 'csrf';
public const META_TAG_KEY = 'csrf';

private string $metaAttributeName = self::DEFAULT_META_ATTRIBUTE_NAME;
private string $parameterName = self::DEFAULT_PARAMETER_NAME;

public function __construct(private CsrfTokenInterface $token)
{
}


/**
* @throws LogicException when CSRF token is not defined
* Returns parameters for added to layout.
*
* For example:
*
* ```
* [
* 'paramA' => 'something',
* 'paramB' => 42,
* ...
* ]
* ```
*
* @psalm-return array<string, mixed>
*/
public function getCsrfParameters(): array
{
$tokenValue = $this->token->getValue();
$csrf = new Csrf(
$tokenValue,
$this->getFormParameterName(),
$this->getHeaderName(),
);
return [
[
$this->parameterName => $csrf
],
[
self::META_TAG_KEY => [
'name' => $this->metaAttributeName,
'content' => $tokenValue
],
],
];
}


public function getLayoutParameters(): array;
}

0 comments on commit d50403f

Please sign in to comment.