From e812c862ca6aa6bf181fe18da42da88fb5586551 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Falk=20D=C3=B6ring?= Date: Tue, 19 Dec 2017 14:33:17 +0100 Subject: [PATCH] data collector (#61) * add data collector * replace code with ToggleSerializer * use yellow for inactive toggles * Add description for the data collector to the README.md --- DataCollector/ToggleCollector.php | 96 ++++++++++++++++++ README.md | 9 ++ Resources/config/services.xml | 11 ++- .../views/data_collector/toggle.html.twig | 99 +++++++++++++++++++ composer.json | 2 + 5 files changed, 215 insertions(+), 2 deletions(-) create mode 100644 DataCollector/ToggleCollector.php create mode 100644 Resources/views/data_collector/toggle.html.twig diff --git a/DataCollector/ToggleCollector.php b/DataCollector/ToggleCollector.php new file mode 100644 index 0000000..5c39440 --- /dev/null +++ b/DataCollector/ToggleCollector.php @@ -0,0 +1,96 @@ + + * + * 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(); + } +} diff --git a/README.md b/README.md index bf5a4ac..992b5a0 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/Resources/config/services.xml b/Resources/config/services.xml index ce6a2ba..9b99a92 100644 --- a/Resources/config/services.xml +++ b/Resources/config/services.xml @@ -1,8 +1,8 @@ + 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"> Qandidate\Toggle\ToggleManager @@ -13,6 +13,7 @@ Qandidate\Bundle\ToggleBundle\Twig\ToggleTwigExtension Qandidate\Bundle\ToggleBundle\EventListener\ToggleListener Qandidate\Toggle\Context + Qandidate\Bundle\ToggleBundle\DataCollector\ToggleCollector @@ -47,5 +48,11 @@ + + + + + + diff --git a/Resources/views/data_collector/toggle.html.twig b/Resources/views/data_collector/toggle.html.twig new file mode 100644 index 0000000..cab1eac --- /dev/null +++ b/Resources/views/data_collector/toggle.html.twig @@ -0,0 +1,99 @@ +{% extends '@WebProfiler/Profiler/layout.html.twig' %} + +{% block toolbar %} + + {% set icon %} + Toggle + {% endset %} + + {% set text %} + {% for toggle in collector.toggleDetails %} +
+ {{ toggle.name }} + {% if toggle.name is active feature %} + active + {% else %} + inactive + {% endif %} +
+ {% endfor %} + {% endset %} + + {{ include('@WebProfiler/Profiler/toolbar_item.html.twig') }} + +{% endblock %} + +{% block menu %} + + Toggle + +{% endblock %} + +{% block panel %} +

Context

+ + {% if collector.context.toArray|length %} + + + + + + + + + {% for contextName, contextValue in collector.context.toArray %} + + + + + {% endfor %} + +
ContextValue
{{ contextName }}{{ contextValue }}
+ {% else %} +
+

No context data found.

+
+ {% endif %} + +

Toggles

+ + {% if collector.toggleDetails|length %} + + + + + + + + + + + {% for toggleDetails in collector.toggleDetails %} + + + + + {% if toggleDetails.name is active feature %} + + {% else %} + + {% endif %} + + {% endfor %} + +
Toggle nameConditionsStatusCurrent Status
{{ toggleDetails.name }}{{ block('toggle_detail_conditions') }}{{ toggleDetails.status }}activeinactive
+ {% else %} +
+

No toggle definition found.

+
+ {% endif %} +{% endblock %} + +{% block toggle_detail_conditions %} + {% for condition in toggleDetails.conditions %} + {{ condition.name }}: {{ condition.key }} {{ condition.operator.name }} {{ condition.operator.value }} + {% if not loop.last %}
{% endif %} + {% else %} + No conditions + {% endfor %} +{% endblock %} diff --git a/composer.json b/composer.json index 78fed55..57e79f3 100644 --- a/composer.json +++ b/composer.json @@ -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" },