|
| 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