LaVOZs

The World’s Largest Online Community for Developers

'; symfony - Unable to add global twig - LavOzs.Com

I have an AppAuthAuthenticator, and on onAuthenticationFailure I dispatch an event:

if ($count === self::NOTIFY_ATTEMPTS) {
    $this->eventDispatcher->dispatch(new TooManyWrongLoginAttemptsEvent($user));
}

in TooManyWrongLoginAttemptsEvent I render (with Environment) email message and send it to Bus.

I have TimezoneListener:

namespace App\EventListener;

use App\DoctrineDBALTypes\UTCDateTimeType;
use App\Entity\User;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Doctrine\DBAL\Types\Type;
use Twig\Environment;

class TimezoneListener
{
    /** @var TokenStorageInterface */
    private $tokenStorage;
    /** @var Environment  */
    private $twig;

    /**
     * @param TokenStorageInterface $token_storage
     * @param Environment $twig
     */
    public function __construct(TokenStorageInterface $token_storage, Environment $twig) {
        $this->tokenStorage = $token_storage;
        $this->twig = $twig;
    }

    /**
     *
     * @param RequestEvent $event
     *
     * @throws \Doctrine\DBAL\DBALException
     */
    public function onKernelRequest(RequestEvent $event) {
        $token = $this->tokenStorage->getToken();

        // override doctrine datetime class
        Type::overrideType('datetime', UTCDateTimeType::class);
        Type::overrideType('datetimetz', UTCDateTimeType::class);

        // set time zone
        if (!defined('_USER_TIMEZONE_')) {
            if ($token != null && (($user = $token->getUser()) instanceof User) && $user->getTimezone()) {
                define('_USER_TIMEZONE_', $user->getTimezone());
            } else {
                define('_USER_TIMEZONE_', 'UTC');
            }
        }
        date_default_timezone_set(_USER_TIMEZONE_);

        // get utc time zone offset
        $now = new \DateTime();
        $mins = $now->getOffset() / 60;
        $sgn = ($mins < 0 ? -1 : 1);
        $mins = abs($mins);
        $hrs = floor($mins / 60);
        $mins -= $hrs * 60;
        $offset = sprintf('%+d:%02d', $hrs * $sgn, $mins);
        $this->twig->addGlobal('TIMEZONE_UTC', $offset);
    }
}

and services.yaml config:

App\EventListener\TimezoneListener:
    tags:
        - { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }

Service that handle event autoloaded by symfony itself.

And with this configuration I have following error:

Unable to add global "TIMEZONE_UTC" as the runtime or the extensions have already been initialized.

I see, that twig initiates at AppAuthAuthenticator::onAuthenticationFailure and when TimezoneListener try to add global twig var it's throws error, and if I change priority of TimezoneListener to 6 it's can't handle timezone (show +00:00)

How to configure this two events right?

To avoid the error when adding the global variable you could define a default value through the Twig configuration:

# config/packages/twig.yaml
twig:
    globals:
        TIMEZONE_UTC: 0
Related
How to check for null in Twig?
How to concatenate strings in twig
How to render a DateTime object in a Twig template
get current url in twig template?
Add more than one parameter in Twig path
Symfony2 PHP 5.4.4 Cannot change the ID of an active session
How to set the DateTime timezone programatically on user login in Symfony2
Symfony2 cannot connect to sqlite
How do I pass Token Storage as a param to an Event Listener in Symfony 3.4