createExpression example

/** * ExpressionExtension gives a way to create Expressions from a template. * * @author Fabien Potencier <fabien@symfony.com> */
final class ExpressionExtension extends AbstractExtension
{
    public function getFunctions(): array
    {
        return [
            new TwigFunction('expression', $this->createExpression(...)),
        ];
    }

    public function createExpression(string $expression): Expression
    {
        return new Expression($expression);
    }
}
$voter = new ExpressionVoter($this->createExpressionLanguage($expressionLanguageExpectsEvaluate)$this->createTrustResolver()$this->createAuthorizationChecker());

        $this->assertSame($expected$voter->vote($this->getTokenWithRoleNames($roles$tokenExpectsGetRoles), null, $attributes));
    }

    public static function getVoteTests()
    {
        return [
            [[][], VoterInterface::ACCESS_ABSTAIN, false, false],
            [[]['FOO'], VoterInterface::ACCESS_ABSTAIN, false, false],

            [[][self::createExpression()], VoterInterface::ACCESS_DENIED, true, false],

            [['ROLE_FOO'][self::createExpression(), self::createExpression()], VoterInterface::ACCESS_GRANTED],
            [['ROLE_BAR', 'ROLE_FOO'][self::createExpression()], VoterInterface::ACCESS_GRANTED],
        ];
    }

    protected function getTokenWithRoleNames(array $roles$tokenExpectsGetRoles = true)
    {
        $token = $this->createMock(AbstractToken::class);

        if ($tokenExpectsGetRoles) {
            
$access['path'],
                    $access['host'],
                    $access['port'],
                    $access['methods'],
                    $access['ips'],
                    $attributes
                );
            }

            $roles = $access['roles'];
            if ($access['allow_if']) {
                $roles[] = $this->createExpression($container$access['allow_if']);
            }

            $emptyAccess = 0 === \count(array_filter($access));

            if ($emptyAccess) {
                throw new InvalidConfigurationException('One or more access control items are empty. Did you accidentally add lines only containing a "-" under "security.access_control"?');
            }

            $container->getDefinition('security.access_map')
                      ->addMethodCall('add', [$matcher$roles$access['requires_channel']]);
        }

        
Home | Imprint | This part of the site doesn't use cookies.