Created
March 15, 2019 15:08
-
-
Save romaricdrigon/32c6b23de0d4ed58be650f14f93d757e to your computer and use it in GitHub Desktop.
Example of a Symfony (3) custom DataCollector
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{# AppBundle/Resources/views/my_security.html.twig #} | |
{% extends '@WebProfiler/Profiler/layout.html.twig' %} | |
{% block toolbar %} | |
{% set icon %} | |
<span class="icon" style="color: #AAA; margin-right: 3px;"> | |
{# svp de l'icône lock open de Font-awesome #} | |
<svg aria-hidden="true" data-prefix="fas" data-icon="lock-open" class="svg-inline--fa fa-lock-open fa-w-18" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 576 512"><path fill="currentColor" d="M423.5 0C339.5.3 272 69.5 272 153.5V224H48c-26.5 0-48 21.5-48 48v192c0 26.5 21.5 48 48 48h352c26.5 0 48-21.5 48-48V272c0-26.5-21.5-48-48-48h-48v-71.1c0-39.6 31.7-72.5 71.3-72.9 40-.4 72.7 32.1 72.7 72v80c0 13.3 10.7 24 24 24h32c13.3 0 24-10.7 24-24v-80C576 68 507.5-.3 423.5 0z"></path></svg> | |
</span> | |
<span class="sf-toolbar-value">{% if collector.currentSecurity %}@Security{% else %}Pas de sécurité{% endif %}</span> | |
{% endset %} | |
{% set text %} | |
<div class="sf-toolbar-info-piece"> | |
{# Pas utilisé ici, utile pour rajouter un texte qui s'affichera lorsqu'on passe le curseur sur la toolbar #} | |
</div> | |
{% endset %} | |
{{ include('@WebProfiler/Profiler/toolbar_item.html.twig', { 'link': true }) }} | |
{% endblock %} | |
{% block menu %} | |
<span class="label"> | |
<strong>@Security</strong> | |
</span> | |
{% endblock %} | |
{% block panel %} | |
<h2>@Security</h2> | |
<p>Le contrôleur courant a la règle suivante:</p> | |
<pre><code> | |
@Security("{{ collector.currentSecurity }}") | |
</code></pre> | |
{% endblock %} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace AppBundle\DataCollector; | |
use Doctrine\Common\Annotations\AnnotationReader; | |
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security; | |
use Symfony\Component\HttpFoundation\Request; | |
use Symfony\Component\HttpFoundation\Response; | |
use Symfony\Component\HttpKernel\DataCollector\DataCollector; | |
class MySecurityDataCollector extends DataCollector | |
{ | |
public function collect(Request $request, Response $response, \Exception $exception = null) | |
{ | |
// On peut collecter des informations sur la requête en cours, | |
// dans cet exemple basique je veux savoir quelle est l'action appelée pour récupérer plus tard son annotation | |
$this->data['current_controller'] = $request->attributes->get('_controller'); | |
} | |
public function getCurrentSecurity() | |
{ | |
if (!$this->data['current_controller']) { | |
return null; | |
} | |
$params = explode('::', $this->data['current_controller']); | |
if (2 !== count($params)) { | |
return null; | |
} | |
$method = new \ReflectionMethod($params[0], $params[1]); | |
$annotationReader = new AnnotationReader(); | |
$annotation = $annotationReader->getMethodAnnotation($method, Security::class); | |
if (!$annotation) { | |
return null; | |
} | |
return $annotation->getExpression(); | |
} | |
/** | |
* Obligatoire avec Symfony 4, recommandé à partir de Symfony 3, inutile avec Symfony 2 | |
*/ | |
public function reset() | |
{ | |
$this->data = []; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function getName() | |
{ | |
return 'app.my_security_collector'; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
services: | |
app.my_security_collector: | |
class: AppBundle\DataCollector\MySecurityDataCollector | |
tags: | |
- | |
name: data_collector | |
template: 'AppBundle:DataCollector:my_security.html.twig' | |
id: 'app.my_security_collector' | |
public: false |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment