Enable debug mode interactively in symfony

Phen picture Phen · Mar 11, 2011 · Viewed 12.2k times · Source

I'm using symfony 1.4 with Doctrine.

I'm trying to find a way to enable debug mode only if the current sfUser has a special debugger credential.

I already created a filter that deactivates the symfony debug bar if the sfUser has not this credential (the web_debug is set to true in my settings.yml file):

class checkWebDebugFilter extends sfFilter
{
  public function execute($filterChain)
  {
    if(!$this->getContext()->getUser()->hasCredential('debugger'))
    {
      sfConfig::set('sf_web_debug', false);
    }

    $filterChain->execute();
  }
}

The code of my index.php file is:

require_once(dirname(__FILE__).'/../config/ProjectConfiguration.class.php');
$configuration = ProjectConfiguration::getApplicationConfiguration('frontend', 'prod', false));
sfContext::createInstance($configuration)->dispatch();

The problem is, as the debug mode is hardcoded to false in my index.php, it is also disabled for debuggers; therefore the Web debug bar does not show Doctrine statements nor timing indications.

Is there a way to enable debug mode only if the current sfUser has a precise credential?

I tried to add sfConfig::set('sf_debug', true); to my checkWebDebugFilter::execute() method but as the filter is executed after Doctrine statements, they are not recorded.

I also tried to add session_start(); in my index.php file, then browsing through the $_SESSION variable to check whether the current user has the debugger credential, but it did not work (and it was not in the spirit of symfony either).

Thanks in advance for your answers.

Answer

georgelx picture georgelx · May 10, 2011

When you pass the debug parameter in the index.php file, it actually is passed down to the sfApplicationConfiguration class of your application. In your case it can be found in the /apps/frontend/config/frontendConfiguration.class.php file. frontendConfiguration class extends sfApplicationConfiguration, and here you can add your code.

Debug parameter is stored in a protected variable of this class, so you wont be able to change it from filter, but you can create a function for example:

setDebug($mode) {
   $this->debug = $mode;
} 

And call it in your filter:

$this->context->getConfiguration()->setDebug(true);

You also could override isDebug() function in frontendConfiguration class, because that is used in the initConfiguration() function to initialize timing indicators and other debugging stuff.

if ($this->isDebug() && !sfWebDebugPanelTimer::isStarted())
{
    sfWebDebugPanelTimer::startTime();
}

But you won't be able to check user permissions here, as sfUser class won't be initialized in this stage yet. But you can check $_COOKIES or $_SESSION global variables for a value that you can set when user is logging in. Or you can call sfWebDebugPanelTimer::startTime() in your Filter, but will miss a few microseconds.

I have not tested this, but that's how I would do it.