-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
Showing
5 changed files
with
215 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
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(); | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
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 %} |
This file contains 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