added a way to conditionnaly enable the profiler based on the request

This commit is contained in:
Fabien Potencier 2010-09-02 13:54:32 +02:00
parent 4f337615e3
commit 0b378d1b3e
6 changed files with 77 additions and 23 deletions

View File

@ -75,21 +75,7 @@ class WebExtension extends Extension
}
if (isset($config['profiler'])) {
if ($config['profiler']) {
if (!$container->hasDefinition('profiler')) {
$loader = new XmlFileLoader($container, __DIR__.'/../Resources/config');
$loader->load('profiling.xml');
$loader->load('collectors.xml');
}
if (isset($config['profiler']['only-exceptions'])) {
$container->setParameter('profiler_listener.only_exceptions', $config['profiler']['only-exceptions']);
} elseif (isset($config['profiler']['only_exceptions'])) {
$container->setParameter('profiler_listener.only_exceptions', $config['profiler']['only_exceptions']);
}
} elseif ($container->hasDefinition('profiler')) {
$container->getDefinition('profiling')->clearTags();
}
$this->registerProfilerConfiguration($config, $container);
}
if (isset($config['validation']['enabled'])) {
@ -204,6 +190,54 @@ class WebExtension extends Extension
));
}
/*
<profiler only-exceptions="false">
<matcher ip="192.168.0.0/24" path="#/admin/#i" />
<matcher>
<service class="MyMatcher" />
</matcher>
<matcher service="my_matcher" />
</profiler>
*/
protected function registerProfilerConfiguration($config, ContainerBuilder $container)
{
if ($config['profiler']) {
if (!$container->hasDefinition('profiler')) {
$loader = new XmlFileLoader($container, __DIR__.'/../Resources/config');
$loader->load('profiling.xml');
$loader->load('collectors.xml');
}
if (isset($config['profiler']['only-exceptions'])) {
$container->setParameter('profiler_listener.only_exceptions', $config['profiler']['only-exceptions']);
} elseif (isset($config['profiler']['only_exceptions'])) {
$container->setParameter('profiler_listener.only_exceptions', $config['profiler']['only_exceptions']);
}
if (isset($config['profiler']['matcher'])) {
if (isset($config['profiler']['matcher']['service'])) {
$container->setAlias('profiler.request_matcher', $config['profiler']['matcher']['service']);
} elseif (isset($config['profiler']['matcher']['_services'])) {
$container->setAlias('profiler.request_matcher', (string) $config['profiler']['matcher']['_services'][0]);
} else {
$definition = $container->register('profiler.request_matcher', 'Symfony\\Component\\HttpFoundation\\RequestMatcher');
if (isset($config['profiler']['matcher']['ip'])) {
$definition->addMethodCall('matchIp', array($config['profiler']['matcher']['ip']));
}
if (isset($config['profiler']['matcher']['path'])) {
$definition->addMethodCall('matchPath', array($config['profiler']['matcher']['path']));
}
}
} else {
$container->removeAlias('profiler.request_matcher');
}
} elseif ($container->hasDefinition('profiler')) {
$container->getDefinition('profiling')->clearTags();
}
}
protected function registerValidationConfiguration($config, ContainerBuilder $container)
{
if ($config['validation']['enabled']) {

View File

@ -28,6 +28,7 @@
<service id="profiler_listener" class="%profiler_listener.class%">
<tag name="kernel.listener" />
<argument type="service" id="profiler" />
<argument type="service" id="profiler.request_matcher" on-invalid="null" />
<argument>%profiler_listener.only_exceptions%</argument>
</service>
</services>

View File

@ -13,11 +13,22 @@
<xsd:sequence>
<xsd:element name="router" type="router" minOccurs="0" maxOccurs="1" />
<xsd:element name="validation" type="validation" minOccurs="0" maxOccurs="1" />
<xsd:element name="profiler" type="profiler" minOccurs="0" maxOccurs="1" />
</xsd:sequence>
<xsd:attribute name="ide" type="xsd:string" />
<xsd:attribute name="profiler" type="xsd:boolean" />
<xsd:attribute name="toolbar" type="xsd:boolean" />
</xsd:complexType>
<xsd:complexType name="profiler">
<xsd:sequence>
<xsd:element name="matcher" type="profiler_matcher" minOccurs="0" maxOccurs="1" />
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="profiler_matcher">
<xsd:attribute name="ip" type="xsd:string" />
<xsd:attribute name="path" type="xsd:string" />
<xsd:attribute name="service" type="xsd:string" />
</xsd:complexType>
<xsd:complexType name="router">

View File

@ -38,7 +38,7 @@ class RequestDataCollector extends DataCollector
'request_server' => $request->server->all(),
'request_cookies' => $request->cookies->all(),
'response_headers' => $response->headers->all(),
'session_attributes' => $request->getSession()->getAttributes(),
'session_attributes' => $request->hasSession() ? $request->getSession()->getAttributes() : array(),
);
}

View File

@ -6,6 +6,7 @@ use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpFoundation\RequestMatcherInterface;
/*
* This file is part of the Symfony framework.
@ -26,16 +27,19 @@ class ProfilerListener
protected $profiler;
protected $exception;
protected $onlyException;
protected $matcher;
/**
* Constructor.
*
* @param Profiler $profiler A Profiler instance
* @param Boolean $onlyException true if the profiler only collects data when an exception occurs, false otherwise
* @param Profiler $profiler A Profiler instance
* @param RequestMatcherInterface $matcher A RequestMatcher instance
* @param Boolean $onlyException true if the profiler only collects data when an exception occurs, false otherwise
*/
public function __construct(Profiler $profiler, $onlyException = false)
public function __construct(Profiler $profiler, RequestMatcherInterface $matcher = null, $onlyException = false)
{
$this->profiler = $profiler;
$this->matcher = $matcher;
$this->onlyException = $onlyException;
}
@ -80,6 +84,10 @@ class ProfilerListener
return $response;
}
if (null !== $this->matcher && !$this->matcher->matches($event->getParameter('request'))) {
return $response;
}
if ($this->onlyException && null === $this->exception) {
return $response;
}

View File

@ -30,12 +30,12 @@ class ProfilerTest extends \PHPUnit_Framework_TestCase
$storage->purge(true);
$profiler = new Profiler($storage);
$profiler->addCollector($collector);
$profiler->add($collector);
$profiler->setToken('foobar');
$profiler->collect($request, $response);
$profiler = new Profiler($storage);
$profiler->setToken('foobar');
$this->assertEquals(array('foo' => 'bar'), $profiler->getCollector('request')->getRequestQuery()->all());
$this->assertEquals(array('foo' => 'bar'), $profiler->get('request')->getRequestQuery()->all());
}
}