4
4
How to Use Matchers to Enable the Profiler Conditionally
5
5
========================================================
6
6
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 **.
12
11
13
12
Using the built-in Matcher
14
13
--------------------------
15
14
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
17
17
: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:
21
21
22
22
.. configuration-block ::
23
23
@@ -50,22 +50,24 @@ use this configuration:
50
50
51
51
You can also set a ``path `` option to define the path on which the profiler
52
52
should 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/ ``.
54
54
55
- Creating a custom Matcher
55
+ Creating a Custom Matcher
56
56
-------------------------
57
57
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
60
60
which implements
61
61
:class: `Symfony\\ Component\\ HttpFoundation\\ RequestMatcherInterface `. This
62
62
interface requires one method:
63
63
: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.
66
67
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::
69
71
70
72
// src/AppBundle/Profiler/SuperAdminMatcher.php
71
73
namespace AppBundle\Profiler;
@@ -89,24 +91,26 @@ something like::
89
91
}
90
92
}
91
93
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:
93
96
94
97
.. configuration-block ::
95
98
96
99
.. code-block :: yaml
97
100
98
101
# app/config/services.yml
99
102
services :
100
- app.profiler.matcher.super_admin :
103
+ app.super_admin_matcher :
101
104
class : AppBundle\Profiler\SuperAdminMatcher
102
105
arguments : ["@security.context"]
106
+ public : false
103
107
104
108
.. code-block :: xml
105
109
106
110
<!-- app/config/services.xml -->
107
111
<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 " >
110
114
<argument type =" service" id =" security.context" />
111
115
</services >
112
116
@@ -116,12 +120,15 @@ Then, you need to configure the service:
116
120
use Symfony\Component\DependencyInjection\Definition;
117
121
use Symfony\Component\DependencyInjection\Reference;
118
122
119
- $container->setDefinition('app.profiler.matcher.super_admin', new Definition(
123
+ $definition = new Definition(
120
124
'AppBundle\Profiler\SuperAdminMatcher',
121
125
array(new Reference('security.context'))
122
126
);
127
+ $definition->setPublic(false);
123
128
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
125
132
profiler to use this service as the matcher:
126
133
127
134
.. configuration-block ::
@@ -133,15 +140,15 @@ profiler to use this service as the matcher:
133
140
# ...
134
141
profiler :
135
142
matcher :
136
- service : app.profiler.matcher.super_admin
143
+ service : app.super_admin_matcher
137
144
138
145
.. code-block :: xml
139
146
140
147
<!-- app/config/config.xml -->
141
148
<framework : config >
142
149
<!-- ... -->
143
150
<framework : profiler
144
- service =" app.profiler.matcher.super_admin "
151
+ service =" app.super_admin_matcher "
145
152
/>
146
153
</framework : config >
147
154
@@ -151,6 +158,6 @@ profiler to use this service as the matcher:
151
158
$container->loadFromExtension('framework', array(
152
159
// ...
153
160
'profiler' => array(
154
- 'service' => 'app.profiler.matcher.super_admin ',
161
+ 'service' => 'app.super_admin_matcher ',
155
162
),
156
163
));
0 commit comments