Last active
June 3, 2020 23:04
-
-
Save dextervip/5609852645b366a92a4952ede153f9cb to your computer and use it in GitHub Desktop.
Enable Symfony Profiler for Admins In Production
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use App\Kernel; | |
use Symfony\Component\ErrorHandler\Debug; | |
use Symfony\Component\HttpFoundation\Request; | |
require dirname(__DIR__).'/config/bootstrap.php'; | |
if(isset($_COOKIE['debug']) && $_COOKIE['debug']){ | |
$_SERVER['APP_DEBUG'] = (bool) $_COOKIE['debug']; | |
} | |
if ($_SERVER['APP_DEBUG']) { | |
umask(0000); | |
Debug::enable(); | |
} | |
if ($trustedProxies = $_SERVER['TRUSTED_PROXIES'] ?? false) { | |
Request::setTrustedProxies(explode(',', $trustedProxies), Request::HEADER_X_FORWARDED_ALL ^ Request::HEADER_X_FORWARDED_HOST); | |
} | |
if ($trustedHosts = $_SERVER['TRUSTED_HOSTS'] ?? false) { | |
Request::setTrustedHosts([$trustedHosts]); | |
} | |
$kernel = new Kernel($_SERVER['APP_ENV'], (bool) $_SERVER['APP_DEBUG']); | |
$request = Request::createFromGlobals(); | |
$response = $kernel->handle($request); | |
$response->send(); | |
$kernel->terminate($request, $response); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\EventSubscriber; | |
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | |
use Symfony\Component\HttpFoundation\Request; | |
use Symfony\Component\HttpFoundation\RequestStack; | |
use Symfony\Component\HttpKernel\Event\RequestEvent; | |
use Symfony\Component\HttpKernel\KernelEvents; | |
use Symfony\Component\HttpKernel\Profiler\Profiler; | |
use Symfony\Component\Security\Core\Security; | |
use Symfony\Component\ErrorHandler\Debug; | |
class ProfilerSubscriber implements EventSubscriberInterface | |
{ | |
/** | |
* @var Profiler | |
*/ | |
private $profiler; | |
/** @var Security */ | |
private $security; | |
/** @var string */ | |
private $appEnv; | |
/** | |
* ProfilerSubscriber constructor. | |
* @param Profiler $profiler | |
* @param Security $security | |
* @param string $appEnv | |
*/ | |
public function __construct(Profiler $profiler, Security $security, string $appEnv) | |
{ | |
$this->profiler = $profiler; | |
$this->security = $security; | |
$this->appEnv = $appEnv; | |
} | |
public function onKernelRequest(RequestEvent $event) | |
{ | |
if (!$event->isMasterRequest()) { | |
return; | |
} | |
if($this->appEnv == 'prod'){ | |
setcookie("debug", 0, time()+3600); | |
$this->profiler->disable(); | |
} | |
if($user = $this->security->getUser()){ | |
if(in_array('ROLE_ADMIN',$user->getRoles())){ | |
$this->profiler->enable(); | |
setcookie("debug", 1, time()+3600); | |
} | |
} | |
} | |
public static function getSubscribedEvents() | |
{ | |
return array( | |
KernelEvents::REQUEST => 'onKernelRequest' | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment