php - How to prevent Symfony profiler from accessing or executing a listener -
my user has counttasks
property, corresponding setter , getter:
class user implements userinterface, \serializable { /** * @var integer */ private $counttasks; }
i want property shown in application's navigation bar (the "14" number in red):
obviously, property should set every controller. (actually, every deals rendering navigation bar, that's not case here). application should count tasks logged-in user every controller.
i found relevant topic in symfony cookbook: how setup before , after filters, , managed implement it:
acme\testbundle\eventlistener\userlistener.php
:
namespace acme\testbundle\eventlistener; use symfony\component\httpkernel\event\filtercontrollerevent; class userlistener { public function onkernelcontroller(filtercontrollerevent $event) { $controller = $event->getcontroller(); if ( ! is_array($controller)) { return; } $securitycontext = $controller[0]->get('security.context'); // count tasks, if user logged-in if ($securitycontext->isgranted('is_authenticated_remembered') or $securitycontext->isgranted('is_authenticated_fully')) { $user = $securitycontext->gettoken()->getuser(); // ... // countig tasks , setting $counttasks var // ... $user->setcounttasks($counttasks); } } }
services.yml
:
services: acme.user.before_controller: class: acme\testbundle\eventlistener\userlistener tags: - { name: kernel.event_listener, event: kernel.controller, method: onkernelcontroller }
it works expected , i'm able pull property in twig template this:
{{ app.user.counttasks }}
it works expected in prod env.
in dev however, profiler throws undefinedmethodexception
:
undefinedmethodexception: attempted call method "get" on class "symfony\bundle\webprofilerbundle\controller\profilercontroller" in ...\src\acme\testbundle\eventlistener\userlistener.php line 18.
where line 18 one:
$securitycontext = $controller[0]->get('security.context');
as quick patch added additional check (before line 18) prevent profiler executing further logic:
if (is_a($controller[0], '\symfony\bundle\webprofilerbundle\controller\profilercontroller')) { return; } $securitycontext = $controller[0]->get('security.context');
and has made trick. i'm afraid it's not right way. i'm afraid i'm loosing part of debug information in profiler.
am right concerns? can point me better way prevent profiler executing listener? in config somehow?
even in symfony's documentation how setup before , after filters, instanceof condition being evaluated in line 29.
i'd go saying if doc's doing it, you're pretty safe doing (unless stated otherwise, not case).
Comments
Post a Comment