Skip to content

Commit 1adc399

Browse files
committed
Merge branch 'issue_2104' of github.com:WouterJ/symfony-docs into WouterJ-issue_2104
Conflicts: book/internals.rst
2 parents e3265ce + 1038fb8 commit 1adc399

File tree

4 files changed

+168
-98
lines changed

4 files changed

+168
-98
lines changed

book/internals.rst

Lines changed: 2 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -632,104 +632,8 @@ As the profiler adds some overhead, you might want to enable it only under
632632
certain circumstances in the production environment. The ``only-exceptions``
633633
settings limits profiling to 500 pages, but what if you want to get
634634
information when the client IP comes from a specific address, or for a limited
635-
portion of the website? You can use a request matcher:
636-
637-
.. configuration-block::
638-
639-
.. code-block:: yaml
640-
641-
# enables the profiler only for request coming
642-
# for the 192.168.0.0 network
643-
framework:
644-
profiler:
645-
matcher: { ip: 192.168.0.0/24 }
646-
647-
# enables the profiler only for the /admin URLs
648-
framework:
649-
profiler:
650-
matcher: { path: "^/admin/" }
651-
652-
# combine rules
653-
framework:
654-
profiler:
655-
matcher: { ip: 192.168.0.0/24, path: "^/admin/" }
656-
657-
# use a custom matcher instance defined in
658-
# the "custom_matcher" service
659-
framework:
660-
profiler:
661-
matcher: { service: custom_matcher }
662-
663-
.. code-block:: xml
664-
665-
<!--
666-
enables the profiler only for request coming
667-
for the 192.168.0.0 network
668-
-->
669-
<framework:config>
670-
<framework:profiler>
671-
<framework:matcher ip="192.168.0.0/24" />
672-
</framework:profiler>
673-
</framework:config>
674-
675-
<!-- enables the profiler only for the /admin URLs -->
676-
<framework:config>
677-
<framework:profiler>
678-
<framework:matcher path="^/admin/" />
679-
</framework:profiler>
680-
</framework:config>
681-
682-
<!-- combine rules -->
683-
<framework:config>
684-
<framework:profiler>
685-
<framework:matcher ip="192.168.0.0/24" path="^/admin/" />
686-
</framework:profiler>
687-
</framework:config>
688-
689-
<!--
690-
use a custom matcher instance defined in
691-
the "custom_matcher" service
692-
-->
693-
<framework:config>
694-
<framework:profiler>
695-
<framework:matcher service="custom_matcher" />
696-
</framework:profiler>
697-
</framework:config>
698-
699-
.. code-block:: php
700-
701-
// enables the profiler only for request coming
702-
// for the 192.168.0.0 network
703-
$container->loadFromExtension('framework', array(
704-
'profiler' => array(
705-
'matcher' => array('ip' => '192.168.0.0/24'),
706-
),
707-
));
708-
709-
// enables the profiler only for the /admin URLs
710-
$container->loadFromExtension('framework', array(
711-
'profiler' => array(
712-
'matcher' => array('path' => '^/admin/'),
713-
),
714-
));
715-
716-
// combine rules
717-
$container->loadFromExtension('framework', array(
718-
'profiler' => array(
719-
'matcher' => array(
720-
'ip' => '192.168.0.0/24',
721-
'path' => '^/admin/',
722-
),
723-
),
724-
));
725-
726-
// use a custom matcher instance defined in
727-
// the "custom_matcher" service
728-
$container->loadFromExtension('framework', array(
729-
'profiler' => array(
730-
'matcher' => array('service' => 'custom_matcher'),
731-
),
732-
));
635+
portion of the website? You can use a Profiler Matcher, learn more about that
636+
in ":doc:`/cookbook/profiler/matchers`".
733637

734638
Learn more from the Cookbook
735639
----------------------------

cookbook/map.rst.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@
100100
* :doc:`/cookbook/profiler/index`
101101

102102
* :doc:`/cookbook/profiler/data_collector`
103+
* :doc:`/cookbook/profiler/matchers`
103104

104105
* :doc:`/cookbook/request/index`
105106

cookbook/profiler/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ Profiler
55
:maxdepth: 2
66

77
data_collector
8+
matchers

cookbook/profiler/matchers.rst

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
.. index::
2+
single: Profiling; Matchers
3+
4+
How to use Matchers to enable the Profiler
5+
==========================================
6+
7+
By default, the profiler is only activated in the development environment. But
8+
it's imaginable that a developer always wants to see the profiler, even in
9+
production. Another situation may be to show the profiler when an admin has
10+
logged in. You can enable the profiler in these situations by using matchers.
11+
12+
Using the build-in Matcher
13+
--------------------------
14+
15+
Symfony2 provides a
16+
:class:`build-in matcher <Symfony\\Component\\HttpFoundation\\RequestMatcher>`
17+
which can match paths and IPs. For instance, only show the profiler when
18+
accessing the page with the ``168.0.0.1`` ip. Then, the profiler can be
19+
configured to something like this:
20+
21+
.. configuration-block::
22+
23+
.. code-block:: yaml
24+
25+
# app/config/config.yml
26+
framework:
27+
# ...
28+
profiler:
29+
matcher:
30+
ip: 168.0.0.1
31+
32+
.. code-block:: xml
33+
34+
<!-- app/config/config.xml -->
35+
<framework:config>
36+
<framework:profiler
37+
ip="168.0.0.1"
38+
/>
39+
</framework:config>
40+
41+
.. code-block:: php
42+
43+
// app/config/config.php
44+
$container->loadFromExtension('framework', array(
45+
'profiler' => array(
46+
'ip' => '168.0.0.1',
47+
),
48+
));
49+
50+
You can also set a ``path`` option to define the path on which the profiler
51+
should be enabled. For instance, setting it to `^/admin/` will enable the
52+
profiler only for the ``/admin/`` urls.
53+
54+
Creating a Custom Matcher
55+
-------------------------
56+
57+
You can also create a custom matcher. This is a service that checks whether
58+
the profiler should be enabled or not. To create that service, create a class
59+
which implements
60+
:class:`Symfony\\Component\\HttpFoundation\\RequestMatcherInterface`. This
61+
interface requires one method:
62+
:method:`Symfony\\Component\\HttpFoundation\\RequestMatcherInterface::matches`.
63+
This method returns a falsey value to disable the profiler, any other value
64+
enables the profiler.
65+
66+
To enable the profiler when a ``ROLE_SUPER_ADMIN`` is logged in, you can use
67+
something like::
68+
69+
// src/Acme/DemoBundle/Profiler/SuperAdminMatcher.php
70+
namespace Acme\DemoBundle\Profiler;
71+
72+
use Symfony\Component\Security\Core\SecurityContext;
73+
use Symfony\Component\HttpFoundation\Request;
74+
use Symfony\Component\HttpFoundation\RequestMatcherInterface;
75+
76+
class SuperAdminMatcher implements RequestMatcherInterface
77+
{
78+
protected $securityContext;
79+
80+
public function __construct(SecurityContext $securityContext)
81+
{
82+
$this->securityContext = $securityContext;
83+
}
84+
85+
public function matches(Request $request)
86+
{
87+
return $this->securityContext->isGranted('ROLE_SUPER_ADMIN');
88+
}
89+
}
90+
91+
Then, you need to configure the service:
92+
93+
.. configuration-block::
94+
95+
.. code-block:: yaml
96+
97+
parameters:
98+
acme_demo.profiler.matcher.super_admin.class: Acme\DemoBundle\Profiler\SuperAdminMatcher
99+
100+
services:
101+
acme_demo.profiler.matcher.super_admin:
102+
class: "%acme_demo.profiler.matcher.super_admin.class%"
103+
arguments: [@security.context]
104+
105+
.. code-block:: xml
106+
107+
<parameters>
108+
<parameter
109+
key="acme_demo.profiler.matcher.super_admin.class"
110+
>Acme\DemoBundle\Profiler\SuperAdminMatcher</parameter>
111+
</parameters>
112+
113+
<services>
114+
<service id="acme_demo.profiler.matcher.super_admin"
115+
class="%acme_demo.profiler.matcher.super_admin.class%">
116+
<argument type="service" id="security.context" />
117+
</services>
118+
119+
.. code-block:: php
120+
121+
use Symfony\Component\DependencyInjection\Definition;
122+
use Symfony\Component\DependencyInjection\Reference;
123+
124+
$container->setParameter(
125+
'acme_demo.profiler.matcher.super_admin.class',
126+
'Acme\DemoBundle\Profiler\SuperAdminMatcher'
127+
);
128+
129+
$container->setDefinition('acme_demo.profiler.matcher.super_admin', new Definition(
130+
'%acme_demo.profiler.matcher.super_admin.class%',
131+
array(new Reference('security.context'))
132+
);
133+
134+
Now the service is registered, the only thing left to do is configure the
135+
profiler to use this service as the matcher:
136+
137+
.. configuration-block::
138+
139+
.. code-block:: yaml
140+
141+
# app/config/config.yml
142+
framework:
143+
# ...
144+
profiler:
145+
matcher:
146+
service: acme_demo.profiler.matcher.super_admin
147+
148+
.. code-block:: xml
149+
150+
<!-- app/config/config.xml -->
151+
<framework:config>
152+
<framework:profiler
153+
service="acme_demo.profiler.matcher.super_admin"
154+
/>
155+
</framework:config>
156+
157+
.. code-block:: php
158+
159+
// app/config/config.php
160+
$container->loadFromExtension('framework', array(
161+
'profiler' => array(
162+
'service' => 'acme_demo.profiler.matcher.super_admin',
163+
),
164+
));

0 commit comments

Comments
 (0)