瀏覽代碼

added a configuraiton to allow the profiler to be enabled only when an exception occurs

Fabien Potencier 15 年之前
父節點
當前提交
60ea1eef69

+ 6 - 0
src/Symfony/Bundle/FrameworkBundle/DependencyInjection/WebExtension.php

@@ -81,6 +81,12 @@ class WebExtension extends Extension
                     $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();
             }

+ 2 - 0
src/Symfony/Bundle/FrameworkBundle/Resources/config/profiling.xml

@@ -10,6 +10,7 @@
         <parameter key="profiler.storage.file">%kernel.cache_dir%/profiler.db</parameter>
         <parameter key="profiler.storage.lifetime">86400</parameter>
         <parameter key="profiler_listener.class">Symfony\Component\HttpKernel\Profiler\ProfilerListener</parameter>
+        <parameter key="profiler_listener.only_exceptions">false</parameter>
     </parameters>
 
     <services>
@@ -27,6 +28,7 @@
         <service id="profiler_listener" class="%profiler_listener.class%">
             <tag name="kernel.listener" />
             <argument type="service" id="profiler" />
+            <argument>%profiler_listener.only_exceptions%</argument>
         </service>
     </services>
 </container>

+ 9 - 2
src/Symfony/Component/HttpKernel/Profiler/ProfilerListener.php

@@ -25,15 +25,18 @@ class ProfilerListener
 {
     protected $profiler;
     protected $exception;
+    protected $onlyException;
 
     /**
      * Constructor.
      *
-     * @param Profiler $profiler A Profiler instance
+     * @param Profiler $profiler      A Profiler instance
+     * @param Boolean  $onlyException true if the profiler only collects data when an exception occurs, false otherwise
      */
-    public function __construct(Profiler $profiler)
+    public function __construct(Profiler $profiler, $onlyException = false)
     {
         $this->profiler = $profiler;
+        $this->onlyException = $onlyException;
     }
 
     /**
@@ -77,6 +80,10 @@ class ProfilerListener
             return $response;
         }
 
+        if ($this->onlyException && null === $this->exception) {
+            return $response;
+        }
+
         $this->profiler->collect($event->getParameter('request'), $response, $this->exception);
         $this->exception = null;