Skip to content

Commit

Permalink
data collector (#61)
Browse files Browse the repository at this point in the history
* add data collector

* replace code with ToggleSerializer

* use yellow for inactive toggles

* Add description for the data collector to the README.md
  • Loading branch information
fadoe authored and othillo committed Dec 19, 2017
1 parent a9a23fc commit e812c86
Show file tree
Hide file tree
Showing 5 changed files with 215 additions and 2 deletions.
96 changes: 96 additions & 0 deletions DataCollector/ToggleCollector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

/*
* This file is part of the qandidate-labs/qandidate-toggle-bundle package.
*
* (c) Qandidate.com <opensource@qandidate.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Qandidate\Bundle\ToggleBundle\DataCollector;

use Qandidate\Toggle\Context;
use Qandidate\Toggle\ContextFactory;
use Qandidate\Toggle\Serializer\OperatorConditionSerializer;
use Qandidate\Toggle\Serializer\OperatorSerializer;
use Qandidate\Toggle\Serializer\ToggleSerializer;
use Qandidate\Toggle\Toggle;
use Qandidate\Toggle\ToggleManager;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\DataCollector\DataCollector;

class ToggleCollector extends DataCollector
{
/**
* @var ToggleManager
*/
private $toggleManager;
/**
* @var ContextFactory
*/
private $contextFactory;

/**
* ToggleCollector constructor.
* @param ToggleManager $toggleManager
* @param ContextFactory $contextFactory
*/
public function __construct(ToggleManager $toggleManager, ContextFactory $contextFactory)
{
$this->toggleManager = $toggleManager;
$this->contextFactory = $contextFactory;
}

/**
* Collects data for the given Request and Response.
*
* @param Request $request A Request instance
* @param Response $response A Response instance
* @param \Exception $exception An Exception instance
*/
public function collect(Request $request, Response $response, \Exception $exception = null)
{
$serializer = new ToggleSerializer(new OperatorConditionSerializer(new OperatorSerializer()));

$toggleData = array_map(function (Toggle $toggle) use ($serializer) {
return $serializer->serialize($toggle);
}, $this->toggleManager->all());

$this->data['toggleDetails'] = $toggleData;
$this->data['context'] = $this->contextFactory->createContext();
}

/**
* @return Context
*/
public function getContext()
{
return $this->data['context'];
}

/**
* @return array
*/
public function getToggleDetails()
{
return $this->data['toggleDetails'];
}

/**
* Returns the name of the collector.
*
* @return string The collector name
*/
public function getName()
{
return 'qandidate.toggle_collector';
}

public function reset()
{
$this->data = array();
}
}
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,15 @@ Or the Twig test:

Both are registered in the [ToggleTwigExtension](Twig/ToggleTwigExtension.php).

## Data collector

With the data collector you have a overview about all toggles. In the toolbar you see all conditions and the current status.

In the panel you have two lists:

* You can see all keys and there current values.
* Then you can see all configured toggles, there conditions and if they are active.

## Testing

To run PHPUnit tests:
Expand Down
11 changes: 9 additions & 2 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" ?>

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<parameters>
<parameter key="qandidate.toggle.manager.class">Qandidate\Toggle\ToggleManager</parameter>
Expand All @@ -13,6 +13,7 @@
<parameter key="qandidate.toggle.twig_extension.class">Qandidate\Bundle\ToggleBundle\Twig\ToggleTwigExtension</parameter>
<parameter key="qandidate.toggle.toggle.listener.class">Qandidate\Bundle\ToggleBundle\EventListener\ToggleListener</parameter>
<parameter key="qandidate.toggle.context.class">Qandidate\Toggle\Context</parameter>
<parameter key="qandidate.toggle.data_collector.toggle_collector.class">Qandidate\Bundle\ToggleBundle\DataCollector\ToggleCollector</parameter>
</parameters>

<services>
Expand Down Expand Up @@ -47,5 +48,11 @@
<tag name="kernel.event_listener" event="kernel.controller" />
</service>

<service id="qandidate.toggle.data_collector.toggle_collector" class="%qandidate.toggle.data_collector.toggle_collector.class%">
<argument type="service" id="qandidate.toggle.manager" />
<argument type="service" id="qandidate.toggle.context_factory" />
<tag name="data_collector" id="qandidate.toggle_collector" template="@QandidateToggle\data_collector\toggle.html.twig" />
</service>

</services>
</container>
99 changes: 99 additions & 0 deletions Resources/views/data_collector/toggle.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
{% extends '@WebProfiler/Profiler/layout.html.twig' %}

{% block toolbar %}

{% set icon %}
<span class="sf-toolbar-value">Toggle</span>
{% endset %}

{% set text %}
{% for toggle in collector.toggleDetails %}
<div class="sf-toolbar-info-piece">
<b>{{ toggle.name }}</b>
{% if toggle.name is active feature %}
<span class="sf-toolbar-status sf-toolbar-status-green">active</span>
{% else %}
<span class="sf-toolbar-status sf-toolbar-status-yellow">inactive</span>
{% endif %}
</div>
{% endfor %}
{% endset %}

{{ include('@WebProfiler/Profiler/toolbar_item.html.twig') }}

{% endblock %}

{% block menu %}
<span class="label">
<strong>Toggle</strong>
</span>
{% endblock %}

{% block panel %}
<h2>Context</h2>

{% if collector.context.toArray|length %}
<table>
<thead>
<tr>
<th>Context</th>
<th>Value</th>
</tr>
</thead>
<tbody>
{% for contextName, contextValue in collector.context.toArray %}
<tr>
<td>{{ contextName }}</td>
<td>{{ contextValue }}</td>
</tr>
{% endfor %}
</tbody>
</table>
{% else %}
<div class="empty">
<p>No context data found.</p>
</div>
{% endif %}

<h2>Toggles</h2>

{% if collector.toggleDetails|length %}
<table>
<thead>
<tr>
<th>Toggle name</th>
<th>Conditions</th>
<th>Status</th>
<th>Current Status</th>
</tr>
</thead>
<tbody>
{% for toggleDetails in collector.toggleDetails %}
<tr>
<td>{{ toggleDetails.name }}</td>
<td>{{ block('toggle_detail_conditions') }}</td>
<td>{{ toggleDetails.status }}</td>
{% if toggleDetails.name is active feature %}
<td><span class="label status-success">active</span></td>
{% else %}
<td><span class="label status-warning">inactive</span></td>
{% endif %}
</tr>
{% endfor %}
</tbody>
</table>
{% else %}
<div class="empty">
<p>No toggle definition found.</p>
</div>
{% endif %}
{% endblock %}

{% block toggle_detail_conditions %}
{% for condition in toggleDetails.conditions %}
<b>{{ condition.name }}</b>: {{ condition.key }} {{ condition.operator.name }} {{ condition.operator.value }}
{% if not loop.last %}<br />{% endif %}
{% else %}
No conditions
{% endfor %}
{% endblock %}
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
"php": ">=7.0",
"qandidate/toggle": "~1.0",
"symfony/framework-bundle": "^2.7||^3.0||^4.0",
"symfony/http-foundation": "^2.7||^3.0||^4.0",
"symfony/http-kernel": "^2.7||^3.0||^4.0",
"symfony/security-bundle": "^2.7||^3.0||^4.0",
"doctrine/common": "~2.2"
},
Expand Down

0 comments on commit e812c86

Please sign in to comment.