44How to Use Matchers to Enable the Profiler Conditionally
55========================================================
66
7- By default, the profiler is only activated in the development environment. But
8- it's imaginable that a developer may want to see the profiler even in
9- production. Another situation may be that you want to show the profiler only
10- when an admin has logged in. You can enable the profiler in these situations
11- by using matchers.
7+ The Symfony profiler is only activated in the development environment to not hurt
8+ your application performance. However, sometimes it may be useful to conditionally
9+ enable the profiler in the production environment to assist you in hard to debug
10+ issues. This behavior is implemented with the **Request Matchers **.
1211
1312Using the built-in Matcher
1413--------------------------
1514
16- Symfony provides a
15+ A Request Matcher is a class that checks whether a given ``Request `` instance
16+ matches a set of conditions. Symfony provides a
1717:class: `built-in matcher <Symfony\\ Component\\ HttpFoundation\\ RequestMatcher> `
18- which can match paths and IPs. For example, if you want to only show the
19- profiler when accessing the page with the ``168.0.0.1 `` IP, then you can
20- use this configuration:
18+ which matches paths and IPs. For example, if you want to only show the profiler
19+ when accessing the page with the ``168.0.0.1 `` IP, then you can use this
20+ configuration:
2121
2222.. configuration-block ::
2323
@@ -50,22 +50,24 @@ use this configuration:
5050
5151 You can also set a ``path `` option to define the path on which the profiler
5252should be enabled. For instance, setting it to ``^/admin/ `` will enable the
53- profiler only for the ``/admin/ `` URLs .
53+ profiler only for the URLs which start with ``/admin/ ``.
5454
55- Creating a custom Matcher
55+ Creating a Custom Matcher
5656-------------------------
5757
58- You can also create a custom matcher. This is a service that checks whether
59- the profiler should be enabled or not . To create that service , create a class
58+ Leveraging the concept of Request Matchers you can define a custom matcher to
59+ enable the profiler conditionally in your application . To do so , create a class
6060which implements
6161:class: `Symfony\\ Component\\ HttpFoundation\\ RequestMatcherInterface `. This
6262interface requires one method:
6363:method: `Symfony\\ Component\\ HttpFoundation\\ RequestMatcherInterface::matches `.
64- This method returns false to disable the profiler and true to enable the
65- profiler.
64+ This method returns ``false `` when the request doesn't match the conditions and
65+ ``true `` otherwise. Therefore, the custom matcher must return ``false `` to
66+ disable the profiler and ``true `` to enable it.
6667
67- To enable the profiler when a ``ROLE_SUPER_ADMIN `` is logged in, you can use
68- something like::
68+ Suppose that the profiler must be enabled whenever a user with a
69+ ``ROLE_SUPER_ADMIN `` is logged in. This is the only code needed for that custom
70+ matcher::
6971
7072 // src/AppBundle/Profiler/SuperAdminMatcher.php
7173 namespace AppBundle\Profiler;
@@ -89,24 +91,26 @@ something like::
8991 }
9092 }
9193
92- Then, you need to configure the service:
94+ Then, configure a new service and set it as ``private `` because the application
95+ won't use it directly:
9396
9497.. configuration-block ::
9598
9699 .. code-block :: yaml
97100
98101 # app/config/services.yml
99102 services :
100- app.profiler.matcher.super_admin :
103+ app.super_admin_matcher :
101104 class : AppBundle\Profiler\SuperAdminMatcher
102105 arguments : ["@security.context"]
106+ public : false
103107
104108 .. code-block :: xml
105109
106110 <!-- app/config/services.xml -->
107111 <services >
108- <service id =" app.profiler.matcher.super_admin "
109- class =" AppBundle\Profiler\SuperAdminMatcher" >
112+ <service id =" app.super_admin_matcher "
113+ class =" AppBundle\Profiler\SuperAdminMatcher" public = " false " >
110114 <argument type =" service" id =" security.context" />
111115 </services >
112116
@@ -116,12 +120,15 @@ Then, you need to configure the service:
116120 use Symfony\Component\DependencyInjection\Definition;
117121 use Symfony\Component\DependencyInjection\Reference;
118122
119- $container->setDefinition('app.profiler.matcher.super_admin', new Definition(
123+ $definition = new Definition(
120124 'AppBundle\Profiler\SuperAdminMatcher',
121125 array(new Reference('security.context'))
122126 );
127+ $definition->setPublic(false);
123128
124- Now the service is registered, the only thing left to do is configure the
129+ $container->setDefinition('app.super_admin_matcher', $definition);
130+
131+ Once the service is registered, the only thing left to do is configure the
125132profiler to use this service as the matcher:
126133
127134.. configuration-block ::
@@ -133,15 +140,15 @@ profiler to use this service as the matcher:
133140 # ...
134141 profiler :
135142 matcher :
136- service : app.profiler.matcher.super_admin
143+ service : app.super_admin_matcher
137144
138145 .. code-block :: xml
139146
140147 <!-- app/config/config.xml -->
141148 <framework : config >
142149 <!-- ... -->
143150 <framework : profiler
144- service =" app.profiler.matcher.super_admin "
151+ service =" app.super_admin_matcher "
145152 />
146153 </framework : config >
147154
@@ -151,6 +158,6 @@ profiler to use this service as the matcher:
151158 $container->loadFromExtension('framework', array(
152159 // ...
153160 'profiler' => array(
154- 'service' => 'app.profiler.matcher.super_admin ',
161+ 'service' => 'app.super_admin_matcher ',
155162 ),
156163 ));
0 commit comments